lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Test for fdopen bugs. */ |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <unistd.h> |
| 6 | #include <fcntl.h> |
| 7 | |
| 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 | int |
| 17 | main (int argc, char *argv[]) |
| 18 | { |
| 19 | char name[256]; |
| 20 | FILE *fp = NULL; |
| 21 | int retval = 0; |
| 22 | int fd; |
| 23 | |
| 24 | /* hack to get a tempfile name w/out using tmpname() |
| 25 | * as that func causes a link time warning */ |
| 26 | sprintf(name, "%s-uClibc-test.XXXXXX", __FILE__); |
| 27 | fd = mkstemp(name); |
| 28 | close(fd); |
| 29 | |
| 30 | fp = fopen (name, "w"); |
| 31 | assert (fp != NULL) |
| 32 | assert (fputs ("foobar and baz", fp) > 0); |
| 33 | assert (fclose (fp) == 0); |
| 34 | fp = NULL; |
| 35 | |
| 36 | fd = open (name, O_RDWR|O_CREAT, 0660); |
| 37 | assert (fd != -1); |
| 38 | assert (lseek (fd, 5, SEEK_SET) == 5); |
| 39 | |
| 40 | fp = fdopen (fd, "a"); |
| 41 | assert (fp != NULL); |
| 42 | /* SuSv3 says that doing a fdopen() does not reset the file position, |
| 43 | * thus the '5' here is correct, not '14'. */ |
| 44 | assert (ftell (fp) == 5); |
| 45 | |
| 46 | the_end: |
| 47 | if (fp != NULL) |
| 48 | assert (fclose (fp) == 0); |
| 49 | unlink (name); |
| 50 | |
| 51 | return retval; |
| 52 | } |