chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / stdio-common / scanf14.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <wchar.h>
5 #include <gnu/option-groups.h>
6
7 #define FAIL() \
8   do {                                                  \
9     result = 1;                                         \
10     printf ("test at line %d failed\n", __LINE__);      \
11   } while (0)
12
13 int
14 main (void)
15 {
16   wchar_t *lsp;
17   char *sp;
18   float f;
19   double d;
20   char c[8];
21   int result = 0;
22
23   if (sscanf (" 0.25s x", "%e%3c", &f, c) != 2)
24     FAIL ();
25   else if (f != 0.25 || memcmp (c, "s x", 3) != 0)
26     FAIL ();
27   if (sscanf (" 1.25s x", "%as%2c", &sp, c) != 2)
28     FAIL ();
29   else
30     {
31       if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0)
32         FAIL ();
33       memset (sp, 'x', sizeof "1.25s");
34       free (sp);
35     }
36   if (sscanf (" 2.25s x", "%las%2c", &d, c) != 2)
37     FAIL ();
38   else if (d != 2.25 || memcmp (c, " x", 2) != 0)
39     FAIL ();
40 #if __OPTION_EGLIBC_LOCALE_CODE
41   if (sscanf (" 3.25S x", "%4aS%3c", &lsp, c) != 2)
42     FAIL ();
43   else
44     {
45       if (wcscmp (lsp, L"3.25") != 0 || memcmp (c, "S x", 3) != 0)
46         FAIL ();
47       memset (lsp, 'x', sizeof L"3.25");
48       free (lsp);
49     }
50 #endif
51   if (sscanf ("4.25[0-9.] x", "%a[0-9.]%8c", &sp, c) != 2)
52     FAIL ();
53   else
54     {
55       if (strcmp (sp, "4.25") != 0 || memcmp (c, "[0-9.] x", 8) != 0)
56         FAIL ();
57       memset (sp, 'x', sizeof "4.25");
58       free (sp);
59     }
60   if (sscanf ("5.25[0-9.] x", "%la[0-9.]%2c", &d, c) != 2)
61     FAIL ();
62   else if (d != 5.25 || memcmp (c, " x", 2) != 0)
63     FAIL ();
64
65   const char *tmpdir = getenv ("TMPDIR");
66   if (tmpdir == NULL || tmpdir[0] == '\0')
67     tmpdir = "/tmp";
68
69   char fname[strlen (tmpdir) + sizeof "/tst-scanf14.XXXXXX"];
70   sprintf (fname, "%s/tst-scanf14.XXXXXX", tmpdir);
71   if (fname == NULL)
72     FAIL ();
73
74   /* Create a temporary file.   */
75   int fd = mkstemp (fname);
76   if (fd == -1)
77     FAIL ();
78
79   FILE *fp = fdopen (fd, "w+");
80   if (fp == NULL)
81     FAIL ();
82   else
83     {
84       if (fputs (" 1.25s x", fp) == EOF)
85         FAIL ();
86       if (fseek (fp, 0, SEEK_SET) != 0)
87         FAIL ();
88       if (fscanf (fp, "%as%2c", &sp, c) != 2)
89         FAIL ();
90       else
91         {
92           if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0)
93             FAIL ();
94           memset (sp, 'x', sizeof "1.25s");
95           free (sp);
96         }
97
98       if (freopen (fname, "r", stdin) == NULL)
99         FAIL ();
100       else
101         {
102           if (scanf ("%as%2c", &sp, c) != 2)
103             FAIL ();
104           else
105             {
106               if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0)
107                 FAIL ();
108               memset (sp, 'x', sizeof "1.25s");
109               free (sp);
110             }
111         }
112
113       fclose (fp);
114     }
115
116   remove (fname);
117
118   return result;
119 }