]> git.ipfire.org Git - thirdparty/glibc.git/blob - stdio-common/scanf17.c
[powerpc] No need to enter "Ignore Exceptions Mode"
[thirdparty/glibc.git] / stdio-common / scanf17.c
1 /* Copyright (C) 2008-2019 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18 #include <stdarg.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <wchar.h>
23
24 #if __GLIBC_USE_DEPRECATED_SCANF
25 # error "This file should not be compiled with deprecated scanf"
26 #endif
27
28 #define FAIL() \
29 do { \
30 result = 1; \
31 printf ("test at line %d failed\n", __LINE__); \
32 } while (0)
33
34 static int
35 xsscanf (const char *str, const char *fmt, ...)
36 {
37 va_list ap;
38 va_start (ap, fmt);
39 int ret = vsscanf (str, fmt, ap);
40 va_end (ap);
41 return ret;
42 }
43
44 static int
45 xscanf (const char *fmt, ...)
46 {
47 va_list ap;
48 va_start (ap, fmt);
49 int ret = vscanf (fmt, ap);
50 va_end (ap);
51 return ret;
52 }
53
54 static int
55 xfscanf (FILE *f, const char *fmt, ...)
56 {
57 va_list ap;
58 va_start (ap, fmt);
59 int ret = vfscanf (f, fmt, ap);
60 va_end (ap);
61 return ret;
62 }
63
64 int
65 main (void)
66 {
67 float f;
68 double d;
69 char c[8];
70 int result = 0;
71
72 if (xsscanf (" 0.25s x", "%e%3c", &f, c) != 2)
73 FAIL ();
74 else if (f != 0.25 || memcmp (c, "s x", 3) != 0)
75 FAIL ();
76 if (xsscanf (" 1.25s x", "%as%2c", &f, c) != 2)
77 FAIL ();
78 else if (f != 1.25 || memcmp (c, " x", 2) != 0)
79 FAIL ();
80 if (xsscanf (" 2.25s x", "%las%2c", &d, c) != 2)
81 FAIL ();
82 else if (d != 2.25 || memcmp (c, " x", 2) != 0)
83 FAIL ();
84 if (xsscanf (" 3.25S x", "%4aS%2c", &f, c) != 2)
85 FAIL ();
86 else if (f != 3.25 || memcmp (c, " x", 2) != 0)
87 FAIL ();
88 if (xsscanf (" 4.25[0-9.] x", "%a[0-9.]%2c", &f, c) != 2)
89 FAIL ();
90 else if (f != 4.25 || memcmp (c, " x", 2) != 0)
91 FAIL ();
92 if (xsscanf (" 5.25[0-9.] x", "%la[0-9.]%2c", &d, c) != 2)
93 FAIL ();
94 else if (d != 5.25 || memcmp (c, " x", 2) != 0)
95 FAIL ();
96
97 const char *tmpdir = getenv ("TMPDIR");
98 if (tmpdir == NULL || tmpdir[0] == '\0')
99 tmpdir = "/tmp";
100
101 char fname[strlen (tmpdir) + sizeof "/tst-scanf17.XXXXXX"];
102 sprintf (fname, "%s/tst-scanf17.XXXXXX", tmpdir);
103 if (fname == NULL)
104 FAIL ();
105
106 /* Create a temporary file. */
107 int fd = mkstemp (fname);
108 if (fd == -1)
109 FAIL ();
110
111 FILE *fp = fdopen (fd, "w+");
112 if (fp == NULL)
113 FAIL ();
114 else
115 {
116 if (fputs (" 1.25s x", fp) == EOF)
117 FAIL ();
118 if (fseek (fp, 0, SEEK_SET) != 0)
119 FAIL ();
120 if (xfscanf (fp, "%as%2c", &f, c) != 2)
121 FAIL ();
122 else if (f != 1.25 || memcmp (c, " x", 2) != 0)
123 FAIL ();
124
125 if (freopen (fname, "r", stdin) == NULL)
126 FAIL ();
127 else
128 {
129 if (xscanf ("%as%2c", &f, c) != 2)
130 FAIL ();
131 else if (f != 1.25 || memcmp (c, " x", 2) != 0)
132 FAIL ();
133 }
134
135 fclose (fp);
136 }
137
138 remove (fname);
139
140 return result;
141 }