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