xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | #undef _GNU_SOURCE |
| 2 | #define _XOPEN_SOURCE 600 |
| 3 | #undef _LIBC |
| 4 | #undef _IO_MTSAFE_IO |
| 5 | /* The following macro definitions are a hack. They word around disabling |
| 6 | the GNU extension while still using a few internal headers. */ |
| 7 | #define u_char unsigned char |
| 8 | #define u_short unsigned short |
| 9 | #define u_int unsigned int |
| 10 | #define u_long unsigned long |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <wchar.h> |
| 15 | |
| 16 | #define FAIL() \ |
| 17 | do { \ |
| 18 | result = 1; \ |
| 19 | printf ("test at line %d failed\n", __LINE__); \ |
| 20 | } while (0) |
| 21 | |
| 22 | int |
| 23 | main (void) |
| 24 | { |
| 25 | float f; |
| 26 | double d; |
| 27 | char c[8]; |
| 28 | int result = 0; |
| 29 | |
| 30 | if (sscanf (" 0.25s x", "%e%3c", &f, c) != 2) |
| 31 | FAIL (); |
| 32 | else if (f != 0.25 || memcmp (c, "s x", 3) != 0) |
| 33 | FAIL (); |
| 34 | if (sscanf (" 1.25s x", "%as%2c", &f, c) != 2) |
| 35 | FAIL (); |
| 36 | else if (f != 1.25 || memcmp (c, " x", 2) != 0) |
| 37 | FAIL (); |
| 38 | if (sscanf (" 2.25s x", "%las%2c", &d, c) != 2) |
| 39 | FAIL (); |
| 40 | else if (d != 2.25 || memcmp (c, " x", 2) != 0) |
| 41 | FAIL (); |
| 42 | if (sscanf (" 3.25S x", "%4aS%2c", &f, c) != 2) |
| 43 | FAIL (); |
| 44 | else if (f != 3.25 || memcmp (c, " x", 2) != 0) |
| 45 | FAIL (); |
| 46 | if (sscanf (" 4.25[0-9.] x", "%a[0-9.]%2c", &f, c) != 2) |
| 47 | FAIL (); |
| 48 | else if (f != 4.25 || memcmp (c, " x", 2) != 0) |
| 49 | FAIL (); |
| 50 | if (sscanf (" 5.25[0-9.] x", "%la[0-9.]%2c", &d, c) != 2) |
| 51 | FAIL (); |
| 52 | else if (d != 5.25 || memcmp (c, " x", 2) != 0) |
| 53 | FAIL (); |
| 54 | |
| 55 | const char *tmpdir = getenv ("TMPDIR"); |
| 56 | if (tmpdir == NULL || tmpdir[0] == '\0') |
| 57 | tmpdir = "/tmp"; |
| 58 | |
| 59 | char fname[strlen (tmpdir) + sizeof "/tst-scanf15.XXXXXX"]; |
| 60 | sprintf (fname, "%s/tst-scanf15.XXXXXX", tmpdir); |
| 61 | if (fname == NULL) |
| 62 | FAIL (); |
| 63 | |
| 64 | /* Create a temporary file. */ |
| 65 | int fd = mkstemp (fname); |
| 66 | if (fd == -1) |
| 67 | FAIL (); |
| 68 | |
| 69 | FILE *fp = fdopen (fd, "w+"); |
| 70 | if (fp == NULL) |
| 71 | FAIL (); |
| 72 | else |
| 73 | { |
| 74 | if (fputs (" 1.25s x", fp) == EOF) |
| 75 | FAIL (); |
| 76 | if (fseek (fp, 0, SEEK_SET) != 0) |
| 77 | FAIL (); |
| 78 | if (fscanf (fp, "%as%2c", &f, c) != 2) |
| 79 | FAIL (); |
| 80 | else if (f != 1.25 || memcmp (c, " x", 2) != 0) |
| 81 | FAIL (); |
| 82 | |
| 83 | if (freopen (fname, "r", stdin) == NULL) |
| 84 | FAIL (); |
| 85 | else |
| 86 | { |
| 87 | if (scanf ("%as%2c", &f, c) != 2) |
| 88 | FAIL (); |
| 89 | else if (f != 1.25 || memcmp (c, " x", 2) != 0) |
| 90 | FAIL (); |
| 91 | } |
| 92 | |
| 93 | fclose (fp); |
| 94 | } |
| 95 | |
| 96 | remove (fname); |
| 97 | |
| 98 | return result; |
| 99 | } |