posix: Add invalid flags test for p{write,read}v2
[glibc.git] / libio / bug-ungetwc1.c
blob56a3d336ae89d69f383b1e5f8a9906295606cb5c
1 #define _XOPEN_SOURCE 500
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <locale.h>
5 #include <wchar.h>
7 const char write_chars[] = "ABC"; /* Characters on testfile. */
8 const wint_t unget_wchar = L'A'; /* Ungotten wide character. */
10 char *fname;
13 static int do_test (void);
14 #define TEST_FUNCTION do_test ()
16 #include "../test-skeleton.c"
19 static int
20 do_test (void)
22 wint_t wc;
23 FILE *fp;
24 int fd;
26 fname = (char *) malloc (strlen (test_dir) + sizeof "/bug-ungetwc1.XXXXXX");
27 if (fname == NULL)
29 puts ("no memory");
30 return 1;
32 strcpy (stpcpy (fname, test_dir), "/bug-ungetwc1.XXXXXX");
33 fd = mkstemp (fname);
34 if (fd == -1)
36 printf ("cannot open temporary file: %m\n");
37 return 1;
39 add_temp_file (fname);
41 setlocale(LC_ALL, "");
43 /* Output to the file. */
44 if ((fp = fdopen (fd, "w")) == NULL)
46 fprintf (stderr, "Cannot make `%s' file\n", fname);
47 exit (EXIT_FAILURE);
50 fprintf (fp, "%s", write_chars);
51 fclose (fp);
53 /* Read from the file. */
54 fp = fopen (fname, "r");
56 size_t i = 0;
57 while (!feof (fp))
59 wc = getwc (fp);
60 if (i >= sizeof (write_chars))
62 printf ("Did not get end-of-file when expected.\n");
63 return 1;
65 else if (wc != (write_chars[i] ? write_chars[i] : WEOF))
67 printf ("Unexpected %lu from getwc.\n", (unsigned long int) wc);
68 return 1;
70 i++;
72 printf ("\nThe end-of-file indicator is set.\n");
74 /* Unget a wide character. */
75 ungetwc (unget_wchar, fp);
76 printf ("< `%lc' is ungotten.\n", unget_wchar);
78 /* Check the end-of-file indicator. */
79 if (feof (fp))
81 printf ("The end-of-file indicator is still set.\n");
82 return 1;
84 else
85 printf ("The end-of-file flag is cleared.\n");
87 fflush (stdout);
88 fclose (fp);
90 return 0;