2.9
[glibc/nacl-glibc.git] / stdio-common / scanf17.c
blobee9024f9b7aa4ca581eecc32ff38c120bafb3dfe
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 <stdarg.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <wchar.h>
15 #define FAIL() \
16 do { \
17 result = 1; \
18 printf ("test at line %d failed\n", __LINE__); \
19 } while (0)
21 static int
22 xsscanf (const char *str, const char *fmt, ...)
24 va_list ap;
25 va_start (ap, fmt);
26 int ret = vsscanf (str, fmt, ap);
27 va_end (ap);
28 return ret;
31 static int
32 xscanf (const char *fmt, ...)
34 va_list ap;
35 va_start (ap, fmt);
36 int ret = vscanf (fmt, ap);
37 va_end (ap);
38 return ret;
41 static int
42 xfscanf (FILE *f, const char *fmt, ...)
44 va_list ap;
45 va_start (ap, fmt);
46 int ret = vfscanf (f, fmt, ap);
47 va_end (ap);
48 return ret;
51 int
52 main (void)
54 float f;
55 double d;
56 char c[8];
57 int result = 0;
59 if (xsscanf (" 0.25s x", "%e%3c", &f, c) != 2)
60 FAIL ();
61 else if (f != 0.25 || memcmp (c, "s x", 3) != 0)
62 FAIL ();
63 if (xsscanf (" 1.25s x", "%as%2c", &f, c) != 2)
64 FAIL ();
65 else if (f != 1.25 || memcmp (c, " x", 2) != 0)
66 FAIL ();
67 if (xsscanf (" 2.25s x", "%las%2c", &d, c) != 2)
68 FAIL ();
69 else if (d != 2.25 || memcmp (c, " x", 2) != 0)
70 FAIL ();
71 if (xsscanf (" 3.25S x", "%4aS%2c", &f, c) != 2)
72 FAIL ();
73 else if (f != 3.25 || memcmp (c, " x", 2) != 0)
74 FAIL ();
75 if (xsscanf (" 4.25[0-9.] x", "%a[0-9.]%2c", &f, c) != 2)
76 FAIL ();
77 else if (f != 4.25 || memcmp (c, " x", 2) != 0)
78 FAIL ();
79 if (xsscanf (" 5.25[0-9.] x", "%la[0-9.]%2c", &d, c) != 2)
80 FAIL ();
81 else if (d != 5.25 || memcmp (c, " x", 2) != 0)
82 FAIL ();
84 const char *tmpdir = getenv ("TMPDIR");
85 if (tmpdir == NULL || tmpdir[0] == '\0')
86 tmpdir = "/tmp";
88 char fname[strlen (tmpdir) + sizeof "/tst-scanf17.XXXXXX"];
89 sprintf (fname, "%s/tst-scanf17.XXXXXX", tmpdir);
90 if (fname == NULL)
91 FAIL ();
93 /* Create a temporary file. */
94 int fd = mkstemp (fname);
95 if (fd == -1)
96 FAIL ();
98 FILE *fp = fdopen (fd, "w+");
99 if (fp == NULL)
100 FAIL ();
101 else
103 if (fputs (" 1.25s x", fp) == EOF)
104 FAIL ();
105 if (fseek (fp, 0, SEEK_SET) != 0)
106 FAIL ();
107 if (xfscanf (fp, "%as%2c", &f, c) != 2)
108 FAIL ();
109 else if (f != 1.25 || memcmp (c, " x", 2) != 0)
110 FAIL ();
112 if (freopen (fname, "r", stdin) == NULL)
113 FAIL ();
114 else
116 if (xscanf ("%as%2c", &f, c) != 2)
117 FAIL ();
118 else if (f != 1.25 || memcmp (c, " x", 2) != 0)
119 FAIL ();
122 fclose (fp);
125 remove (fname);
127 return result;