tst-fwrite-error.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Test of fwrite() function, adapted from gnulib-tests in grep.
  2. Copyright (C) 2011-2026 Free Software Foundation, Inc.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <fcntl.h>
  16. #include <unistd.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. static int
  20. do_test (void)
  21. {
  22. char tmpl[] = "/tmp/tst-fwrite-error.XXXXXX";
  23. int fd = mkstemp (tmpl);
  24. if (fd == -1)
  25. {
  26. printf ("mkstemp failed with errno %d\n", errno);
  27. return 1;
  28. }
  29. FILE *fp = fdopen (fd, "w");
  30. if (fp == NULL)
  31. {
  32. printf ("fdopen failed with errno %d\n", errno);
  33. return 1;
  34. }
  35. char buf[] = "world";
  36. setvbuf (fp, NULL, _IONBF, 0);
  37. close (fd);
  38. unlink (tmpl);
  39. errno = 0;
  40. int ret = fwrite (buf, 1, sizeof (buf), fp);
  41. if (ret != 0)
  42. {
  43. printf ("fwrite returned %d\n", ret);
  44. return 1;
  45. }
  46. if (errno != EBADF)
  47. {
  48. printf ("Errno is not EBADF: %d\n", errno);
  49. return 1;
  50. }
  51. if (ferror (fp) == 0)
  52. {
  53. printf ("ferror not set\n");
  54. return 1;
  55. }
  56. return 0;
  57. }
  58. #define TEST_FUNCTION do_test ()
  59. #include "../test-skeleton.c"