|
Packit |
6c4009 |
/* Test of fwrite() function, adapted from gnulib-tests in grep.
|
|
Packit |
6c4009 |
Copyright (C) 2011-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
This program is free software; you can redistribute it and/or modify
|
|
Packit |
6c4009 |
it under the terms of the GNU General Public License as published by
|
|
Packit |
6c4009 |
the Free Software Foundation; either version 3, or (at your option)
|
|
Packit |
6c4009 |
any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
This program is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
6c4009 |
GNU General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU General Public License
|
|
Packit |
6c4009 |
along with this program; if not, see <http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <fcntl.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
do_test (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
char tmpl[] = "/tmp/tst-fwrite-error.XXXXXX";
|
|
Packit |
6c4009 |
int fd = mkstemp (tmpl);
|
|
Packit |
6c4009 |
if (fd == -1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("mkstemp failed with errno %d\n", errno);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
FILE *fp = fdopen (fd, "w");
|
|
Packit |
6c4009 |
if (fp == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("fdopen failed with errno %d\n", errno);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
char buf[] = "world";
|
|
Packit |
6c4009 |
setvbuf (fp, NULL, _IONBF, 0);
|
|
Packit |
6c4009 |
close (fd);
|
|
Packit |
6c4009 |
unlink (tmpl);
|
|
Packit |
6c4009 |
errno = 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int ret = fwrite (buf, 1, sizeof (buf), fp);
|
|
Packit |
6c4009 |
if (ret != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("fwrite returned %d\n", ret);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (errno != EBADF)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("Errno is not EBADF: %d\n", errno);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (ferror (fp) == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("ferror not set\n");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define TEST_FUNCTION do_test ()
|
|
Packit |
6c4009 |
#include "../test-skeleton.c"
|