xref: /haiku/src/tests/system/libroot/posix/tst-ungetwc1.c (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
1 /* Taken from the Li18nux base test suite.  */
2 
3 #define _XOPEN_SOURCE 500
4 #include <locale.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <wchar.h>
9 
10 
11 int
12 main(void)
13 {
14 	FILE *fp;
15 	const char *str = "abcdef";
16 	wint_t ret, wc, ungetone = 0x00E4; /* 0x00E4 means `a umlaut'. */
17 	char fname[] = "/tmp/tst-ungetwc1.out.XXXXXX";
18 	int fd;
19 	int result = 0;
20 
21 	puts("This program runs on de_DE.UTF-8 locale.");
22 	if (setlocale(LC_ALL, "de_DE.UTF-8") == NULL) {
23 		fprintf(stderr, "Err: Cannot run on the de_DE.UTF-8 locale");
24 		exit(EXIT_FAILURE);
25 	}
26 
27 	fd = mkstemp(fname);
28 	if (fd == -1) {
29 		printf("cannot open temp file: %m\n");
30 		exit(EXIT_FAILURE);
31 	}
32 
33 	/* Write some characters to `testfile'. */
34 	if ((fp = fdopen(fd, "w")) == NULL) {
35 		fprintf(stderr, "Cannot open 'testfile'.");
36 		exit(EXIT_FAILURE);
37 	}
38 	fputs(str, fp);
39 	fclose(fp);
40 
41 	/* Open `testfile'. */
42 	if ((fp = fopen(fname, "r")) == NULL) {
43 		fprintf(stderr, "Cannot open 'testfile'.");
44 		exit(EXIT_FAILURE);
45 	}
46 
47 	/* Unget a character. */
48 	ret = ungetwc(ungetone, fp);
49 	printf("Unget a character (0x%04x)\n", (unsigned int) ungetone);
50 	fflush(stdout);
51 	if (ret == WEOF) {
52 		puts("ungetwc() returns NULL.");
53 		exit(EXIT_SUCCESS);
54 	}
55 
56 	/* Reget a character. */
57 	wc = getwc(fp);
58 	printf("Reget a character (0x%04x)\n", (unsigned int) wc);
59 	fflush(stdout);
60 	if (wc == ungetone) {
61 		puts("The ungotten character is equal to the regotten character.");
62 		fflush(stdout);
63 	} else {
64 		puts("The ungotten character is not equal to the regotten character.");
65 		printf("ungotten one: %04x, regetone: %04x", ungetone, wc);
66 		fflush(stdout);
67 		result = 1;
68 	}
69 	fclose(fp);
70 
71 	unlink(fname);
72 
73 	return result;
74 }
75