]>
Commit | Line | Data |
---|---|---|
2b778ceb | 1 | /* Copyright (C) 2007-2021 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 ZW |
17 | |
18 | /* This test exercises the deprecated GNU %as, %aS, and %a[...] scanf | |
19 | modifiers, which are not available to programs compiled as C99 | |
20 | anymore; therefore, this file is compiled with -std=gnu89 and C99 | |
21 | syntax must not be used. */ | |
22 | ||
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include <wchar.h> | |
27 | ||
28 | #if !__GLIBC_USE_DEPRECATED_SCANF | |
29 | # error "This file should be compiled with deprecated scanf" | |
30 | #endif | |
31 | ||
32 | ||
33 | #define FAIL() \ | |
34 | do { \ | |
35 | result = 1; \ | |
36 | printf ("test at line %d failed\n", __LINE__); \ | |
37 | } while (0) | |
38 | ||
39 | int | |
40 | main (void) | |
41 | { | |
42 | wchar_t *lsp; | |
43 | char *sp; | |
44 | float f; | |
45 | double d; | |
46 | char c[8]; | |
47 | int result = 0; | |
48 | ||
49 | if (sscanf (" 0.25s x", "%e%3c", &f, c) != 2) | |
50 | FAIL (); | |
51 | else if (f != 0.25 || memcmp (c, "s x", 3) != 0) | |
52 | FAIL (); | |
53 | if (sscanf (" 1.25s x", "%as%2c", &sp, c) != 2) | |
54 | FAIL (); | |
55 | else | |
56 | { | |
57 | if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0) | |
58 | FAIL (); | |
59 | memset (sp, 'x', sizeof "1.25s"); | |
60 | free (sp); | |
61 | } | |
62 | if (sscanf (" 2.25s x", "%las%2c", &d, c) != 2) | |
63 | FAIL (); | |
64 | else if (d != 2.25 || memcmp (c, " x", 2) != 0) | |
65 | FAIL (); | |
66 | if (sscanf (" 3.25S x", "%4aS%3c", &lsp, c) != 2) | |
67 | FAIL (); | |
68 | else | |
69 | { | |
70 | if (wcscmp (lsp, L"3.25") != 0 || memcmp (c, "S x", 3) != 0) | |
71 | FAIL (); | |
72 | memset (lsp, 'x', sizeof L"3.25"); | |
73 | free (lsp); | |
74 | } | |
75 | if (sscanf ("4.25[0-9.] x", "%a[0-9.]%8c", &sp, c) != 2) | |
76 | FAIL (); | |
77 | else | |
78 | { | |
79 | if (strcmp (sp, "4.25") != 0 || memcmp (c, "[0-9.] x", 8) != 0) | |
80 | FAIL (); | |
81 | memset (sp, 'x', sizeof "4.25"); | |
82 | free (sp); | |
83 | } | |
84 | if (sscanf ("5.25[0-9.] x", "%la[0-9.]%2c", &d, c) != 2) | |
85 | FAIL (); | |
86 | else if (d != 5.25 || memcmp (c, " x", 2) != 0) | |
87 | FAIL (); | |
88 | ||
89 | const char *tmpdir = getenv ("TMPDIR"); | |
90 | if (tmpdir == NULL || tmpdir[0] == '\0') | |
91 | tmpdir = "/tmp"; | |
92 | ||
93 | char fname[strlen (tmpdir) + sizeof "/tst-scanf14.XXXXXX"]; | |
94 | sprintf (fname, "%s/tst-scanf14.XXXXXX", tmpdir); | |
95 | if (fname == NULL) | |
96 | FAIL (); | |
97 | ||
98 | /* Create a temporary file. */ | |
99 | int fd = mkstemp (fname); | |
100 | if (fd == -1) | |
101 | FAIL (); | |
102 | ||
103 | FILE *fp = fdopen (fd, "w+"); | |
104 | if (fp == NULL) | |
105 | FAIL (); | |
106 | else | |
107 | { | |
108 | if (fputs (" 1.25s x", fp) == EOF) | |
109 | FAIL (); | |
110 | if (fseek (fp, 0, SEEK_SET) != 0) | |
111 | FAIL (); | |
112 | if (fscanf (fp, "%as%2c", &sp, c) != 2) | |
113 | FAIL (); | |
114 | else | |
115 | { | |
116 | if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0) | |
117 | FAIL (); | |
118 | memset (sp, 'x', sizeof "1.25s"); | |
119 | free (sp); | |
120 | } | |
121 | ||
122 | if (freopen (fname, "r", stdin) == NULL) | |
123 | FAIL (); | |
124 | else | |
125 | { | |
126 | if (scanf ("%as%2c", &sp, c) != 2) | |
127 | FAIL (); | |
128 | else | |
129 | { | |
130 | if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0) | |
131 | FAIL (); | |
132 | memset (sp, 'x', sizeof "1.25s"); | |
133 | free (sp); | |
134 | } | |
135 | } | |
136 | ||
137 | fclose (fp); | |
138 | } | |
139 | ||
140 | remove (fname); | |
141 | ||
142 | return result; | |
143 | } |