master xplshn/aruu / shared / libutil / fshut.c
 1/* See LICENSE file for copyright and license details. */
 2#include <stdio.h>
 3#include <stdlib.h>
 4
 5#include "../util.h"
 6
 7int
 8fshut(FILE *fp, const char *fname)
 9{
10	int ret = 0;
11
12	/* fflush() is undefined for input streams by ISO C,
13	 * but not POSIX 2008 if you ignore ISO C overrides.
14	 * Leave it unchecked and rely on the following
15	 * functions to detect errors.
16	 */
17	fflush(fp);
18
19	if (ferror(fp) && !ret) {
20		weprintf("ferror %s:", fname);
21		ret = 1;
22	}
23
24	if (fclose(fp) && !ret) {
25		weprintf("fclose %s:", fname);
26		ret = 1;
27	}
28
29	return ret;
30}
31
32void
33enfshut(int status, FILE *fp, const char *fname)
34{
35	if (fshut(fp, fname))
36		exit(status);
37}
38
39void
40efshut(FILE *fp, const char *fname)
41{
42	enfshut(1, fp, fname);
43}