1 | /* Test for fdopen bugs. */ |
---|---|
2 | |
3 | #include <stdio.h> |
4 | #include <stdlib.h> |
5 | #include <unistd.h> |
6 | #include <fcntl.h> |
7 | |
8 | #undef assert |
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 | { |
22 | char name[] = "/tmp/tst-fdopen.XXXXXX"; |
23 | FILE *fp = NULL; |
24 | int retval = 0; |
25 | int fd; |
26 | |
27 | fd = mkstemp (template: name); |
28 | if (fd == -1) |
29 | { |
30 | printf (format: "mkstemp failed: %m\n"); |
31 | return 1; |
32 | } |
33 | close (fd: fd); |
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 (file: 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: name); |
55 | |
56 | return retval; |
57 | } |
58 |