1 /* src/common/readwrite.c
3 * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4 * or any later version. For details on its copyright, license, and warranties,
5 * see the file NOTICE in the root directory of the PlomRogue source package.
9 #include <stddef.h> /* size_t */
10 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT32_MAX */
11 #include <stdio.h> /* FILE, fseek(), sprintf(), fgets(), fgetc(), ferror(),
12 * fputc(), fwrite(), fclose(), fopen(), clearerr()
14 #include <stdlib.h> /* free() */
15 #include <string.h> /* strlen() */
16 #include <unistd.h> /* access(), unlink() */
17 #include "rexit.h" /* exit_err(), exit_trouble() */
18 #include "try_malloc.h" /* try_malloc() */
22 extern FILE * try_fopen(char * path, char * mode, const char * f)
24 char * msg1 = "Trouble in ";
25 char * msg2 = " with fopen (mode '";
26 char * msg3 = "') on path '";
28 uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg3) + strlen(msg4)
29 + strlen(f) + strlen(path) + strlen(mode) + 1;
30 char * msg = try_malloc(size, __func__);
31 int test = sprintf(msg, "%s%s%s%s%s%s%s", msg1,f,msg2,mode,msg3,path,msg4);
32 exit_trouble(test < 0, __func__, "sprintf");
33 FILE * file_p = fopen(path, mode);
34 exit_err(!file_p, msg);
41 extern void try_fclose(FILE * file, const char * f)
43 exit_trouble(fclose(file), f, "fclose");
48 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
51 exit_trouble(0 == fwrite(ptr, size, nmemb, stream), f, "fwrite");
56 extern void try_fputc(uint8_t c, FILE * file, const char * f)
58 exit_trouble(EOF == fputc(c, file), f, "fputc");
63 extern int try_fgetc(FILE * file, const char * f)
65 clearerr(file); /* OSX' (BSD?) fgetc() needs this to undo previous EOFs. */
66 int test = fgetc(file);
67 exit_trouble(EOF == test && ferror(file), f, "fgetc");
73 extern char * try_fgets(char * line, int linemax, FILE * file, const char * f)
75 char * test = fgets(line, linemax, file);
76 exit_trouble(!test && ferror(file), f, "fgets");
82 extern char * build_temp_path(char * path)
84 char * suffix_tmp = "_tmp";
85 uint16_t size = strlen(path) + strlen(suffix_tmp) + 1;
86 char * path_tmp = try_malloc(size, __func__);
87 int test = sprintf(path_tmp, "%s%s", path, suffix_tmp);
88 exit_trouble(test < 0, __func__, "sprintf");
94 extern FILE * atomic_write_start(char * path, char ** path_tmp)
96 *path_tmp = build_temp_path(path);
97 return try_fopen(*path_tmp, "w", __func__);
102 extern void atomic_write_finish(FILE * file, char * path, char * path_tmp)
104 try_fclose(file, __func__);
105 char * msg1 = "Trouble in ";
107 if (!access(path, F_OK))
109 char * msg2 = " with unlink on path '";
110 uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg4)
111 + strlen(__func__) + strlen(path) + 1;
112 char * msg = try_malloc(size, __func__);
113 int test = sprintf(msg, "%s%s%s%s%s", msg1, __func__, msg2, path, msg4);
114 exit_trouble(test < 0, __func__, "sprintf");
115 exit_err(unlink(path), msg);
118 char * msg2 = " with rename from '";
119 char * msg3 = "' to '";
120 uint16_t size = strlen(msg1) + strlen(__func__) + strlen(msg2) +
121 + strlen(path_tmp) + strlen(msg3) + strlen(path)
123 char * msg = try_malloc(size, __func__);
124 int test = sprintf(msg, "%s%s%s%s%s%s%s",
125 msg1, __func__, msg2, path_tmp, msg3, path, msg4);
126 exit_trouble(test < 0, __func__, "sprintf");
127 exit_err(rename(path_tmp, path), msg);
134 extern void detect_atomic_leftover(char * path)
136 char * path_tmp = build_temp_path(path);
137 char * part1 = "Found file '";
138 char * part2 = "' that may be a leftover from an aborted previous attempt "
140 char * part3 = "'. Aborting until the matter is solved by (re-)moving it.";
141 uint32_t size = strlen(part1) + strlen(path_tmp) + strlen(part2)
142 + strlen(path) + strlen(part3) + 1;
143 char * msg = try_malloc(size, __func__);
144 int test = sprintf(msg, "%s%s%s%s%s", part1, path_tmp, part2, path, part3);
145 exit_trouble(test < 0, __func__, "sprintf");
146 exit_err(!access(path_tmp, F_OK), msg);
153 extern uint32_t textfile_width(FILE * file)
156 uint32_t c_count = 0;
157 uint32_t linemax = 0;
160 c = try_fgetc(file, __func__);
166 exit_trouble(UINT32_MAX==c_count, __func__, "too large text file line");
169 if (c_count > linemax)
176 if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
177 { /* line / lack newline chars. */
180 exit_trouble(-1 == fseek(file, 0, SEEK_SET), __func__, "fseek");