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