tst-fwrite-error.c 1.7 KB

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