Back to index

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