fdopen.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* Test for fdopen bugs. */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #define assert(x) \
  7. if (!(x)) \
  8. { \
  9. fputs ("test failed: " #x "\n", stderr); \
  10. retval = 1; \
  11. goto the_end; \
  12. }
  13. int
  14. main (int argc, char *argv[])
  15. {
  16. char name[256];
  17. FILE *fp = NULL;
  18. int retval = 0;
  19. int fd;
  20. /* hack to get a tempfile name w/out using tmpname()
  21. * as that func causes a link time warning */
  22. sprintf(name, "%s-uClibc-test.XXXXXX", __FILE__);
  23. fd = mkstemp(name);
  24. close(fd);
  25. fp = fopen (name, "w");
  26. assert (fp != NULL)
  27. assert (fputs ("foobar and baz", fp) > 0);
  28. assert (fclose (fp) == 0);
  29. fp = NULL;
  30. fd = open (name, O_RDWR|O_CREAT, 0660);
  31. assert (fd != -1);
  32. assert (lseek (fd, 5, SEEK_SET) == 5);
  33. fp = fdopen (fd, "a");
  34. assert (fp != NULL);
  35. /* SuSv3 says that doing a fdopen() does not reset the file position,
  36. * thus the '5' here is correct, not '14'. */
  37. assert (ftell (fp) == 5);
  38. the_end:
  39. if (fp != NULL)
  40. assert (fclose (fp) == 0);
  41. unlink (name);
  42. return retval;
  43. }