fdopen.c 856 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* Test for fdopen bugs. */
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #define assert(x) \
  6. if (!(x)) \
  7. { \
  8. fputs ("test failed: " #x "\n", stderr); \
  9. retval = 1; \
  10. goto the_end; \
  11. }
  12. char buffer[256];
  13. int
  14. main (int argc, char *argv[])
  15. {
  16. char *name;
  17. FILE *fp = NULL;
  18. int retval = 0;
  19. int fd;
  20. name = tmpnam (NULL);
  21. fp = fopen (name, "w");
  22. assert (fp != NULL)
  23. fputs ("foobar and baz", fp);
  24. fclose (fp);
  25. fp = NULL;
  26. fd = open (name, O_RDWR|O_CREAT);
  27. assert (fd != -1);
  28. assert (lseek (fd, 5, SEEK_SET) == 5);
  29. fp = fdopen (fd, "a");
  30. assert (fp != NULL);
  31. /* SuSv3 says that doing a fdopen() does not reset the file position,
  32. * thus the '5' here is correct, not '14'. */
  33. assert (ftell (fp) == 5);
  34. the_end:
  35. if (fp != NULL)
  36. fclose (fp);
  37. unlink (name);
  38. return retval;
  39. }