fshut.c - sbase - suckless unix tools
 (HTM) git clone git://git.suckless.org/sbase
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       fshut.c (717B)
       ---
            1 /* See LICENSE file for copyright and license details. */
            2 #include <stdio.h>
            3 #include <stdlib.h>
            4 
            5 #include "../util.h"
            6 
            7 int
            8 fshut(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 
           32 void
           33 enfshut(int status, FILE *fp, const char *fname)
           34 {
           35         if (fshut(fp, fname))
           36                 exit(status);
           37 }
           38 
           39 void
           40 efshut(FILE *fp, const char *fname)
           41 {
           42         enfshut(1, fp, fname);
           43 }