struct stat is not posix conform
[glibc.git] / stdio-common / scanf14.c
blobcffccb0b19d2927b11f29703680f7bb5fb9ffd82
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <wchar.h>
5 #include <libc-internal.h>
7 #define FAIL() \
8 do { \
9 result = 1; \
10 printf ("test at line %d failed\n", __LINE__); \
11 } while (0)
13 int
14 main (void)
16 wchar_t *lsp;
17 char *sp;
18 float f;
19 double d;
20 char c[8];
21 int result = 0;
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 /* GCC in C99 mode treats %a as the C99 format expecting float *,
28 but glibc with _GNU_SOURCE treats %as as the GNU allocation
29 extension, so resulting in "warning: format '%a' expects argument
30 of type 'float *', but argument 3 has type 'char **'". This
31 applies to the other %as, %aS and %a[] formats below as well. */
32 DIAG_PUSH_NEEDS_COMMENT;
33 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat");
34 if (sscanf (" 1.25s x", "%as%2c", &sp, c) != 2)
35 FAIL ();
36 else
38 if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0)
39 FAIL ();
40 memset (sp, 'x', sizeof "1.25s");
41 free (sp);
43 DIAG_POP_NEEDS_COMMENT;
44 if (sscanf (" 2.25s x", "%las%2c", &d, c) != 2)
45 FAIL ();
46 else if (d != 2.25 || memcmp (c, " x", 2) != 0)
47 FAIL ();
48 /* See explanation above. */
49 DIAG_PUSH_NEEDS_COMMENT;
50 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat");
51 if (sscanf (" 3.25S x", "%4aS%3c", &lsp, c) != 2)
52 FAIL ();
53 else
55 if (wcscmp (lsp, L"3.25") != 0 || memcmp (c, "S x", 3) != 0)
56 FAIL ();
57 memset (lsp, 'x', sizeof L"3.25");
58 free (lsp);
60 if (sscanf ("4.25[0-9.] x", "%a[0-9.]%8c", &sp, c) != 2)
61 FAIL ();
62 else
64 if (strcmp (sp, "4.25") != 0 || memcmp (c, "[0-9.] x", 8) != 0)
65 FAIL ();
66 memset (sp, 'x', sizeof "4.25");
67 free (sp);
69 DIAG_POP_NEEDS_COMMENT;
70 if (sscanf ("5.25[0-9.] x", "%la[0-9.]%2c", &d, c) != 2)
71 FAIL ();
72 else if (d != 5.25 || memcmp (c, " x", 2) != 0)
73 FAIL ();
75 const char *tmpdir = getenv ("TMPDIR");
76 if (tmpdir == NULL || tmpdir[0] == '\0')
77 tmpdir = "/tmp";
79 char fname[strlen (tmpdir) + sizeof "/tst-scanf14.XXXXXX"];
80 sprintf (fname, "%s/tst-scanf14.XXXXXX", tmpdir);
81 if (fname == NULL)
82 FAIL ();
84 /* Create a temporary file. */
85 int fd = mkstemp (fname);
86 if (fd == -1)
87 FAIL ();
89 FILE *fp = fdopen (fd, "w+");
90 if (fp == NULL)
91 FAIL ();
92 else
94 if (fputs (" 1.25s x", fp) == EOF)
95 FAIL ();
96 if (fseek (fp, 0, SEEK_SET) != 0)
97 FAIL ();
98 /* See explanation above. */
99 DIAG_PUSH_NEEDS_COMMENT;
100 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat");
101 if (fscanf (fp, "%as%2c", &sp, c) != 2)
102 FAIL ();
103 else
105 if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0)
106 FAIL ();
107 memset (sp, 'x', sizeof "1.25s");
108 free (sp);
110 DIAG_POP_NEEDS_COMMENT;
112 if (freopen (fname, "r", stdin) == NULL)
113 FAIL ();
114 else
116 /* See explanation above. */
117 DIAG_PUSH_NEEDS_COMMENT;
118 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat");
119 if (scanf ("%as%2c", &sp, c) != 2)
120 FAIL ();
121 else
123 if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0)
124 FAIL ();
125 memset (sp, 'x', sizeof "1.25s");
126 free (sp);
128 DIAG_POP_NEEDS_COMMENT;
131 fclose (fp);
134 remove (fname);
136 return result;