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