4 #include <stdlib.h> /* for size_t */
5 #include <stdio.h> /* for FILE typedef, fopen(), fgetc(), fputc(), fseek(),
6 * sprintf(), fwrite(), ferror()
8 #include <stdint.h> /* for uint8_t, uint16_t, uint32_t */
9 #include <string.h> /* for strlen() */
10 #include <unistd.h> /* for unlink() */
11 #include "rexit.h" /* for exit_err(), exit_trouble() */
15 extern FILE * try_fopen(char * path, char * mode, char * f)
17 char * msg1 = "Trouble in ";
18 char * msg2 = " with fopen() (mode '";
19 char * msg3 = "') on path '";
21 uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg3) + strlen(msg4)
22 + strlen(f) + strlen(path) + strlen(mode) + 1;
24 sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, mode, msg3, path, msg4);
25 FILE * file_p = fopen(path, mode);
26 exit_err(NULL == file_p, msg);
32 extern void try_fclose(FILE * file, char * f)
34 exit_trouble(fclose(file), f, "fclose()");
39 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
42 exit_trouble(0 == fwrite(ptr, size, nmemb, stream), f, "fwrite()");
47 extern void try_fputc(uint8_t c, FILE * file, char * f)
49 exit_trouble(EOF == fputc(c, file), f, "fputc()");
54 extern char * try_fgets(char * line, int linemax, FILE * file, char * f)
56 char * test = fgets(line, linemax, file);
57 exit_trouble(NULL == test && ferror(file), f, "fgets()");
63 extern int try_fgetc(FILE * file, char * f)
65 int test = fgetc(file);
66 exit_trouble(EOF == test && ferror(file), f, "fgetc()");
72 extern uint8_t try_fgetc_noeof(FILE * file, char * f)
74 char * f_name = "try_fgetc_noeof()";
75 int test = try_fgetc(file, f_name);
76 exit_trouble(EOF == test, f, "fgetc() unexpectedly hitting EOF");
77 return (uint8_t) test;
82 extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
86 char * msg1 = "Trouble in ";
88 if (!access(p2, F_OK))
90 char * msg2 = " with unlink() on path '";
91 uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg4)
92 + strlen(f) + strlen(p2) + 1;
94 sprintf(msg, "%s%s%s%s%s", msg1, f, msg2, p2, msg4);
95 exit_err(unlink(p2), msg);
97 char * msg2 = " with rename() from '";
98 char * msg3 = "' to '";
99 uint16_t size = strlen(msg1) + strlen(f) + strlen(msg2) + strlen(p1)
100 + strlen(msg3) + strlen(p2) + strlen(msg4) + 1;
102 sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, p1, msg3, p2, msg4);
103 exit_err(rename(p1, p2), msg);
108 extern uint16_t textfile_sizes(FILE * file, uint16_t * n_lines_p)
110 char * f_name = "textfile_sizes()";
112 uint16_t c_count = 0;
113 uint16_t n_lines = 0;
114 uint16_t linemax = 0;
117 c = try_fgetc(file, f_name);
125 if (c_count > linemax)
136 if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
137 { /* line / lack newline chars. */
140 exit_trouble(-1 == fseek(file, 0, SEEK_SET), f_name, "fseek()");
143 * n_lines_p = n_lines;
150 extern uint32_t read_uint32_bigendian(FILE * file)
152 char * f_name = "read_uint32_bigendian()";
154 x = (uint32_t) try_fgetc_noeof(file, f_name) << 24;
155 x = x + ( (uint32_t) try_fgetc_noeof(file, f_name) << 16 );
156 x = x + ( (uint32_t) try_fgetc_noeof(file, f_name) << 8 );
157 x = x + (uint32_t) try_fgetc_noeof(file, f_name);
163 extern void write_uint32_bigendian(uint32_t x, FILE * file)
165 char * f_name = "write_uint32_bigendian()";
166 try_fputc( x >> 24, file, f_name);
167 try_fputc( ( x >> 16 ) & 0xFF, file, f_name);
168 try_fputc( ( x >> 8 ) & 0xFF, file, f_name);
169 try_fputc( x & 0xFF, file, f_name);