]> git.ipfire.org Git - thirdparty/glibc.git/blob - libio/tst-fwrite-error.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / libio / tst-fwrite-error.c
1 /* Test of fwrite() function, adapted from gnulib-tests in grep.
2 Copyright (C) 2011-2019 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 <https://www.gnu.org/licenses/>. */
16
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 static int
24 do_test (void)
25 {
26 char tmpl[] = "/tmp/tst-fwrite-error.XXXXXX";
27 int fd = mkstemp (tmpl);
28 if (fd == -1)
29 {
30 printf ("mkstemp failed with errno %d\n", errno);
31 return 1;
32 }
33 FILE *fp = fdopen (fd, "w");
34 if (fp == NULL)
35 {
36 printf ("fdopen failed with errno %d\n", errno);
37 return 1;
38 }
39
40 char buf[] = "world";
41 setvbuf (fp, NULL, _IONBF, 0);
42 close (fd);
43 unlink (tmpl);
44 errno = 0;
45
46 int ret = fwrite (buf, 1, sizeof (buf), fp);
47 if (ret != 0)
48 {
49 printf ("fwrite returned %d\n", ret);
50 return 1;
51 }
52 if (errno != EBADF)
53 {
54 printf ("Errno is not EBADF: %d\n", errno);
55 return 1;
56 }
57 if (ferror (fp) == 0)
58 {
59 printf ("ferror not set\n");
60 return 1;
61 }
62
63 return 0;
64 }
65
66 #define TEST_FUNCTION do_test ()
67 #include "../test-skeleton.c"