1 /*
2    Copyright (c) 2005 Perry Rapp
3    "The MIT license"
4    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7 */
8 /*=============================================================
9  * fileops.c -- Wrappers around some file operations, to exit if fail
10  *==============================================================*/
11 
12 
13 #include "llstdlib.h"
14 /* llstdlib.h pulls in standard.h, config.h, sys_inc.h */
15 
16 
17 
18 /*********************************************
19  * local function prototypes
20  *********************************************/
21 
22 static void report_fatal_fileop(STRING call, INT code, CNSTRING filename, STRING srcfile, int srcline);
23 
24 /*********************************************
25  * local & exported function definitions
26  * body of module
27  *********************************************/
28 
29 /*======================================================
30  * report_fatal_fileop -- Describe error and call __fatal to exit
31  *  filename may be null (only used for error message)
32  *====================================================*/
33 static void
report_fatal_fileop(STRING call,INT code,CNSTRING filename,STRING srcfile,int srcline)34 report_fatal_fileop (STRING call, INT code, CNSTRING filename, STRING srcfile, int srcline)
35 {
36 	char details[4096];
37 	if (!filename || !filename[0]) filename = "?";
38 	snprintf(details, sizeof(details), "%s failed code %ld, to file %s",
39 		call, code, filename);
40 	__fatal(srcfile, srcline, details); /* exits */
41 }
42 /*======================================================
43  * do_checked_fwrite -- fwrite, and report & exit if fails
44  *  filename may be null (only used for error message)
45  *====================================================*/
46 size_t
do_checked_fwrite(const void * buf,size_t size,size_t count,FILE * fp,STRING filename,STRING srcfile,int srcline)47 do_checked_fwrite (const void *buf, size_t size, size_t count, FILE *fp,
48 	STRING filename, STRING srcfile, int srcline)
49 {
50 	size_t rtn = fwrite(buf, size, count, fp);
51 	if (rtn != count) {
52 		report_fatal_fileop("fwrite", rtn, filename, srcfile, srcline); /* exits */
53 	}
54 	return rtn;
55 }
56 /*======================================================
57  * do_checked_fwrite -- fwrite, and report & exit if fails
58  *  filename may be null (only used for error message)
59  *====================================================*/
60 int
do_checked_fflush(FILE * fp,STRING filename,STRING srcfile,int srcline)61 do_checked_fflush (FILE *fp, STRING filename, STRING srcfile, int srcline)
62 {
63 	INT rtn = fflush(fp);
64 	if (rtn != 0) {
65 		report_fatal_fileop("fflush", rtn, filename, srcfile, srcline); /* exits */
66 	}
67 	return rtn;
68 }
69 /*======================================================
70  * do_checked_fclose -- fclose, and report & exit if fails
71  *  filename may be null (only used for error message)
72  *====================================================*/
73 int
do_checked_fclose(FILE * fp,STRING filename,STRING srcfile,int srcline)74 do_checked_fclose (FILE *fp, STRING filename, STRING srcfile, int srcline)
75 {
76 	INT rtn = fclose(fp);
77 	if (rtn != 0) {
78 		report_fatal_fileop("fclose", rtn, filename, srcfile, srcline); /* exits */
79 	}
80 	return rtn;
81 }
82 /*======================================================
83  * do_checked_fseek -- fseek, and report & exit if fails
84  *  filename may be null (only used for error message)
85  *====================================================*/
86 int
do_checked_fseek(FILE * fp,long offset,int whence,STRING filename,STRING srcfile,int srcline)87 do_checked_fseek (FILE *fp, long offset, int whence, STRING filename, STRING srcfile, int srcline)
88 {
89 	INT rtn = fseek(fp, offset, whence);
90 	if (rtn != 0) {
91 		report_fatal_fileop("fseek", rtn, filename, srcfile, srcline); /* exits */
92 	}
93 	return rtn;
94 }
95