]>
Commit | Line | Data |
---|---|---|
d614a753 | 1 | /* Copyright (C) 2007-2020 Free Software Foundation, Inc. |
03992356 ZW |
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 | |
874aa523 UD |
18 | #include <stdio.h> |
19 | #include <stdlib.h> | |
20 | #include <string.h> | |
21 | #include <wchar.h> | |
03992356 ZW |
22 | |
23 | #if __GLIBC_USE_DEPRECATED_SCANF | |
24 | # error "This file should not be compiled with deprecated scanf" | |
25 | #endif | |
874aa523 UD |
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 | wchar_t *lsp; | |
37 | char *sp; | |
38 | float f; | |
39 | double d; | |
40 | char c[8]; | |
41 | int result = 0; | |
42 | ||
43 | if (sscanf (" 0.25s x", "%e%3c", &f, c) != 2) | |
44 | FAIL (); | |
45 | else if (f != 0.25 || memcmp (c, "s x", 3) != 0) | |
46 | FAIL (); | |
03992356 | 47 | if (sscanf (" 1.25s x", "%ms%2c", &sp, c) != 2) |
874aa523 UD |
48 | FAIL (); |
49 | else | |
50 | { | |
51 | if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0) | |
52 | FAIL (); | |
53 | memset (sp, 'x', sizeof "1.25s"); | |
54 | free (sp); | |
55 | } | |
56 | if (sscanf (" 2.25s x", "%las%2c", &d, c) != 2) | |
57 | FAIL (); | |
58 | else if (d != 2.25 || memcmp (c, " x", 2) != 0) | |
59 | FAIL (); | |
03992356 | 60 | if (sscanf (" 3.25S x", "%4mS%3c", &lsp, c) != 2) |
874aa523 UD |
61 | FAIL (); |
62 | else | |
63 | { | |
64 | if (wcscmp (lsp, L"3.25") != 0 || memcmp (c, "S x", 3) != 0) | |
65 | FAIL (); | |
66 | memset (lsp, 'x', sizeof L"3.25"); | |
67 | free (lsp); | |
68 | } | |
03992356 | 69 | if (sscanf ("4.25[0-9.] x", "%m[0-9.]%8c", &sp, c) != 2) |
874aa523 UD |
70 | FAIL (); |
71 | else | |
72 | { | |
73 | if (strcmp (sp, "4.25") != 0 || memcmp (c, "[0-9.] x", 8) != 0) | |
74 | FAIL (); | |
75 | memset (sp, 'x', sizeof "4.25"); | |
76 | free (sp); | |
77 | } | |
78 | if (sscanf ("5.25[0-9.] x", "%la[0-9.]%2c", &d, c) != 2) | |
79 | FAIL (); | |
80 | else if (d != 5.25 || memcmp (c, " x", 2) != 0) | |
81 | FAIL (); | |
82 | ||
ad8a5511 JJ |
83 | const char *tmpdir = getenv ("TMPDIR"); |
84 | if (tmpdir == NULL || tmpdir[0] == '\0') | |
85 | tmpdir = "/tmp"; | |
86 | ||
87 | char fname[strlen (tmpdir) + sizeof "/tst-scanf14.XXXXXX"]; | |
88 | sprintf (fname, "%s/tst-scanf14.XXXXXX", tmpdir); | |
89 | if (fname == NULL) | |
90 | FAIL (); | |
91 | ||
92 | /* Create a temporary file. */ | |
93 | int fd = mkstemp (fname); | |
94 | if (fd == -1) | |
95 | FAIL (); | |
96 | ||
97 | FILE *fp = fdopen (fd, "w+"); | |
98 | if (fp == NULL) | |
99 | FAIL (); | |
100 | else | |
101 | { | |
102 | if (fputs (" 1.25s x", fp) == EOF) | |
103 | FAIL (); | |
104 | if (fseek (fp, 0, SEEK_SET) != 0) | |
105 | FAIL (); | |
03992356 | 106 | if (fscanf (fp, "%ms%2c", &sp, c) != 2) |
ad8a5511 JJ |
107 | FAIL (); |
108 | else | |
109 | { | |
110 | if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0) | |
111 | FAIL (); | |
112 | memset (sp, 'x', sizeof "1.25s"); | |
113 | free (sp); | |
114 | } | |
115 | ||
116 | if (freopen (fname, "r", stdin) == NULL) | |
117 | FAIL (); | |
118 | else | |
119 | { | |
03992356 | 120 | if (scanf ("%ms%2c", &sp, c) != 2) |
ad8a5511 JJ |
121 | FAIL (); |
122 | else | |
123 | { | |
124 | if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0) | |
125 | FAIL (); | |
126 | memset (sp, 'x', sizeof "1.25s"); | |
127 | free (sp); | |
128 | } | |
129 | } | |
130 | ||
131 | fclose (fp); | |
132 | } | |
133 | ||
134 | remove (fname); | |
135 | ||
874aa523 UD |
136 | return result; |
137 | } |