Update.
[glibc.git] / libio / tst_wprintf2.c
blobbe0f29f53fc6ca517861d7ffaa6de8ba7cfce421
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>
12 int
13 main (int argc, char *argv[])
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;
23 fd = mkstemp (name);
24 if (fd == -1)
25 error (EXIT_FAILURE, errno, "cannot open temporary file");
27 unlink (name);
29 setlocale (LC_ALL, "");
31 fp = fdopen (dup (fd), "w");
32 if (fp == NULL)
33 error (EXIT_FAILURE, errno, "fdopen(,\"w\")");
35 fwprintf (fp, L"test start");
36 fwprintf (fp, L" int %d\n", a);
38 /* String with precision. */
39 fwprintf (fp, L"1[%6.3s]\n", argv[1]);
41 fclose (fp);
43 fp = fdopen (dup (fd), "a");
44 if (fp == NULL)
45 error (EXIT_FAILURE, errno, "fdopen(,\"a\")");
47 setvbuf (fp, NULL, _IONBF, 0);
49 /* fwprintf to unbuffered stream. */
50 fwprintf (fp, L"hello.\n");
52 fclose (fp);
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\")");
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
68 puts ("newline missing after first line");
69 res = 1;
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;
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
82 puts ("newline missing after second line");
83 res = 1;
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;
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
96 puts ("newline missing after third line");
97 res = 1;
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;
103 return res;