| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Test case by Yoshito Kawada <KAWADA@jp.ibm.com>.  */ | 
|  | 2 | #include <errno.h> | 
|  | 3 | #include <error.h> | 
|  | 4 | #include <fcntl.h> | 
|  | 5 | #include <locale.h> | 
|  | 6 | #include <stdio.h> | 
|  | 7 | #include <stdlib.h> | 
|  | 8 | #include <string.h> | 
|  | 9 | #include <unistd.h> | 
|  | 10 | #include <wchar.h> | 
|  | 11 |  | 
|  | 12 | int | 
|  | 13 | main (int argc, char *argv[]) | 
|  | 14 | { | 
|  | 15 | int a = 3; | 
|  | 16 | int fd; | 
|  | 17 | char name[] = "/tmp/wprintf.out.XXXXXX"; | 
|  | 18 | FILE *fp; | 
|  | 19 | char buf[100]; | 
|  | 20 | size_t len; | 
|  | 21 | int res = 0; | 
|  | 22 |  | 
|  | 23 | fd = mkstemp (name); | 
|  | 24 | if (fd == -1) | 
|  | 25 | error (EXIT_FAILURE, errno, "cannot open temporary file"); | 
|  | 26 |  | 
|  | 27 | unlink (name); | 
|  | 28 |  | 
|  | 29 | setlocale (LC_ALL, "en_US.UTF-8"); | 
|  | 30 |  | 
|  | 31 | fp = fdopen (dup (fd), "w"); | 
|  | 32 | if (fp == NULL) | 
|  | 33 | error (EXIT_FAILURE, errno, "fdopen(,\"w\")"); | 
|  | 34 |  | 
|  | 35 | fwprintf (fp, L"test start"); | 
|  | 36 | fwprintf (fp, L" int %d\n", a); | 
|  | 37 |  | 
|  | 38 | /* String with precision.  */ | 
|  | 39 | fwprintf (fp, L"1[%6.3s]\n", argv[1]); | 
|  | 40 |  | 
|  | 41 | fclose (fp); | 
|  | 42 |  | 
|  | 43 | fp = fdopen (dup (fd), "a"); | 
|  | 44 | if (fp == NULL) | 
|  | 45 | error (EXIT_FAILURE, errno, "fdopen(,\"a\")"); | 
|  | 46 |  | 
|  | 47 | setvbuf (fp, NULL, _IONBF, 0); | 
|  | 48 |  | 
|  | 49 | /* fwprintf to unbuffered stream.   */ | 
|  | 50 | fwprintf (fp, L"hello.\n"); | 
|  | 51 |  | 
|  | 52 | fclose (fp); | 
|  | 53 |  | 
|  | 54 |  | 
|  | 55 | /* Now read it back in.  This time using multibyte functions.  */ | 
|  | 56 | lseek (fd, SEEK_SET, 0); | 
|  | 57 | fp = fdopen (fd, "r"); | 
|  | 58 | if (fp == NULL) | 
|  | 59 | error (EXIT_FAILURE, errno, "fdopen(,\"r\")"); | 
|  | 60 |  | 
|  | 61 | if (fgets (buf, sizeof buf, fp) != buf) | 
|  | 62 | error (EXIT_FAILURE, errno, "first fgets"); | 
|  | 63 | len = strlen (buf); | 
|  | 64 | if (buf[len - 1] == '\n') | 
|  | 65 | --len; | 
|  | 66 | else | 
|  | 67 | { | 
|  | 68 | puts ("newline missing after first line"); | 
|  | 69 | res = 1; | 
|  | 70 | } | 
|  | 71 | printf ("1st line: \"%.*s\" -> %s\n", (int) len, buf, | 
|  | 72 | strncmp (buf, "test start int 3", len) == 0 ? "OK" : "FAIL"); | 
|  | 73 | res |= strncmp (buf, "test start int 3", len) != 0; | 
|  | 74 |  | 
|  | 75 | if (fgets (buf, sizeof buf, fp) != buf) | 
|  | 76 | error (EXIT_FAILURE, errno, "second fgets"); | 
|  | 77 | len = strlen (buf); | 
|  | 78 | if (buf[len - 1] == '\n') | 
|  | 79 | --len; | 
|  | 80 | else | 
|  | 81 | { | 
|  | 82 | puts ("newline missing after second line"); | 
|  | 83 | res = 1; | 
|  | 84 | } | 
|  | 85 | printf ("2nd line: \"%.*s\" -> %s\n", (int) len, buf, | 
|  | 86 | strncmp (buf, "1[   Som]", len) == 0 ? "OK" : "FAIL"); | 
|  | 87 | res |= strncmp (buf, "1[   Som]", len) != 0; | 
|  | 88 |  | 
|  | 89 | if (fgets (buf, sizeof buf, fp) != buf) | 
|  | 90 | error (EXIT_FAILURE, errno, "third fgets"); | 
|  | 91 | len = strlen (buf); | 
|  | 92 | if (buf[len - 1] == '\n') | 
|  | 93 | --len; | 
|  | 94 | else | 
|  | 95 | { | 
|  | 96 | puts ("newline missing after third line"); | 
|  | 97 | res = 1; | 
|  | 98 | } | 
|  | 99 | printf ("3rd line: \"%.*s\" -> %s\n", (int) len, buf, | 
|  | 100 | strncmp (buf, "hello.", len) == 0 ? "OK" : "FAIL"); | 
|  | 101 | res |= strncmp (buf, "hello.", len) != 0; | 
|  | 102 |  | 
|  | 103 | return res; | 
|  | 104 | } |