Back to index

glibc  2.9
scanf16.c
Go to the documentation of this file.
00001 #include <stdarg.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include <wchar.h>
00006 
00007 #define FAIL() \
00008   do {                                           \
00009     result = 1;                                         \
00010     printf ("test at line %d failed\n", __LINE__);      \
00011   } while (0)
00012 
00013 static int
00014 xsscanf (const char *str, const char *fmt, ...)
00015 {
00016   va_list ap;
00017   va_start (ap, fmt);
00018   int ret = vsscanf (str, fmt, ap);
00019   va_end (ap);
00020   return ret;
00021 }
00022 
00023 static int
00024 xscanf (const char *fmt, ...)
00025 {
00026   va_list ap;
00027   va_start (ap, fmt);
00028   int ret = vscanf (fmt, ap);
00029   va_end (ap);
00030   return ret;
00031 }
00032 
00033 static int
00034 xfscanf (FILE *f, const char *fmt, ...)
00035 {
00036   va_list ap;
00037   va_start (ap, fmt);
00038   int ret = vfscanf (f, fmt, ap);
00039   va_end (ap);
00040   return ret;
00041 }
00042 
00043 int
00044 main (void)
00045 {
00046   wchar_t *lsp;
00047   char *sp;
00048   float f;
00049   double d;
00050   char c[8];
00051   int result = 0;
00052 
00053   if (xsscanf (" 0.25s x", "%e%3c", &f, c) != 2)
00054     FAIL ();
00055   else if (f != 0.25 || memcmp (c, "s x", 3) != 0)
00056     FAIL ();
00057   if (xsscanf (" 1.25s x", "%as%2c", &sp, c) != 2)
00058     FAIL ();
00059   else
00060     {
00061       if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0)
00062        FAIL ();
00063       memset (sp, 'x', sizeof "1.25s");
00064       free (sp);
00065     }
00066   if (xsscanf (" 2.25s x", "%las%2c", &d, c) != 2)
00067     FAIL ();
00068   else if (d != 2.25 || memcmp (c, " x", 2) != 0)
00069     FAIL ();
00070   if (xsscanf (" 3.25S x", "%4aS%3c", &lsp, c) != 2)
00071     FAIL ();
00072   else
00073     {
00074       if (wcscmp (lsp, L"3.25") != 0 || memcmp (c, "S x", 3) != 0)
00075        FAIL ();
00076       memset (lsp, 'x', sizeof L"3.25");
00077       free (lsp);
00078     }
00079   if (xsscanf ("4.25[0-9.] x", "%a[0-9.]%8c", &sp, c) != 2)
00080     FAIL ();
00081   else
00082     {
00083       if (strcmp (sp, "4.25") != 0 || memcmp (c, "[0-9.] x", 8) != 0)
00084        FAIL ();
00085       memset (sp, 'x', sizeof "4.25");
00086       free (sp);
00087     }
00088   if (xsscanf ("5.25[0-9.] x", "%la[0-9.]%2c", &d, c) != 2)
00089     FAIL ();
00090   else if (d != 5.25 || memcmp (c, " x", 2) != 0)
00091     FAIL ();
00092 
00093   const char *tmpdir = getenv ("TMPDIR");
00094   if (tmpdir == NULL || tmpdir[0] == '\0')
00095     tmpdir = "/tmp";
00096 
00097   char fname[strlen (tmpdir) + sizeof "/tst-scanf16.XXXXXX"];
00098   sprintf (fname, "%s/tst-scanf16.XXXXXX", tmpdir);
00099   if (fname == NULL)
00100     FAIL ();
00101 
00102   /* Create a temporary file.   */
00103   int fd = mkstemp (fname);
00104   if (fd == -1)
00105     FAIL ();
00106 
00107   FILE *fp = fdopen (fd, "w+");
00108   if (fp == NULL)
00109     FAIL ();
00110   else
00111     {
00112       if (fputs (" 1.25s x", fp) == EOF)
00113        FAIL ();
00114       if (fseek (fp, 0, SEEK_SET) != 0)
00115        FAIL ();
00116       if (xfscanf (fp, "%as%2c", &sp, c) != 2)
00117        FAIL ();
00118       else
00119        {
00120          if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0)
00121            FAIL ();
00122          memset (sp, 'x', sizeof "1.25s");
00123          free (sp);
00124        }
00125 
00126       if (freopen (fname, "r", stdin) == NULL)
00127        FAIL ();
00128       else
00129        {
00130          if (xscanf ("%as%2c", &sp, c) != 2)
00131            FAIL ();
00132          else
00133            {
00134              if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0)
00135               FAIL ();
00136              memset (sp, 'x', sizeof "1.25s");
00137              free (sp);
00138            }
00139        }
00140 
00141       fclose (fp);
00142     }
00143 
00144   remove (fname);
00145 
00146   return result;
00147 }