tst-eof.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <fcntl.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. static void do_prepare (void);
  6. #define PREPARE(argc, argv) do_prepare ()
  7. static int do_test (void);
  8. #define TEST_FUNCTION do_test ()
  9. #include <test-skeleton.c>
  10. int fd;
  11. static void
  12. do_prepare (void)
  13. {
  14. fd = create_temp_file ("tst-eof.", NULL);
  15. if (fd == -1)
  16. {
  17. printf ("cannot create temporary file: %m\n");
  18. exit (1);
  19. }
  20. }
  21. static int
  22. do_test (void)
  23. {
  24. char buf[40];
  25. FILE *fp;
  26. if (write (fd, "some string\n", 12) != 12)
  27. {
  28. printf ("cannot write temporary file: %m\n");
  29. return 1;
  30. }
  31. if (lseek (fd, 0, SEEK_SET) == (off_t) -1)
  32. {
  33. printf ("cannot reposition temporary file: %m\n");
  34. return 1;
  35. }
  36. fp = fdopen (fd, "r");
  37. if (fp == NULL)
  38. {
  39. printf ("cannot create stream: %m\n");
  40. return 1;
  41. }
  42. if (feof (fp))
  43. {
  44. puts ("EOF set after fdopen");
  45. return 1;
  46. }
  47. if (fread (buf, 1, 20, fp) != 12)
  48. {
  49. puts ("didn't read the correct number of bytes");
  50. return 1;
  51. }
  52. if (! feof (fp))
  53. {
  54. puts ("EOF not set after fread");
  55. return 1;
  56. }
  57. fclose (fp);
  58. return 0;
  59. }