Move nss directory into sysdeps Subdirs.
[glibc.git] / libio / tst-mmap-offend.c
blob19732e6201844d184835b85b9e7bab3184d91bde
1 /* Test case for bug with mmap stdio read past end of file. */
3 #include <stdio.h>
4 #include <error.h>
5 #include <errno.h>
7 static void do_prepare (void);
8 #define PREPARE(argc, argv) do_prepare ()
9 static int do_test (void);
10 #define TEST_FUNCTION do_test ()
11 #include <test-skeleton.c>
13 static char *temp_file;
15 static const char text1[] = "hello\n";
17 static void
18 do_prepare (void)
20 int temp_fd = create_temp_file ("tst-mmap-offend.", &temp_file);
21 if (temp_fd == -1)
22 error (1, errno, "cannot create temporary file");
23 else
25 ssize_t cc = write (temp_fd, text1, sizeof text1 - 1);
26 if (cc != sizeof text1 - 1)
27 error (1, errno, "cannot write to temporary file");
29 close (temp_fd);
32 static int
33 do_test (void)
35 unsigned char buffer[8192];
36 int result = 0;
37 FILE *f = fopen (temp_file, "rm");
38 size_t cc;
40 if (f == NULL)
42 perror (temp_file);
43 return 1;
46 cc = fread (buffer, 1, sizeof (buffer), f);
47 printf ("fread %zu: \"%.*s\"\n", cc, (int) cc, buffer);
48 if (cc != sizeof text1 - 1)
50 perror ("fread");
51 result = 1;
54 if (fseek (f, 2048, SEEK_SET) != 0)
56 perror ("fseek off end");
57 result = 1;
60 if (fread (buffer, 1, sizeof (buffer), f) != 0
61 || ferror (f) || !feof (f))
63 printf ("after fread error %d eof %d\n",
64 ferror (f), feof (f));
65 result = 1;
68 printf ("ftell %ld\n", ftell (f));
70 if (fseek (f, 0, SEEK_SET) != 0)
72 perror ("fseek rewind");
73 result = 1;
76 cc = fread (buffer, 1, sizeof (buffer), f);
77 printf ("fread after rewind %zu: \"%.*s\"\n", cc, (int) cc, buffer);
78 if (cc != sizeof text1 - 1)
80 perror ("fread after rewind");
81 result = 1;
84 fclose (f);
85 return result;