fdopen.c 737 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. assert (ftell (fp) == 14);
  32. the_end:
  33. if (fp != NULL)
  34. fclose (fp);
  35. unlink (name);
  36. return retval;
  37. }