]>
Commit | Line | Data |
---|---|---|
b39d5719 UD |
1 | /* Test for fdopen bugs. */ |
2 | ||
3 | #include <stdio.h> | |
5c112f1b | 4 | #include <stdlib.h> |
b39d5719 UD |
5 | #include <unistd.h> |
6 | #include <fcntl.h> | |
7 | ||
0bc02a40 | 8 | #undef assert |
b39d5719 UD |
9 | #define assert(x) \ |
10 | if (!(x)) \ | |
11 | { \ | |
12 | fputs ("test failed: " #x "\n", stderr); \ | |
13 | retval = 1; \ | |
14 | goto the_end; \ | |
15 | } | |
16 | ||
17 | char buffer[256]; | |
18 | ||
19 | int | |
20 | main (int argc, char *argv[]) | |
21 | { | |
5c112f1b | 22 | char name[] = "/tmp/tst-fdopen.XXXXXX"; |
b39d5719 UD |
23 | FILE *fp = NULL; |
24 | int retval = 0; | |
8430ab40 | 25 | int fd; |
b39d5719 | 26 | |
5c112f1b JM |
27 | fd = mkstemp (name); |
28 | if (fd == -1) | |
29 | { | |
30 | printf ("mkstemp failed: %m\n"); | |
31 | return 1; | |
32 | } | |
33 | close (fd); | |
b39d5719 UD |
34 | fp = fopen (name, "w"); |
35 | assert (fp != NULL) | |
36 | fputs ("foobar and baz", fp); | |
37 | fclose (fp); | |
38 | fp = NULL; | |
39 | ||
40 | fd = open (name, O_RDONLY); | |
41 | assert (fd != -1); | |
42 | assert (lseek (fd, 5, SEEK_SET) == 5); | |
43 | /* The file position indicator associated with the new stream is set to | |
44 | the position indicated by the file offset associated with the file | |
45 | descriptor. */ | |
46 | fp = fdopen (fd, "r"); | |
47 | assert (fp != NULL); | |
48 | assert (getc (fp) == 'r'); | |
49 | assert (getc (fp) == ' '); | |
50 | ||
51 | the_end: | |
52 | if (fp != NULL) | |
53 | fclose (fp); | |
54 | unlink (name); | |
55 | ||
56 | return retval; | |
57 | } |