/* Test program for gnulib's close_stdout, regarding discussion
in https://lists.gnu.org/archive/html/bug-gnulib/2019-05/msg00039.html
Copyright (C) 2019 Assaf Gordon
Placed under Public Domain.
Compile with:
gcc -o aa aa.c
Test with:
$ ./aa stdout fclose && echo ok
Hello
ok
$ ./aa stdout none >/dev/full && echo ok
ok
$ ./aa stdout fclose >/dev/full && echo ok
aa: fclose failed: No space left on device
*/
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
FILE* f;
int i;
if (argc!=3)
errx(1,"usage: %s [stdout|stderr] [fclose|fflush|fsync|none]",
argv[0]);
if (strncmp(argv[1],"stdout",6)==0)
f = stdout;
else if (strncmp(argv[1],"stderr",6)==0)
f = stderr;
else
errx(1,"invalid 1st param (%s): must be 'stdout' or 'stderr'",
argv[1]);
i = fprintf(f,"Hello\n");
if (i<0)
err(1,"fprintf failed");
i = ferror(f);
if (i)
errx(1,"ferror reports ERROR state");
if (strncmp(argv[2],"fclose",6)==0) {
i = fclose(f);
if (i)
err(1,"fclose failed");
} else if (strncmp(argv[2],"fflush",6)==0) {
i = fflush(f);
if (i)
err(1,"fflush failed");
} else if (strncmp(argv[2],"fsync",5)==0) {
i = fsync(fileno(f));
if (i)
err(1,"fsync failed");
} else if (strncmp(argv[2],"none",5)!=0) {
errx(1,"invalid 2st param (%s): must be 'fclose' or " \
"'fflush' or 'fsync' or 'none'", argv[2]);
}
return 0;
}