Back to index

glibc  2.9
tstscanf.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,92,1996-2001,2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #ifdef BSD
00020 #include </usr/include/stdio.h>
00021 #else
00022 #include <stdio.h>
00023 #endif
00024 #include <math.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 
00028 
00029 int
00030 main (int argc, char **argv)
00031 {
00032   char buf[BUFSIZ];
00033   FILE *in = stdin, *out = stdout;
00034   int x;
00035   int result = 0;
00036 
00037   if (sscanf ("0", "%d", &x) != 1)
00038     {
00039       fputs ("test failed!\n", stdout);
00040       result = 1;
00041     }
00042 
00043   if (sscanf ("08905x", "%9[0-9]", buf) != 1
00044       || strcmp (buf, "08905") != 0)
00045     {
00046       fputs ("test failed!\n", stdout);
00047       result = 1;
00048     }
00049 
00050   if (sscanf ("", "%10[a-z]", buf) != EOF)
00051     {
00052       fputs ("test failed!\n", stdout);
00053       result = 1;
00054     }
00055 
00056   sscanf ("conversion] Zero flag Ze]ro#\n", "%*[^]] %[^#]\n", buf);
00057   if (strcmp (buf, "] Zero flag Ze]ro") != 0)
00058     {
00059       fputs ("test failed!\n", stdout);
00060       result = 1;
00061     }
00062 
00063   if (argc == 2 && !strcmp (argv[1], "-opipe"))
00064     {
00065       out = popen ("/bin/cat", "w");
00066       if (out == NULL)
00067        {
00068          perror ("popen: /bin/cat");
00069          result = 1;
00070        }
00071     }
00072   else if (argc == 3 && !strcmp (argv[1], "-ipipe"))
00073     {
00074       sprintf (buf, "/bin/cat %s", argv[2]);
00075       in = popen (buf, "r");
00076       if (in == NULL)
00077        {
00078          perror ("popen: /bin/cat");
00079          result = 1;
00080        }
00081     }
00082 
00083   {
00084     char name[50];
00085     fprintf (out,
00086             "sscanf (\"thompson\", \"%%s\", name) == %d, name == \"%s\"\n",
00087             sscanf ("thompson", "%s", name),
00088             name);
00089     if (strcmp (name, "thompson") != 0)
00090       {
00091        fputs ("test failed!\n", stdout);
00092        result = 1;
00093       }
00094   }
00095 
00096   fputs ("Testing scanf (vfscanf)\n", out);
00097 
00098   fputs ("Test 1:\n", out);
00099   {
00100     int n, i;
00101     float x;
00102     char name[50];
00103     n = fscanf (in, "%d%f%s", &i, &x, name);
00104     fprintf (out, "n = %d, i = %d, x = %f, name = \"%.50s\"\n",
00105             n, i, x, name);
00106     if (n != 3 || i != 25 || x != 5.432F || strcmp (name, "thompson"))
00107       {
00108        fputs ("test failed!\n", stdout);
00109        result = 1;
00110       }
00111   }
00112   fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
00113   if (strcmp (buf, "\n"))
00114     {
00115       fputs ("test failed!\n", stdout);
00116       result = 1;
00117     }
00118   fputs ("Test 2:\n", out);
00119   {
00120     int i;
00121     float x;
00122     char name[50];
00123     (void) fscanf (in, "%2d%f%*d %[0123456789]", &i, &x, name);
00124     fprintf (out, "i = %d, x = %f, name = \"%.50s\"\n", i, x, name);
00125     if (i != 56 || x != 789.0F || strcmp (name, "56"))
00126       {
00127        fputs ("test failed!\n", stdout);
00128        result = 1;
00129       }
00130   }
00131   fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
00132   if (strcmp (buf, "a72\n"))
00133     {
00134       fputs ("test failed!\n", stdout);
00135       result = 1;
00136     }
00137   fputs ("Test 3:\n", out);
00138   {
00139     static struct {
00140       int count;
00141       float quant;
00142       const char *units;
00143       const char *item;
00144     } ok[] = {
00145       { 3, 2.0F, "quarts", "oil" },
00146       { 2, -12.8F, "degrees", "" },
00147       { 0, 0.0F, "", "" },
00148       { 3, 10.0F, "LBS", "fertilizer" },
00149       { 3, 100.0F, "rgs", "energy" },
00150       { -1, 0.0F, "", "" }};
00151     size_t rounds = 0;
00152     float quant;
00153     char units[21], item[21];
00154     while (!feof (in) && !ferror (in))
00155       {
00156        int count;
00157 
00158        if (rounds++ >= sizeof (ok) / sizeof (ok[0]))
00159          {
00160            fputs ("test failed!\n", stdout);
00161            result = 1;
00162          }
00163 
00164        quant = 0.0;
00165        units[0] = item[0] = '\0';
00166        count = fscanf (in, "%f%20s of %20s", &quant, units, item);
00167        (void) fscanf (in, "%*[^\n]");
00168        fprintf (out, "count = %d, quant = %f, item = %.21s, units = %.21s\n",
00169                count, quant, item, units);
00170        if (count != ok[rounds-1].count || quant != ok[rounds-1].quant
00171            || strcmp (item, ok[rounds-1].item)
00172            || strcmp (units, ok[rounds-1].units))
00173          {
00174            fputs ("test failed!\n", stdout);
00175            result = 1;
00176          }
00177       }
00178   }
00179   buf[0] = '\0';
00180   fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
00181   if (strcmp (buf, ""))
00182     {
00183       fputs ("test failed!\n", stdout);
00184       result = 1;
00185     }
00186 
00187   if (out != stdout)
00188     pclose (out);
00189 
00190   fputs ("Test 4:\n", out);
00191   {
00192     int res, val, n;
00193 
00194     res = sscanf ("-242", "%3o%n", &val, &n);
00195     printf ("res = %d, val = %d, n = %d\n", res, val, n);
00196     if (res != 1 || val != -20 || n != 3)
00197       {
00198        fputs ("test failed!\n", stdout);
00199        result = 1;
00200       }
00201   }
00202 
00203   fputs ("Test 5:\n", out);
00204   {
00205     double a = 0, b = 0;
00206     int res, n;
00207 
00208     res = sscanf ("1234567", "%3lg%3lg%n", &a, &b, &n);
00209     printf ("res = %d, a = %g, b = %g, n = %d\n", res, a, b, n);
00210 
00211     if (res != 2 || a != 123 || b != 456 || n != 6)
00212       {
00213        fputs ("test failed!\n", stdout);
00214        result = 1;
00215       }
00216 
00217     res = sscanf ("0", "%lg", &a);
00218     printf ("res = %d, a = %g\n", res, a);
00219 
00220     if (res != 1 || a != 0)
00221       {
00222        fputs ("test failed!\n", stdout);
00223        result = 1;
00224       }
00225 
00226     res = sscanf ("1e3", "%lg%n", &a, &n);
00227     printf ("res = %d, a = %g, n = %d\n", res, a, n);
00228 
00229     if (res != 1 || a != 1000 || n != 3)
00230       {
00231        fputs ("test failed!\n", stdout);
00232        result = 1;
00233       }
00234   }
00235 
00236   fputs ("Test 6:\n", stdout);
00237   {
00238     char *p = (char *) -1;
00239     int res;
00240 
00241     sprintf (buf, "%p", NULL);
00242     res = sscanf (buf, "%p", &p);
00243     printf ("sscanf (\"%s\", \"%%p\", &p) = %d, p == %p\n", buf, res, p);
00244 
00245     if (res != 1 || p != NULL)
00246       {
00247        fputs ("test failed!\n", stdout);
00248        result = 1;
00249       }
00250   }
00251 
00252   fputs ("Test 7:\n", stdout);
00253   {
00254     short a[2] = { -1, -1 };
00255     int res;
00256 
00257     res = sscanf ("32767 1234", "%hd %hd", &a[0], &a[1]);
00258     printf ("res = %d, a[0] = %d, a[1] = %d\n", res, a[0], a[1]);
00259 
00260     if (res != 2 || a[0] != 32767 || a[1] != 1234)
00261       {
00262        fputs ("test failed!\n", stdout);
00263        result = 1;
00264       }
00265   }
00266 
00267   fputs ("Test 8:\n", stdout);
00268   {
00269     double d = 123456.789;
00270     int res;
00271 
00272     res = sscanf ("0x1234", "%lf", &d);
00273     printf ("res = %d, d = %f\n", res, d);
00274 
00275     if (res != 1 || d != 4660)
00276       {
00277        fputs ("test failed!\n", stdout);
00278        result = 1;
00279       }
00280   }
00281 
00282   fputs ("Test 9:\n", stdout);
00283   {
00284     /* From PR libc/1313 reported by Ben Caradoc-Davies <bmcd@physics.otago.ac.nz>.  */
00285     float value;
00286     int res;
00287 
00288     res = sscanf ("0123", "%2f", &value);
00289     if (res != 1 || value != 1.0)
00290       {
00291        fputs ("test failed!\n", stdout);
00292        result = 1;
00293       }
00294   }
00295 
00296   fputs ("Test 10:\n", stdout);
00297   {
00298     float value;
00299     int res;
00300 
00301     res = sscanf ("--", "%f", &value);
00302     if (res != 0)
00303       {
00304        fputs ("test failed!\n", stdout);
00305        result = 1;
00306       }
00307   }
00308 
00309   fputs ("Test 11:\n", stdout);
00310   {
00311     char uart[50];
00312     int res;
00313 
00314     res = sscanf ("uart:16550A tx:0", "uart:%31s tx:%*u", uart);
00315     if (res != 1)
00316       {
00317        fputs ("test failed!\n", stdout);
00318        result = 1;
00319       }
00320   }
00321 
00322   fputs ("Test 12:\n", stdout);
00323   {
00324     char uart[50];
00325     int res;
00326 
00327     res = sscanf ("uart:16550A", "uart:%31s tx:%*u", uart);
00328     if (res != 1)
00329       {
00330        fputs ("test failed!\n", stdout);
00331        result = 1;
00332       }
00333   }
00334 
00335   fputs ("Test 13:\n", stdout);
00336   {
00337     float value;
00338     int res;
00339 
00340     res = sscanf ("-InF", "%f", &value);
00341     if (res != 1 || isinf (value) != -1)
00342       {
00343        fputs ("test failed!\n", stdout);
00344        result = 1;
00345       }
00346 
00347     res = sscanf ("+InfiNiTY", "%f", &value);
00348     if (res != 1 || isinf (value) != 1)
00349       {
00350        fputs ("test failed!\n", stdout);
00351        result = 1;
00352       }
00353   }
00354 
00355   return result;
00356 }