1 /* src/common/readwrite.c */
4 #include <stddef.h> /* size_t */
5 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT32_MAX */
6 #include <stdio.h> /* FILE, fseek(), sprintf(), fgets(), fgetc(), ferror(),
7 * fputc(), fwrite(), fclose(), fopen(), clearerr()
9 #include <stdlib.h> /* free() */
10 #include <string.h> /* strlen() */
11 #include <unistd.h> /* access(), unlink() */
12 #include "rexit.h" /* exit_err(), exit_trouble() */
13 #include "try_malloc.h" /* try_malloc() */
17 extern FILE * try_fopen(char * path, char * mode, char * f)
19 char * f_name = "try_fopen()";
20 char * msg1 = "Trouble in ";
21 char * msg2 = " with fopen() (mode '";
22 char * msg3 = "') on path '";
24 uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg3) + strlen(msg4)
25 + strlen(f) + strlen(path) + strlen(mode) + 1;
26 char * msg = try_malloc(size, f_name);
27 int test = sprintf(msg, "%s%s%s%s%s%s%s", msg1,f,msg2,mode,msg3,path,msg4);
28 exit_trouble(test < 0, f_name, "sprintf()");
29 FILE * file_p = fopen(path, mode);
30 exit_err(NULL == file_p, msg);
37 extern void try_fclose(FILE * file, char * f)
39 exit_trouble(fclose(file), f, "fclose()");
44 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
47 exit_trouble(0 == fwrite(ptr, size, nmemb, stream), f, "fwrite()");
52 extern void try_fputc(uint8_t c, FILE * file, char * f)
54 exit_trouble(EOF == fputc(c, file), f, "fputc()");
59 extern int try_fgetc(FILE * file, char * f)
61 clearerr(file); /* OSX' (BSD?) fgetc() needs this to undo previous EOFs. */
62 int test = fgetc(file);
63 exit_trouble(EOF == test && ferror(file), f, "fgetc()");
69 extern char * try_fgets(char * line, int linemax, FILE * file, char * f)
71 char * test = fgets(line, linemax, file);
72 exit_trouble(NULL == test && ferror(file), f, "fgets()");
78 extern FILE * atomic_write_start(char * path, char ** path_tmp)
80 char * f_name = "atomic_write_start()";
81 char * suffix_tmp = "_tmp";
82 uint16_t size = strlen(path) + strlen(suffix_tmp) + 1;
83 *path_tmp = try_malloc(size, f_name);
84 int test = sprintf(*path_tmp, "%s%s", path, suffix_tmp);
85 exit_trouble(test < 0, f_name, "sprintf()");
86 return try_fopen(*path_tmp, "w", f_name);
91 extern void atomic_write_finish(FILE * file, char * path, char * path_tmp)
93 char * f_name = "atomic_write_finish()";
94 try_fclose(file, f_name);
95 char * msg1 = "Trouble in ";
97 if (!access(path, F_OK))
99 char * msg2 = " with unlink() on path '";
100 uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg4)
101 + strlen(f_name) + strlen(path) + 1;
102 char * msg = try_malloc(size, f_name);
103 int test = sprintf(msg, "%s%s%s%s%s", msg1, f_name, msg2, path, msg4);
104 exit_trouble(test < 0, f_name, "sprintf()");
105 exit_err(unlink(path), msg);
108 char * msg2 = " with rename() from '";
109 char * msg3 = "' to '";
110 uint16_t size = strlen(msg1) + strlen(f_name) + strlen(msg2) +
111 + strlen(path_tmp) + strlen(msg3) + strlen(path)
113 char * msg = try_malloc(size, f_name);
114 int test = sprintf(msg, "%s%s%s%s%s%s%s",
115 msg1, f_name, msg2, path_tmp, msg3, path, msg4);
116 exit_trouble(test < 0, f_name, "sprintf()");
117 exit_err(rename(path_tmp, path), msg);
124 extern uint32_t textfile_width(FILE * file)
126 char * f_name = "textfile_width()";
128 uint32_t c_count = 0;
129 uint32_t linemax = 0;
132 c = try_fgetc(file, f_name);
138 exit_trouble(UINT32_MAX == c_count, f_name, "too large text file line");
141 if (c_count > linemax)
148 if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
149 { /* line / lack newline chars. */
152 exit_trouble(-1 == fseek(file, 0, SEEK_SET), f_name, "fseek()");