]> git.ipfire.org Git - thirdparty/glibc.git/blob - libio/tst_wprintf2.c
Refer to C23 in place of C2X in glibc
[thirdparty/glibc.git] / libio / tst_wprintf2.c
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 (name);
23 if (fd == -1)
24 error (EXIT_FAILURE, errno, "cannot open temporary file");
25
26 unlink (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, "fdopen(,\"w\")");
33
34 fwprintf (fp, L"test start");
35 fwprintf (fp, L" int %d\n", a);
36
37 /* String with precision. */
38 fwprintf (fp, 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, "fdopen(,\"a\")");
45
46 setvbuf (fp, NULL, _IONBF, 0);
47
48 /* fwprintf to unbuffered stream. */
49 fwprintf (fp, L"hello.\n");
50
51 fclose (fp);
52
53
54 /* Now read it back in. This time using multibyte functions. */
55 lseek (fd, SEEK_SET, 0);
56 fp = fdopen (fd, "r");
57 if (fp == NULL)
58 error (EXIT_FAILURE, errno, "fdopen(,\"r\")");
59
60 if (fgets (buf, sizeof buf, fp) != buf)
61 error (EXIT_FAILURE, errno, "first fgets");
62 len = strlen (buf);
63 if (buf[len - 1] == '\n')
64 --len;
65 else
66 {
67 puts ("newline missing after first line");
68 res = 1;
69 }
70 printf ("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 (buf, sizeof buf, fp) != buf)
75 error (EXIT_FAILURE, errno, "second fgets");
76 len = strlen (buf);
77 if (buf[len - 1] == '\n')
78 --len;
79 else
80 {
81 puts ("newline missing after second line");
82 res = 1;
83 }
84 printf ("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 (buf, sizeof buf, fp) != buf)
89 error (EXIT_FAILURE, errno, "third fgets");
90 len = strlen (buf);
91 if (buf[len - 1] == '\n')
92 --len;
93 else
94 {
95 puts ("newline missing after third line");
96 res = 1;
97 }
98 printf ("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 }