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