bug-fopena+.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <fcntl.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. static int fd;
  6. static char *fname;
  7. static void prepare (void);
  8. #define PREPARE(argc, argv) prepare ()
  9. #define TEST_FUNCTION do_test ()
  10. static int do_test (void);
  11. #include "../test-skeleton.c"
  12. static void
  13. prepare (void)
  14. {
  15. fd = create_temp_file ("wrewind.", &fname);
  16. if (fd == -1)
  17. exit (3);
  18. }
  19. static int
  20. do_test (void)
  21. {
  22. char buf[100];
  23. FILE *fp;
  24. int result = 0;
  25. fp = fdopen (fd, "w");
  26. if (fp == NULL)
  27. {
  28. puts ("cannot create file");
  29. exit (1);
  30. }
  31. if (fputs ("one\n", fp) == EOF || fputs ("two\n", fp) == EOF)
  32. {
  33. puts ("cannot create filec content");
  34. exit (1);
  35. }
  36. fclose (fp);
  37. fp = fopen (fname, "a+");
  38. if (fp == NULL)
  39. {
  40. puts ("cannot fopen a+");
  41. exit (1);
  42. }
  43. if (fgets (buf, sizeof (buf), fp) == NULL)
  44. {
  45. puts ("cannot read after fopen a+");
  46. exit (1);
  47. }
  48. if (strcmp (buf, "one\n") != 0)
  49. {
  50. puts ("read after fopen a+ produced wrong result");
  51. result = 1;
  52. }
  53. fclose (fp);
  54. fd = open (fname, O_RDWR);
  55. if (fd == -1)
  56. {
  57. puts ("open failed");
  58. exit (1);
  59. }
  60. fp = fdopen (fd, "a+");
  61. if (fp == NULL)
  62. {
  63. puts ("fopen after open failed");
  64. exit (1);
  65. }
  66. if (fgets (buf, sizeof (buf), fp) == NULL)
  67. {
  68. puts ("cannot read after fdopen a+");
  69. exit (1);
  70. }
  71. if (strcmp (buf, "one\n") != 0)
  72. {
  73. puts ("read after fdopen a+ produced wrong result");
  74. result = 1;
  75. }
  76. fclose (fp);
  77. return result;
  78. }