.
[glibc.git] / stdio-common / scanf14.c
blob6ca5c7c5679632a34f19189b9fc61896a762149a
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <wchar.h>
6 #define FAIL() \
7 do { \
8 result = 1; \
9 printf ("test at line %d failed\n", __LINE__); \
10 } while (0)
12 int
13 main (void)
15 wchar_t *lsp;
16 char *sp;
17 float f;
18 double d;
19 char c[8];
20 int result = 0;
22 if (sscanf (" 0.25s x", "%e%3c", &f, c) != 2)
23 FAIL ();
24 else if (f != 0.25 || memcmp (c, "s x", 3) != 0)
25 FAIL ();
26 if (sscanf (" 1.25s x", "%as%2c", &sp, c) != 2)
27 FAIL ();
28 else
30 if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0)
31 FAIL ();
32 memset (sp, 'x', sizeof "1.25s");
33 free (sp);
35 if (sscanf (" 2.25s x", "%las%2c", &d, c) != 2)
36 FAIL ();
37 else if (d != 2.25 || memcmp (c, " x", 2) != 0)
38 FAIL ();
39 if (sscanf (" 3.25S x", "%4aS%3c", &lsp, c) != 2)
40 FAIL ();
41 else
43 if (wcscmp (lsp, L"3.25") != 0 || memcmp (c, "S x", 3) != 0)
44 FAIL ();
45 memset (lsp, 'x', sizeof L"3.25");
46 free (lsp);
48 if (sscanf ("4.25[0-9.] x", "%a[0-9.]%8c", &sp, c) != 2)
49 FAIL ();
50 else
52 if (strcmp (sp, "4.25") != 0 || memcmp (c, "[0-9.] x", 8) != 0)
53 FAIL ();
54 memset (sp, 'x', sizeof "4.25");
55 free (sp);
57 if (sscanf ("5.25[0-9.] x", "%la[0-9.]%2c", &d, c) != 2)
58 FAIL ();
59 else if (d != 5.25 || memcmp (c, " x", 2) != 0)
60 FAIL ();
62 const char *tmpdir = getenv ("TMPDIR");
63 if (tmpdir == NULL || tmpdir[0] == '\0')
64 tmpdir = "/tmp";
66 char fname[strlen (tmpdir) + sizeof "/tst-scanf14.XXXXXX"];
67 sprintf (fname, "%s/tst-scanf14.XXXXXX", tmpdir);
68 if (fname == NULL)
69 FAIL ();
71 /* Create a temporary file. */
72 int fd = mkstemp (fname);
73 if (fd == -1)
74 FAIL ();
76 FILE *fp = fdopen (fd, "w+");
77 if (fp == NULL)
78 FAIL ();
79 else
81 if (fputs (" 1.25s x", fp) == EOF)
82 FAIL ();
83 if (fseek (fp, 0, SEEK_SET) != 0)
84 FAIL ();
85 if (fscanf (fp, "%as%2c", &sp, c) != 2)
86 FAIL ();
87 else
89 if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0)
90 FAIL ();
91 memset (sp, 'x', sizeof "1.25s");
92 free (sp);
95 if (freopen (fname, "r", stdin) == NULL)
96 FAIL ();
97 else
99 if (scanf ("%as%2c", &sp, c) != 2)
100 FAIL ();
101 else
103 if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0)
104 FAIL ();
105 memset (sp, 'x', sizeof "1.25s");
106 free (sp);
110 fclose (fp);
113 remove (fname);
115 return result;