Back to index

glibc  2.9
scanf15.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 <stdio.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012 #include <wchar.h>
00013 
00014 #define FAIL() \
00015   do {                                           \
00016     result = 1;                                         \
00017     printf ("test at line %d failed\n", __LINE__);      \
00018   } while (0)
00019 
00020 int
00021 main (void)
00022 {
00023   float f;
00024   double d;
00025   char c[8];
00026   int result = 0;
00027 
00028   if (sscanf (" 0.25s x", "%e%3c", &f, c) != 2)
00029     FAIL ();
00030   else if (f != 0.25 || memcmp (c, "s x", 3) != 0)
00031     FAIL ();
00032   if (sscanf (" 1.25s x", "%as%2c", &f, c) != 2)
00033     FAIL ();
00034   else if (f != 1.25 || memcmp (c, " x", 2) != 0)
00035     FAIL ();
00036   if (sscanf (" 2.25s x", "%las%2c", &d, c) != 2)
00037     FAIL ();
00038   else if (d != 2.25 || memcmp (c, " x", 2) != 0)
00039     FAIL ();
00040   if (sscanf (" 3.25S x", "%4aS%2c", &f, c) != 2)
00041     FAIL ();
00042   else if (f != 3.25 || memcmp (c, " x", 2) != 0)
00043     FAIL ();
00044   if (sscanf (" 4.25[0-9.] x", "%a[0-9.]%2c", &f, c) != 2)
00045     FAIL ();
00046   else if (f != 4.25 || memcmp (c, " x", 2) != 0)
00047     FAIL ();
00048   if (sscanf (" 5.25[0-9.] x", "%la[0-9.]%2c", &d, c) != 2)
00049     FAIL ();
00050   else if (d != 5.25 || memcmp (c, " x", 2) != 0)
00051     FAIL ();
00052 
00053   const char *tmpdir = getenv ("TMPDIR");
00054   if (tmpdir == NULL || tmpdir[0] == '\0')
00055     tmpdir = "/tmp";
00056 
00057   char fname[strlen (tmpdir) + sizeof "/tst-scanf15.XXXXXX"];
00058   sprintf (fname, "%s/tst-scanf15.XXXXXX", tmpdir);
00059   if (fname == NULL)
00060     FAIL ();
00061 
00062   /* Create a temporary file.   */
00063   int fd = mkstemp (fname);
00064   if (fd == -1)
00065     FAIL ();
00066 
00067   FILE *fp = fdopen (fd, "w+");
00068   if (fp == NULL)
00069     FAIL ();
00070   else
00071     {
00072       if (fputs (" 1.25s x", fp) == EOF)
00073        FAIL ();
00074       if (fseek (fp, 0, SEEK_SET) != 0)
00075        FAIL ();
00076       if (fscanf (fp, "%as%2c", &f, c) != 2)
00077        FAIL ();
00078       else if (f != 1.25 || memcmp (c, " x", 2) != 0)
00079        FAIL ();
00080 
00081       if (freopen (fname, "r", stdin) == NULL)
00082        FAIL ();
00083       else
00084        {
00085          if (scanf ("%as%2c", &f, c) != 2)
00086            FAIL ();
00087          else if (f != 1.25 || memcmp (c, " x", 2) != 0)
00088            FAIL ();
00089        }
00090 
00091       fclose (fp);
00092     }
00093 
00094   remove (fname);
00095 
00096   return result;
00097 }