]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdio-common/tstscanf.c
(__libc_argv, __libc_argc): Make extern. (__hurd_threadvar_max, __hurd_threadvar_st...
[thirdparty/glibc.git] / stdio-common / tstscanf.c
CommitLineData
33a934a3
UD
1/* Copyright (C) 1991, 1992, 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
28f540f4 3
33a934a3
UD
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
28f540f4 8
33a934a3
UD
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 Library General Public License for more details.
28f540f4 13
33a934a3
UD
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
28f540f4 18
28f540f4
RM
19#ifdef BSD
20#include </usr/include/stdio.h>
21#else
22#include <stdio.h>
23#endif
24#include <stdlib.h>
25#include <string.h>
26
27
28int
33a934a3 29main (int argc, char **argv)
28f540f4
RM
30{
31 char buf[BUFSIZ];
32 FILE *in = stdin, *out = stdout;
bfc04a9f
RM
33 int x;
34
35 if (sscanf ("0", "%d", &x) != 1)
36 exit (EXIT_FAILURE);
28f540f4 37
1177c8ba
RM
38 sscanf ("conversion] Zero flag Ze]ro#\n", "%*[^]] %[^#]\n", buf);
39 if (strcmp (buf, "] Zero flag Ze]ro") != 0)
40 {
3867ee64 41 fputs ("test failed!\n", stderr);
1177c8ba
RM
42 return 1;
43 }
44
28f540f4
RM
45 if (argc == 2 && !strcmp (argv[1], "-opipe"))
46 {
47 out = popen ("/bin/cat", "w");
48 if (out == NULL)
49 {
50 perror ("popen: /bin/cat");
51 exit (EXIT_FAILURE);
52 }
53 }
54 else if (argc == 3 && !strcmp (argv[1], "-ipipe"))
55 {
56 sprintf (buf, "/bin/cat %s", argv[2]);
57 in = popen (buf, "r");
58 }
59
60 {
61 char name[50];
62 fprintf (out,
63 "sscanf (\"thompson\", \"%%s\", name) == %d, name == \"%s\"\n",
64 sscanf ("thompson", "%s", name),
65 name);
3867ee64
RM
66 if (strcmp (name, "thompson") != 0)
67 return 1;
28f540f4
RM
68 }
69
70 fputs ("Testing scanf (vfscanf)\n", out);
71
72 fputs ("Test 1:\n", out);
73 {
74 int n, i;
75 float x;
76 char name[50];
77 n = fscanf (in, "%d%f%s", &i, &x, name);
78 fprintf (out, "n = %d, i = %d, x = %f, name = \"%.50s\"\n",
79 n, i, x, name);
3867ee64
RM
80 if (n != 3 || i != 25 || x != 5.432F || strcmp (name, "thompson"))
81 return 1;
28f540f4
RM
82 }
83 fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
3867ee64
RM
84 if (strcmp (buf, "\n"))
85 return 1;
28f540f4
RM
86 fputs ("Test 2:\n", out);
87 {
88 int i;
89 float x;
90 char name[50];
91 (void) fscanf (in, "%2d%f%*d %[0123456789]", &i, &x, name);
92 fprintf (out, "i = %d, x = %f, name = \"%.50s\"\n", i, x, name);
3867ee64
RM
93 if (i != 56 || x != 789.0F || strcmp(name, "56"))
94 return 1;
28f540f4
RM
95 }
96 fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
3867ee64
RM
97 if (strcmp (buf, "a72\n"))
98 return 1;
28f540f4
RM
99 fputs ("Test 3:\n", out);
100 {
3867ee64
RM
101 static struct {
102 int count;
103 float quant;
104 const char *units;
105 const char *item;
106 } ok[] = {
107 { 3, 2.0F, "quarts", "oil" },
108 { 2, -12.8F, "degrees", "" },
109 { 0, 0.0F, "", "" },
110 { 3, 10.0F, "LBS", "fertilizer" },
111 { 3, 100.0F, "rgs", "energy" },
112 { -1, 0.0F, "", "" }};
33a934a3 113 size_t rounds = 0;
28f540f4
RM
114 float quant;
115 char units[21], item[21];
116 while (!feof (in) && !ferror (in))
117 {
118 int count;
3867ee64
RM
119
120 if (rounds++ >= sizeof (ok) / sizeof (ok[0]))
121 return 1;
122
28f540f4
RM
123 quant = 0.0;
124 units[0] = item[0] = '\0';
125 count = fscanf (in, "%f%20s of %20s", &quant, units, item);
126 (void) fscanf (in, "%*[^\n]");
127 fprintf (out, "count = %d, quant = %f, item = %.21s, units = %.21s\n",
128 count, quant, item, units);
3867ee64
RM
129 if (count != ok[rounds-1].count || quant != ok[rounds-1].quant
130 || strcmp (item, ok[rounds-1].item)
131 || strcmp (units, ok[rounds-1].units))
132 return 1;
28f540f4
RM
133 }
134 }
3867ee64 135 buf[0] = '\0';
28f540f4 136 fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
3867ee64
RM
137 if (strcmp (buf, ""))
138 return 1;
28f540f4
RM
139
140 if (out != stdout)
141 pclose (out);
142
143 exit(EXIT_SUCCESS);
144}