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