Back to index

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