lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <stdarg.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <wchar.h> |
| 6 | |
| 7 | #define FAIL() \ |
| 8 | do { \ |
| 9 | result = 1; \ |
| 10 | printf ("test at line %d failed\n", __LINE__); \ |
| 11 | } while (0) |
| 12 | |
| 13 | static int |
| 14 | xsscanf (const char *str, const char *fmt, ...) |
| 15 | { |
| 16 | va_list ap; |
| 17 | va_start (ap, fmt); |
| 18 | int ret = vsscanf (str, fmt, ap); |
| 19 | va_end (ap); |
| 20 | return ret; |
| 21 | } |
| 22 | |
| 23 | static int |
| 24 | xscanf (const char *fmt, ...) |
| 25 | { |
| 26 | va_list ap; |
| 27 | va_start (ap, fmt); |
| 28 | int ret = vscanf (fmt, ap); |
| 29 | va_end (ap); |
| 30 | return ret; |
| 31 | } |
| 32 | |
| 33 | static int |
| 34 | xfscanf (FILE *f, const char *fmt, ...) |
| 35 | { |
| 36 | va_list ap; |
| 37 | va_start (ap, fmt); |
| 38 | int ret = vfscanf (f, fmt, ap); |
| 39 | va_end (ap); |
| 40 | return ret; |
| 41 | } |
| 42 | |
| 43 | int |
| 44 | main (void) |
| 45 | { |
| 46 | wchar_t *lsp; |
| 47 | char *sp; |
| 48 | float f; |
| 49 | double d; |
| 50 | char c[8]; |
| 51 | int result = 0; |
| 52 | |
| 53 | if (xsscanf (" 0.25s x", "%e%3c", &f, c) != 2) |
| 54 | FAIL (); |
| 55 | else if (f != 0.25 || memcmp (c, "s x", 3) != 0) |
| 56 | FAIL (); |
| 57 | if (xsscanf (" 1.25s x", "%as%2c", &sp, c) != 2) |
| 58 | FAIL (); |
| 59 | else |
| 60 | { |
| 61 | if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0) |
| 62 | FAIL (); |
| 63 | memset (sp, 'x', sizeof "1.25s"); |
| 64 | free (sp); |
| 65 | } |
| 66 | if (xsscanf (" 2.25s x", "%las%2c", &d, c) != 2) |
| 67 | FAIL (); |
| 68 | else if (d != 2.25 || memcmp (c, " x", 2) != 0) |
| 69 | FAIL (); |
| 70 | if (xsscanf (" 3.25S x", "%4aS%3c", &lsp, c) != 2) |
| 71 | FAIL (); |
| 72 | else |
| 73 | { |
| 74 | if (wcscmp (lsp, L"3.25") != 0 || memcmp (c, "S x", 3) != 0) |
| 75 | FAIL (); |
| 76 | memset (lsp, 'x', sizeof L"3.25"); |
| 77 | free (lsp); |
| 78 | } |
| 79 | if (xsscanf ("4.25[0-9.] x", "%a[0-9.]%8c", &sp, c) != 2) |
| 80 | FAIL (); |
| 81 | else |
| 82 | { |
| 83 | if (strcmp (sp, "4.25") != 0 || memcmp (c, "[0-9.] x", 8) != 0) |
| 84 | FAIL (); |
| 85 | memset (sp, 'x', sizeof "4.25"); |
| 86 | free (sp); |
| 87 | } |
| 88 | if (xsscanf ("5.25[0-9.] x", "%la[0-9.]%2c", &d, c) != 2) |
| 89 | FAIL (); |
| 90 | else if (d != 5.25 || memcmp (c, " x", 2) != 0) |
| 91 | FAIL (); |
| 92 | |
| 93 | const char *tmpdir = getenv ("TMPDIR"); |
| 94 | if (tmpdir == NULL || tmpdir[0] == '\0') |
| 95 | tmpdir = "/tmp"; |
| 96 | |
| 97 | char fname[strlen (tmpdir) + sizeof "/tst-scanf16.XXXXXX"]; |
| 98 | sprintf (fname, "%s/tst-scanf16.XXXXXX", tmpdir); |
| 99 | if (fname == NULL) |
| 100 | FAIL (); |
| 101 | |
| 102 | /* Create a temporary file. */ |
| 103 | int fd = mkstemp (fname); |
| 104 | if (fd == -1) |
| 105 | FAIL (); |
| 106 | |
| 107 | FILE *fp = fdopen (fd, "w+"); |
| 108 | if (fp == NULL) |
| 109 | FAIL (); |
| 110 | else |
| 111 | { |
| 112 | if (fputs (" 1.25s x", fp) == EOF) |
| 113 | FAIL (); |
| 114 | if (fseek (fp, 0, SEEK_SET) != 0) |
| 115 | FAIL (); |
| 116 | if (xfscanf (fp, "%as%2c", &sp, c) != 2) |
| 117 | FAIL (); |
| 118 | else |
| 119 | { |
| 120 | if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0) |
| 121 | FAIL (); |
| 122 | memset (sp, 'x', sizeof "1.25s"); |
| 123 | free (sp); |
| 124 | } |
| 125 | |
| 126 | if (freopen (fname, "r", stdin) == NULL) |
| 127 | FAIL (); |
| 128 | else |
| 129 | { |
| 130 | if (xscanf ("%as%2c", &sp, c) != 2) |
| 131 | FAIL (); |
| 132 | else |
| 133 | { |
| 134 | if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0) |
| 135 | FAIL (); |
| 136 | memset (sp, 'x', sizeof "1.25s"); |
| 137 | free (sp); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | fclose (fp); |
| 142 | } |
| 143 | |
| 144 | remove (fname); |
| 145 | |
| 146 | return result; |
| 147 | } |