]> git.ipfire.org Git - thirdparty/glibc.git/blob - stdio-common/scanf15.c
[powerpc] No need to enter "Ignore Exceptions Mode"
[thirdparty/glibc.git] / stdio-common / scanf15.c
1 /* Copyright (C) 2007-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 <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <wchar.h>
22
23 #if __GLIBC_USE_DEPRECATED_SCANF
24 # error "This file should not be compiled with deprecated scanf"
25 #endif
26
27 #define FAIL() \
28 do { \
29 result = 1; \
30 printf ("test at line %d failed\n", __LINE__); \
31 } while (0)
32
33 int
34 main (void)
35 {
36 float f;
37 double d;
38 char c[8];
39 int result = 0;
40
41 if (sscanf (" 0.25s x", "%e%3c", &f, c) != 2)
42 FAIL ();
43 else if (f != 0.25 || memcmp (c, "s x", 3) != 0)
44 FAIL ();
45 if (sscanf (" 1.25s x", "%as%2c", &f, c) != 2)
46 FAIL ();
47 else if (f != 1.25 || memcmp (c, " x", 2) != 0)
48 FAIL ();
49 if (sscanf (" 2.25s x", "%las%2c", &d, c) != 2)
50 FAIL ();
51 else if (d != 2.25 || memcmp (c, " x", 2) != 0)
52 FAIL ();
53 if (sscanf (" 3.25S x", "%4aS%2c", &f, c) != 2)
54 FAIL ();
55 else if (f != 3.25 || memcmp (c, " x", 2) != 0)
56 FAIL ();
57 if (sscanf (" 4.25[0-9.] x", "%a[0-9.]%2c", &f, c) != 2)
58 FAIL ();
59 else if (f != 4.25 || memcmp (c, " x", 2) != 0)
60 FAIL ();
61 if (sscanf (" 5.25[0-9.] x", "%la[0-9.]%2c", &d, c) != 2)
62 FAIL ();
63 else if (d != 5.25 || memcmp (c, " x", 2) != 0)
64 FAIL ();
65
66 const char *tmpdir = getenv ("TMPDIR");
67 if (tmpdir == NULL || tmpdir[0] == '\0')
68 tmpdir = "/tmp";
69
70 char fname[strlen (tmpdir) + sizeof "/tst-scanf15.XXXXXX"];
71 sprintf (fname, "%s/tst-scanf15.XXXXXX", tmpdir);
72 if (fname == NULL)
73 FAIL ();
74
75 /* Create a temporary file. */
76 int fd = mkstemp (fname);
77 if (fd == -1)
78 FAIL ();
79
80 FILE *fp = fdopen (fd, "w+");
81 if (fp == NULL)
82 FAIL ();
83 else
84 {
85 if (fputs (" 1.25s x", fp) == EOF)
86 FAIL ();
87 if (fseek (fp, 0, SEEK_SET) != 0)
88 FAIL ();
89 if (fscanf (fp, "%as%2c", &f, c) != 2)
90 FAIL ();
91 else if (f != 1.25 || memcmp (c, " x", 2) != 0)
92 FAIL ();
93
94 if (freopen (fname, "r", stdin) == NULL)
95 FAIL ();
96 else
97 {
98 if (scanf ("%as%2c", &f, c) != 2)
99 FAIL ();
100 else if (f != 1.25 || memcmp (c, " x", 2) != 0)
101 FAIL ();
102 }
103
104 fclose (fp);
105 }
106
107 remove (fname);
108
109 return result;
110 }