4 #include <stdio.h> /* for FILE typedef, fopen(), fgetc(), fputc(), fseek(),
5 * sprintf(), fwrite(), ferror()
7 #include <stdint.h> /* for uint8_t, uint16_t, uint32_t */
8 #include <string.h> /* for strlen()*/
9 #include <unistd.h> /* for unlink() */
10 #include "rexit.h" /* for exit_err(), exit_trouble() */
11 #include "main.h" /* for world global */
15 /* Read/write "x" from/to "file" as bigendian representation of "size" bits. On
16 * failure, return 1, else 0. (As of of now, all extern read/write functions
17 * build on top of these.) Only use multiples of 8 greater or equal 32 for
18 * "size", so that storage inside uint32_t is possible.
20 static uint8_t read_uintX_bigendian(FILE * file, uint32_t * x, uint8_t size);
21 static uint8_t write_uintX_bigendian(FILE * file, uint32_t x, uint8_t size);
25 extern FILE * try_fopen(char * path, char * mode, char * f)
27 char * msg1 = "Trouble in ";
28 char * msg2 = " with fopen() (mode '";
29 char * msg3 = "') on path '";
31 uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg3) + strlen(msg4)
32 + strlen(f) + strlen(path) + strlen(mode) + 1;
34 sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, mode, msg3, path, msg4);
35 FILE * file_p = fopen(path, mode);
36 exit_err(NULL == file_p, msg);
42 extern void try_fclose(FILE * file, char * f)
44 exit_trouble(fclose(file), f, "fclose()");
49 extern char * try_fgets(char * line, int linemax, FILE * file, char * f)
51 char * test = fgets(line, linemax, file);
52 exit_trouble(NULL == test && ferror(file), f, "fgets()");
58 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
61 exit_trouble(0 == fwrite(ptr, size, nmemb, stream), f, "fwrite()");
66 extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
70 char * msg1 = "Trouble in ";
72 if (!access(p2, F_OK))
74 char * msg2 = " with unlink() on path '";
75 uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg4)
76 + strlen(f) + strlen(p2) + 1;
78 sprintf(msg, "%s%s%s%s%s", msg1, f, msg2, p2, msg4);
79 exit_err(unlink(p2), msg);
81 char * msg2 = " with rename() from '";
82 char * msg3 = "' to '";
83 uint16_t size = strlen(msg1) + strlen(f) + strlen(msg2) + strlen(p1)
84 + strlen(msg3) + strlen(p2) + strlen(msg4) + 1;
86 sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, p1, msg3, p2, msg4);
87 exit_err(rename(p1, p2), msg);
92 extern uint16_t get_linemax(FILE * file, char * f)
95 exit_trouble(textfile_sizes(file, &linemax, NULL), f, "textfile_sizes()");
101 extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p,
102 uint16_t * n_lines_p)
105 uint16_t c_count = 0;
106 uint16_t n_lines = 0;
107 uint16_t linemax = 0;
118 if (c_count > linemax)
129 if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
130 { /* line / lack newline chars. */
134 if (-1 == fseek(file, 0, SEEK_SET))
138 * linemax_p = linemax;
141 * n_lines_p = n_lines;
148 static uint8_t read_uintX_bigendian(FILE * file, uint32_t * x, uint8_t size)
151 int16_t bitshift = size - 8;
153 for (; bitshift >= 0; bitshift = bitshift - 8)
160 * x = * x + ((uint32_t) test << bitshift);
167 static uint8_t write_uintX_bigendian(FILE * file, uint32_t x, uint8_t size)
169 int16_t bitshift = size - 8;
170 for (; bitshift >= 0; bitshift = bitshift - 8)
172 if (EOF == fputc((x >> bitshift) & 0xFF, file))
182 extern uint8_t read_uint8(FILE * file, uint8_t * x)
184 /* Since read_uintX_bigendian() works on -- and zeroes -- four bytes, direct
185 * work on values of fewer bytes would corrupt immediate neighbor values.
188 uint8_t err = read_uintX_bigendian(file, &y, 8);
195 extern uint8_t read_uint32_bigendian(FILE * file, uint32_t * x)
197 return read_uintX_bigendian(file, x, 32);
202 extern uint8_t write_uint8(uint8_t x, FILE * file)
204 return write_uintX_bigendian(file, x, 8);
209 extern uint8_t write_uint32_bigendian(uint32_t x, FILE * file)
211 return write_uintX_bigendian(file, x, 32);