2.9
[glibc/nacl-glibc.git] / stdio-common / scanf15.c
blobc56715c4868481d5c007e8bf156f298d561e7ef9
1 #undef _GNU_SOURCE
2 #define _XOPEN_SOURCE 600
3 /* The following macro definitions are a hack. They word around disabling
4 the GNU extension while still using a few internal headers. */
5 #define u_char unsigned char
6 #define u_short unsigned short
7 #define u_int unsigned int
8 #define u_long unsigned long
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <wchar.h>
14 #define FAIL() \
15 do { \
16 result = 1; \
17 printf ("test at line %d failed\n", __LINE__); \
18 } while (0)
20 int
21 main (void)
23 float f;
24 double d;
25 char c[8];
26 int result = 0;
28 if (sscanf (" 0.25s x", "%e%3c", &f, c) != 2)
29 FAIL ();
30 else if (f != 0.25 || memcmp (c, "s x", 3) != 0)
31 FAIL ();
32 if (sscanf (" 1.25s x", "%as%2c", &f, c) != 2)
33 FAIL ();
34 else if (f != 1.25 || memcmp (c, " x", 2) != 0)
35 FAIL ();
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 (sscanf (" 3.25S x", "%4aS%2c", &f, c) != 2)
41 FAIL ();
42 else if (f != 3.25 || memcmp (c, " x", 2) != 0)
43 FAIL ();
44 if (sscanf (" 4.25[0-9.] x", "%a[0-9.]%2c", &f, c) != 2)
45 FAIL ();
46 else if (f != 4.25 || memcmp (c, " x", 2) != 0)
47 FAIL ();
48 if (sscanf (" 5.25[0-9.] x", "%la[0-9.]%2c", &d, c) != 2)
49 FAIL ();
50 else if (d != 5.25 || memcmp (c, " x", 2) != 0)
51 FAIL ();
53 const char *tmpdir = getenv ("TMPDIR");
54 if (tmpdir == NULL || tmpdir[0] == '\0')
55 tmpdir = "/tmp";
57 char fname[strlen (tmpdir) + sizeof "/tst-scanf15.XXXXXX"];
58 sprintf (fname, "%s/tst-scanf15.XXXXXX", tmpdir);
59 if (fname == NULL)
60 FAIL ();
62 /* Create a temporary file. */
63 int fd = mkstemp (fname);
64 if (fd == -1)
65 FAIL ();
67 FILE *fp = fdopen (fd, "w+");
68 if (fp == NULL)
69 FAIL ();
70 else
72 if (fputs (" 1.25s x", fp) == EOF)
73 FAIL ();
74 if (fseek (fp, 0, SEEK_SET) != 0)
75 FAIL ();
76 if (fscanf (fp, "%as%2c", &f, c) != 2)
77 FAIL ();
78 else if (f != 1.25 || memcmp (c, " x", 2) != 0)
79 FAIL ();
81 if (freopen (fname, "r", stdin) == NULL)
82 FAIL ();
83 else
85 if (scanf ("%as%2c", &f, c) != 2)
86 FAIL ();
87 else if (f != 1.25 || memcmp (c, " x", 2) != 0)
88 FAIL ();
91 fclose (fp);
94 remove (fname);
96 return result;