]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdio-common/tst-fmemopen.c
Fix manual build warnings.
[thirdparty/glibc.git] / stdio-common / tst-fmemopen.c
CommitLineData
de153e7f
UD
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>
10
de153e7f 11int
c98d4212 12main (int argc, char **argv)
de153e7f 13{
4579f81c 14 char *test_file;
de153e7f
UD
15 const char blah[] = "BLAH";
16 FILE *fp;
17 char *mmap_data;
18 int ch, fd;
19 struct stat fs;
20 const char *cp;
21
c98d4212
RM
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);
27
de153e7f 28 /* setup the physical file, and use it */
c98d4212 29 if ((fp = fopen (test_file, "w+")) == NULL)
de153e7f
UD
30 exit (1);
31 if (fwrite (blah, 1, strlen (blah), fp) != strlen (blah))
32 exit (2);
33
34 rewind (fp);
35 printf ("file: ");
36 cp = blah;
37 while ((ch = getc (fp)) != EOF)
38 {
39 fputc (ch, stdout);
40 if (ch != *cp)
41 {
59553897 42 printf ("\ncharacter %td: '%c' instead of '%c'\n",
de153e7f
UD
43 cp - blah, ch, *cp);
44 exit (1);
45 }
46 ++cp;
47 }
48 fputc ('\n', stdout);
49 if (ferror (fp))
50 {
51 puts ("fp: error");
52 exit (1);
53 }
54 if (feof (fp))
55 printf ("fp: EOF\n");
56 else
57 {
58 puts ("not EOF");
59 exit (1);
60 }
61 fclose (fp);
62
63 /* Now, mmap the file into a buffer, and do that too */
c98d4212 64 if ((fd = open (test_file, O_RDONLY)) == -1)
de153e7f
UD
65 exit (3);
66 if (fstat (fd, &fs) == -1)
67 exit (4);
68
69 if ((mmap_data = (char *) mmap (NULL, fs.st_size, PROT_READ,
7c11c4a1 70 MAP_SHARED, fd, 0)) == MAP_FAILED)
de153e7f
UD
71 {
72 if (errno == ENOSYS)
73 exit (0);
74 exit (5);
75 }
76
77 if ((fp = fmemopen (mmap_data, fs.st_size, "r")) == NULL)
78 exit (1);
79
80 printf ("mem: ");
81 cp = blah;
82 while ((ch = getc (fp)) != EOF)
83 {
84 fputc (ch, stdout);
85 if (ch != *cp)
86 {
59553897 87 printf ("%td character: '%c' instead of '%c'\n",
de153e7f
UD
88 cp - blah, ch, *cp);
89 exit (1);
90 }
91 ++cp;
92 }
93
94 fputc ('\n', stdout);
95
96 if (ferror (fp))
97 {
98 puts ("fp: error");
99 exit (1);
100 }
101 if (feof (fp))
102 printf ("fp: EOF\n");
103 else
104 {
105 puts ("not EOF");
106 exit (1);
107 }
108
109 fclose (fp);
110
111 munmap (mmap_data, fs.st_size);
112
c98d4212
RM
113 unlink (test_file);
114 free (test_file);
de153e7f
UD
115
116 return 0;
117}