]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdio-common/scanf16.c
[powerpc] No need to enter "Ignore Exceptions Mode"
[thirdparty/glibc.git] / stdio-common / scanf16.c
CommitLineData
03992356
ZW
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
5a82c748 16 <https://www.gnu.org/licenses/>. */
03992356 17
ad8a5511
JJ
18#include <stdarg.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <wchar.h>
23
03992356
ZW
24#if __GLIBC_USE_DEPRECATED_SCANF
25# error "This file should not be compiled with deprecated scanf"
26#endif
27
ad8a5511
JJ
28#define FAIL() \
29 do { \
30 result = 1; \
31 printf ("test at line %d failed\n", __LINE__); \
32 } while (0)
33
03992356 34static int __attribute__ ((format (scanf, 2, 3)))
ad8a5511
JJ
35xsscanf (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
03992356 44static int __attribute__ ((format (scanf, 1, 2)))
ad8a5511
JJ
45xscanf (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
03992356 54static int __attribute__ ((format (scanf, 2, 3)))
ad8a5511
JJ
55xfscanf (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
64int
65main (void)
66{
67 wchar_t *lsp;
68 char *sp;
69 float f;
70 double d;
71 char c[8];
72 int result = 0;
73
74 if (xsscanf (" 0.25s x", "%e%3c", &f, c) != 2)
75 FAIL ();
76 else if (f != 0.25 || memcmp (c, "s x", 3) != 0)
77 FAIL ();
03992356 78 if (xsscanf (" 1.25s x", "%ms%2c", &sp, c) != 2)
ad8a5511
JJ
79 FAIL ();
80 else
81 {
82 if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0)
83 FAIL ();
84 memset (sp, 'x', sizeof "1.25s");
85 free (sp);
86 }
87 if (xsscanf (" 2.25s x", "%las%2c", &d, c) != 2)
88 FAIL ();
89 else if (d != 2.25 || memcmp (c, " x", 2) != 0)
90 FAIL ();
03992356 91 if (xsscanf (" 3.25S x", "%4mS%3c", &lsp, c) != 2)
ad8a5511
JJ
92 FAIL ();
93 else
94 {
95 if (wcscmp (lsp, L"3.25") != 0 || memcmp (c, "S x", 3) != 0)
96 FAIL ();
97 memset (lsp, 'x', sizeof L"3.25");
98 free (lsp);
99 }
03992356 100 if (xsscanf ("4.25[0-9.] x", "%m[0-9.]%8c", &sp, c) != 2)
ad8a5511
JJ
101 FAIL ();
102 else
103 {
104 if (strcmp (sp, "4.25") != 0 || memcmp (c, "[0-9.] x", 8) != 0)
105 FAIL ();
106 memset (sp, 'x', sizeof "4.25");
107 free (sp);
108 }
109 if (xsscanf ("5.25[0-9.] x", "%la[0-9.]%2c", &d, c) != 2)
110 FAIL ();
111 else if (d != 5.25 || memcmp (c, " x", 2) != 0)
112 FAIL ();
113
114 const char *tmpdir = getenv ("TMPDIR");
115 if (tmpdir == NULL || tmpdir[0] == '\0')
116 tmpdir = "/tmp";
117
118 char fname[strlen (tmpdir) + sizeof "/tst-scanf16.XXXXXX"];
119 sprintf (fname, "%s/tst-scanf16.XXXXXX", tmpdir);
120 if (fname == NULL)
121 FAIL ();
122
123 /* Create a temporary file. */
124 int fd = mkstemp (fname);
125 if (fd == -1)
126 FAIL ();
127
128 FILE *fp = fdopen (fd, "w+");
129 if (fp == NULL)
130 FAIL ();
131 else
132 {
133 if (fputs (" 1.25s x", fp) == EOF)
134 FAIL ();
135 if (fseek (fp, 0, SEEK_SET) != 0)
136 FAIL ();
03992356 137 if (xfscanf (fp, "%ms%2c", &sp, c) != 2)
ad8a5511
JJ
138 FAIL ();
139 else
140 {
141 if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0)
142 FAIL ();
143 memset (sp, 'x', sizeof "1.25s");
144 free (sp);
145 }
146
147 if (freopen (fname, "r", stdin) == NULL)
148 FAIL ();
149 else
150 {
03992356 151 if (xscanf ("%ms%2c", &sp, c) != 2)
ad8a5511
JJ
152 FAIL ();
153 else
154 {
155 if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0)
156 FAIL ();
157 memset (sp, 'x', sizeof "1.25s");
158 free (sp);
159 }
160 }
161
162 fclose (fp);
163 }
164
165 remove (fname);
166
167 return result;
168}