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 /* Return "path" + suffix "_tmp". Value is malloc'd, must be free externally. */
18 static char * build_temp_path(char * path);
22 static char * build_temp_path(char * path)
24 char * f_name = "build_temp_path";
25 char * suffix_tmp = "_tmp";
26 uint16_t size = strlen(path) + strlen(suffix_tmp) + 1;
27 char * path_tmp = try_malloc(size, f_name);
28 int test = sprintf(path_tmp, "%s%s", path, suffix_tmp);
29 exit_trouble(test < 0, f_name, "sprintf()");
35 extern FILE * try_fopen(char * path, char * mode, char * f)
37 char * f_name = "try_fopen()";
38 char * msg1 = "Trouble in ";
39 char * msg2 = " with fopen() (mode '";
40 char * msg3 = "') on path '";
42 uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg3) + strlen(msg4)
43 + strlen(f) + strlen(path) + strlen(mode) + 1;
44 char * msg = try_malloc(size, f_name);
45 int test = sprintf(msg, "%s%s%s%s%s%s%s", msg1,f,msg2,mode,msg3,path,msg4);
46 exit_trouble(test < 0, f_name, "sprintf()");
47 FILE * file_p = fopen(path, mode);
48 exit_err(NULL == file_p, msg);
55 extern void try_fclose(FILE * file, char * f)
57 exit_trouble(fclose(file), f, "fclose()");
62 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
65 exit_trouble(0 == fwrite(ptr, size, nmemb, stream), f, "fwrite()");
70 extern void try_fputc(uint8_t c, FILE * file, char * f)
72 exit_trouble(EOF == fputc(c, file), f, "fputc()");
77 extern int try_fgetc(FILE * file, char * f)
79 clearerr(file); /* OSX' (BSD?) fgetc() needs this to undo previous EOFs. */
80 int test = fgetc(file);
81 exit_trouble(EOF == test && ferror(file), f, "fgetc()");
87 extern char * try_fgets(char * line, int linemax, FILE * file, char * f)
89 char * test = fgets(line, linemax, file);
90 exit_trouble(NULL == test && ferror(file), f, "fgets()");
96 extern FILE * atomic_write_start(char * path, char ** path_tmp)
98 char * f_name = "atomic_write_start()";
99 *path_tmp = build_temp_path(path);
100 return try_fopen(*path_tmp, "w", f_name);
105 extern void atomic_write_finish(FILE * file, char * path, char * path_tmp)
107 char * f_name = "atomic_write_finish()";
108 try_fclose(file, f_name);
109 char * msg1 = "Trouble in ";
111 if (!access(path, F_OK))
113 char * msg2 = " with unlink() on path '";
114 uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg4)
115 + strlen(f_name) + strlen(path) + 1;
116 char * msg = try_malloc(size, f_name);
117 int test = sprintf(msg, "%s%s%s%s%s", msg1, f_name, msg2, path, msg4);
118 exit_trouble(test < 0, f_name, "sprintf()");
119 exit_err(unlink(path), msg);
122 char * msg2 = " with rename() from '";
123 char * msg3 = "' to '";
124 uint16_t size = strlen(msg1) + strlen(f_name) + strlen(msg2) +
125 + strlen(path_tmp) + strlen(msg3) + strlen(path)
127 char * msg = try_malloc(size, f_name);
128 int test = sprintf(msg, "%s%s%s%s%s%s%s",
129 msg1, f_name, msg2, path_tmp, msg3, path, msg4);
130 exit_trouble(test < 0, f_name, "sprintf()");
131 exit_err(rename(path_tmp, path), msg);
138 extern void detect_atomic_leftover(char * path)
140 char * f_name = "detect_atomic_leftover()";
141 char * path_tmp = build_temp_path(path);
142 char * part1 = "Found file '";
143 char * part2 = "' that may be a leftover from an aborted previous attempt "
145 char * part3 = "'. Aborting until the matter is solved by (re-)moving it.";
146 uint32_t size = strlen(part1) + strlen(path_tmp) + strlen(part2)
147 + strlen(path) + strlen(part3) + 1;
148 char * msg = try_malloc(size, f_name);
149 int test = sprintf(msg, "%s%s%s%s%s", part1, path_tmp, part2, path, part3);
150 exit_trouble(test < 0, f_name, "sprintf()");
151 exit_err(!access(path_tmp, F_OK), msg);
158 extern uint32_t textfile_width(FILE * file)
160 char * f_name = "textfile_width()";
162 uint32_t c_count = 0;
163 uint32_t linemax = 0;
166 c = try_fgetc(file, f_name);
172 exit_trouble(UINT32_MAX == c_count, f_name, "too large text file line");
175 if (c_count > linemax)
182 if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
183 { /* line / lack newline chars. */
186 exit_trouble(-1 == fseek(file, 0, SEEK_SET), f_name, "fseek()");