stdlib: Fix stdbit.h with -Wconversion for older gcc
[glibc.git] / libio / tst_wprintf2.c
blobde19ae37815a4336a91421687f9454f7f66a4f93
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>
11 int
12 main (int argc, char *argv[])
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;
22 fd = mkstemp (name);
23 if (fd == -1)
24 error (EXIT_FAILURE, errno, "cannot open temporary file");
26 unlink (name);
28 setlocale (LC_ALL, "en_US.UTF-8");
30 fp = fdopen (dup (fd), "w");
31 if (fp == NULL)
32 error (EXIT_FAILURE, errno, "fdopen(,\"w\")");
34 fwprintf (fp, L"test start");
35 fwprintf (fp, L" int %d\n", a);
37 /* String with precision. */
38 fwprintf (fp, L"1[%6.3s]\n", argv[1]);
40 fclose (fp);
42 fp = fdopen (dup (fd), "a");
43 if (fp == NULL)
44 error (EXIT_FAILURE, errno, "fdopen(,\"a\")");
46 setvbuf (fp, NULL, _IONBF, 0);
48 /* fwprintf to unbuffered stream. */
49 fwprintf (fp, L"hello.\n");
51 fclose (fp);
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\")");
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
67 puts ("newline missing after first line");
68 res = 1;
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;
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
81 puts ("newline missing after second line");
82 res = 1;
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;
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
95 puts ("newline missing after third line");
96 res = 1;
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;
102 return res;