Update copyright notices with scripts/update-copyrights.
[glibc.git] / stdio-common / scanf15.c
blob851466b3a9b1c49c383d598a09dd353ed92dfcd9
1 #undef _GNU_SOURCE
2 #define _XOPEN_SOURCE 600
3 #undef _LIBC
4 /* The following macro definitions are a hack. They word around disabling
5 the GNU extension while still using a few internal headers. */
6 #define u_char unsigned char
7 #define u_short unsigned short
8 #define u_int unsigned int
9 #define u_long unsigned long
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 int
22 main (void)
24 float f;
25 double d;
26 char c[8];
27 int result = 0;
29 if (sscanf (" 0.25s x", "%e%3c", &f, c) != 2)
30 FAIL ();
31 else if (f != 0.25 || memcmp (c, "s x", 3) != 0)
32 FAIL ();
33 if (sscanf (" 1.25s x", "%as%2c", &f, c) != 2)
34 FAIL ();
35 else if (f != 1.25 || memcmp (c, " x", 2) != 0)
36 FAIL ();
37 if (sscanf (" 2.25s x", "%las%2c", &d, c) != 2)
38 FAIL ();
39 else if (d != 2.25 || memcmp (c, " x", 2) != 0)
40 FAIL ();
41 if (sscanf (" 3.25S x", "%4aS%2c", &f, c) != 2)
42 FAIL ();
43 else if (f != 3.25 || memcmp (c, " x", 2) != 0)
44 FAIL ();
45 if (sscanf (" 4.25[0-9.] x", "%a[0-9.]%2c", &f, c) != 2)
46 FAIL ();
47 else if (f != 4.25 || memcmp (c, " x", 2) != 0)
48 FAIL ();
49 if (sscanf (" 5.25[0-9.] x", "%la[0-9.]%2c", &d, c) != 2)
50 FAIL ();
51 else if (d != 5.25 || memcmp (c, " x", 2) != 0)
52 FAIL ();
54 const char *tmpdir = getenv ("TMPDIR");
55 if (tmpdir == NULL || tmpdir[0] == '\0')
56 tmpdir = "/tmp";
58 char fname[strlen (tmpdir) + sizeof "/tst-scanf15.XXXXXX"];
59 sprintf (fname, "%s/tst-scanf15.XXXXXX", tmpdir);
60 if (fname == NULL)
61 FAIL ();
63 /* Create a temporary file. */
64 int fd = mkstemp (fname);
65 if (fd == -1)
66 FAIL ();
68 FILE *fp = fdopen (fd, "w+");
69 if (fp == NULL)
70 FAIL ();
71 else
73 if (fputs (" 1.25s x", fp) == EOF)
74 FAIL ();
75 if (fseek (fp, 0, SEEK_SET) != 0)
76 FAIL ();
77 if (fscanf (fp, "%as%2c", &f, c) != 2)
78 FAIL ();
79 else if (f != 1.25 || memcmp (c, " x", 2) != 0)
80 FAIL ();
82 if (freopen (fname, "r", stdin) == NULL)
83 FAIL ();
84 else
86 if (scanf ("%as%2c", &f, c) != 2)
87 FAIL ();
88 else if (f != 1.25 || memcmp (c, " x", 2) != 0)
89 FAIL ();
92 fclose (fp);
95 remove (fname);
97 return result;