Quash warning in s_sincosl.
[platform/upstream/glibc.git] / stdio-common / tst-fmemopen.c
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
11 int
12 main (int argc, char **argv)
13 {
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;
21
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
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);
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         {
42           printf ("\ncharacter %td: '%c' instead of '%c'\n",
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 */
64   if ((fd = open (test_file, O_RDONLY)) == -1)
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,
70                                   MAP_SHARED, fd, 0)) == MAP_FAILED)
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         {
87           printf ("%td character: '%c' instead of '%c'\n",
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
113   unlink (test_file);
114   free (test_file);
115
116   return 0;
117 }