hppa: Cleanup libm-test-ulps.
[glibc.git] / stdio-common / tst-fmemopen.c
blob206bfd74d23e7f1c91db4329aa76fe7cacbf334f
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <sys/mman.h>
8 #include <sys/stat.h>
9 #include <sys/types.h>
11 int
12 main (int argc, char **argv)
14 char *test_file;
15 const char blah[] = "BLAH";
16 FILE *fp;
17 char *mmap_data;
18 int ch, fd;
19 struct stat fs;
20 const char *cp;
22 /* Construct the test file name based on ARGV[0], which will be
23 an absolute file name in the build directory. Don't touch the
24 source directory, which might be read-only. */
25 if (argc != 1 || asprintf (&test_file, "%s.test", argv[0]) < 0)
26 exit (99);
28 /* setup the physical file, and use it */
29 if ((fp = fopen (test_file, "w+")) == NULL)
30 exit (1);
31 if (fwrite (blah, 1, strlen (blah), fp) != strlen (blah))
32 exit (2);
34 rewind (fp);
35 printf ("file: ");
36 cp = blah;
37 while ((ch = getc (fp)) != EOF)
39 fputc (ch, stdout);
40 if (ch != *cp)
42 printf ("\ncharacter %td: '%c' instead of '%c'\n",
43 cp - blah, ch, *cp);
44 exit (1);
46 ++cp;
48 fputc ('\n', stdout);
49 if (ferror (fp))
51 puts ("fp: error");
52 exit (1);
54 if (feof (fp))
55 printf ("fp: EOF\n");
56 else
58 puts ("not EOF");
59 exit (1);
61 fclose (fp);
63 /* Now, mmap the file into a buffer, and do that too */
64 if ((fd = open (test_file, O_RDONLY)) == -1)
65 exit (3);
66 if (fstat (fd, &fs) == -1)
67 exit (4);
69 if ((mmap_data = (char *) mmap (NULL, fs.st_size, PROT_READ,
70 MAP_SHARED, fd, 0)) == MAP_FAILED)
72 if (errno == ENOSYS)
73 exit (0);
74 exit (5);
77 if ((fp = fmemopen (mmap_data, fs.st_size, "r")) == NULL)
78 exit (1);
80 printf ("mem: ");
81 cp = blah;
82 while ((ch = getc (fp)) != EOF)
84 fputc (ch, stdout);
85 if (ch != *cp)
87 printf ("%td character: '%c' instead of '%c'\n",
88 cp - blah, ch, *cp);
89 exit (1);
91 ++cp;
94 fputc ('\n', stdout);
96 if (ferror (fp))
98 puts ("fp: error");
99 exit (1);
101 if (feof (fp))
102 printf ("fp: EOF\n");
103 else
105 puts ("not EOF");
106 exit (1);
109 fclose (fp);
111 munmap (mmap_data, fs.st_size);
113 unlink (test_file);
114 free (test_file);
116 return 0;