patches from bug-gnu-utils to support more architectures
[glibc.git] / stdio-common / tstscanf.c
blob59a47a3d80361422e7837df2f1ef655813db0f67
1 /* Copyright (C) 1991, 1992, 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <ansidecl.h>
20 #ifdef BSD
21 #include </usr/include/stdio.h>
22 #else
23 #include <stdio.h>
24 #endif
25 #include <stdlib.h>
26 #include <string.h>
29 int
30 DEFUN(main, (argc, argv), int argc AND char **argv)
32 char buf[BUFSIZ];
33 FILE *in = stdin, *out = stdout;
34 int x;
36 if (sscanf ("0", "%d", &x) != 1)
37 exit (EXIT_FAILURE);
39 sscanf ("conversion] Zero flag Ze]ro#\n", "%*[^]] %[^#]\n", buf);
40 if (strcmp (buf, "] Zero flag Ze]ro") != 0)
42 fputs ("test failed!\n", stderr);
43 return 1;
46 if (argc == 2 && !strcmp (argv[1], "-opipe"))
48 out = popen ("/bin/cat", "w");
49 if (out == NULL)
51 perror ("popen: /bin/cat");
52 exit (EXIT_FAILURE);
55 else if (argc == 3 && !strcmp (argv[1], "-ipipe"))
57 sprintf (buf, "/bin/cat %s", argv[2]);
58 in = popen (buf, "r");
62 char name[50];
63 fprintf (out,
64 "sscanf (\"thompson\", \"%%s\", name) == %d, name == \"%s\"\n",
65 sscanf ("thompson", "%s", name),
66 name);
67 if (strcmp (name, "thompson") != 0)
68 return 1;
71 fputs ("Testing scanf (vfscanf)\n", out);
73 fputs ("Test 1:\n", out);
75 int n, i;
76 float x;
77 char name[50];
78 n = fscanf (in, "%d%f%s", &i, &x, name);
79 fprintf (out, "n = %d, i = %d, x = %f, name = \"%.50s\"\n",
80 n, i, x, name);
81 if (n != 3 || i != 25 || x != 5.432F || strcmp (name, "thompson"))
82 return 1;
84 fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
85 if (strcmp (buf, "\n"))
86 return 1;
87 fputs ("Test 2:\n", out);
89 int i;
90 float x;
91 char name[50];
92 (void) fscanf (in, "%2d%f%*d %[0123456789]", &i, &x, name);
93 fprintf (out, "i = %d, x = %f, name = \"%.50s\"\n", i, x, name);
94 if (i != 56 || x != 789.0F || strcmp(name, "56"))
95 return 1;
97 fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
98 if (strcmp (buf, "a72\n"))
99 return 1;
100 fputs ("Test 3:\n", out);
102 static struct {
103 int count;
104 float quant;
105 const char *units;
106 const char *item;
107 } ok[] = {
108 { 3, 2.0F, "quarts", "oil" },
109 { 2, -12.8F, "degrees", "" },
110 { 0, 0.0F, "", "" },
111 { 3, 10.0F, "LBS", "fertilizer" },
112 { 3, 100.0F, "rgs", "energy" },
113 { -1, 0.0F, "", "" }};
114 int rounds = 0;
115 float quant;
116 char units[21], item[21];
117 while (!feof (in) && !ferror (in))
119 int count;
121 if (rounds++ >= sizeof (ok) / sizeof (ok[0]))
122 return 1;
124 quant = 0.0;
125 units[0] = item[0] = '\0';
126 count = fscanf (in, "%f%20s of %20s", &quant, units, item);
127 (void) fscanf (in, "%*[^\n]");
128 fprintf (out, "count = %d, quant = %f, item = %.21s, units = %.21s\n",
129 count, quant, item, units);
130 if (count != ok[rounds-1].count || quant != ok[rounds-1].quant
131 || strcmp (item, ok[rounds-1].item)
132 || strcmp (units, ok[rounds-1].units))
133 return 1;
136 buf[0] = '\0';
137 fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
138 if (strcmp (buf, ""))
139 return 1;
141 if (out != stdout)
142 pclose (out);
144 exit(EXIT_SUCCESS);