lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Test for fdopen bugs. */ |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <unistd.h> |
| 5 | #include <fcntl.h> |
| 6 | |
| 7 | #undef assert |
| 8 | #define assert(x) \ |
| 9 | if (!(x)) \ |
| 10 | { \ |
| 11 | fputs ("test failed: " #x "\n", stderr); \ |
| 12 | retval = 1; \ |
| 13 | goto the_end; \ |
| 14 | } |
| 15 | |
| 16 | char buffer[256]; |
| 17 | |
| 18 | int |
| 19 | main (int argc, char *argv[]) |
| 20 | { |
| 21 | char *name; |
| 22 | FILE *fp = NULL; |
| 23 | int retval = 0; |
| 24 | int fd; |
| 25 | |
| 26 | name = tmpnam (NULL); |
| 27 | fp = fopen (name, "w"); |
| 28 | assert (fp != NULL) |
| 29 | fputs ("foobar and baz", fp); |
| 30 | fclose (fp); |
| 31 | fp = NULL; |
| 32 | |
| 33 | fd = open (name, O_RDONLY); |
| 34 | assert (fd != -1); |
| 35 | assert (lseek (fd, 5, SEEK_SET) == 5); |
| 36 | /* The file position indicator associated with the new stream is set to |
| 37 | the position indicated by the file offset associated with the file |
| 38 | descriptor. */ |
| 39 | fp = fdopen (fd, "r"); |
| 40 | assert (fp != NULL); |
| 41 | assert (getc (fp) == 'r'); |
| 42 | assert (getc (fp) == ' '); |
| 43 | |
| 44 | the_end: |
| 45 | if (fp != NULL) |
| 46 | fclose (fp); |
| 47 | unlink (name); |
| 48 | |
| 49 | return retval; |
| 50 | } |