home · contact · privacy
To read_file_into_queue(), add queue size test.
[plomrogue] / src / common / readwrite.c
1 /* src/common/readwrite.c
2  *
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.
6  */
7
8 #include "readwrite.h"
9 #include <stddef.h> /* NULL, 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()
13                     */
14 #include <stdlib.h> /* free() */
15 #include <string.h> /* strlen(), memcpy() */
16 #include <unistd.h> /* access(), unlink() */
17 #include "rexit.h" /* exit_err(), exit_trouble() */
18 #include "try_malloc.h" /* try_malloc() */
19
20
21
22 extern FILE * try_fopen(char * path, char * mode, const char * f)
23 {
24     char * msg1 = "Trouble in ";
25     char * msg2 = " with fopen (mode '";
26     char * msg3 = "') on path '";
27     char * msg4 = "'.";
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);
35     free(msg);
36     return file_p;
37 }
38
39
40
41 extern void try_fclose(FILE * file, const char * f)
42 {
43     exit_trouble(fclose(file), f, "fclose");
44 }
45
46
47
48 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
49                        const char * f)
50 {
51     exit_trouble(0 == fwrite(ptr, size, nmemb, stream), f, "fwrite");
52 }
53
54
55
56 extern void try_fputc(uint8_t c, FILE * file, const char * f)
57 {
58     exit_trouble(EOF == fputc(c, file), f, "fputc");
59 }
60
61
62
63 extern int try_fgetc(FILE * file, const char * f)
64 {
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");
68     return test;
69 }
70
71
72
73 extern char * try_fgets(char * line, int linemax, FILE * file, const char * f)
74 {
75     char * test = fgets(line, linemax, file);
76     exit_trouble(!test && ferror(file), f, "fgets");
77     return test;
78 }
79
80
81
82 extern char * build_temp_path(char * path)
83 {
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");
89     return path_tmp;
90 }
91
92
93
94 extern FILE * atomic_write_start(char * path, char ** path_tmp)
95 {
96     *path_tmp = build_temp_path(path);
97     return try_fopen(*path_tmp, "w", __func__);
98 }
99
100
101
102 extern void atomic_write_finish(FILE * file, char * path, char * path_tmp)
103 {
104     try_fclose(file, __func__);
105     char * msg1 = "Trouble in ";
106     char * msg4 = "'.";
107     if (!access(path, F_OK))
108     {
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);
116         free(msg);
117     }
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)
122                     + strlen(msg4) + 1;
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);
128     free(msg);
129     free(path_tmp);
130 }
131
132
133
134 extern void detect_atomic_leftover(char * path)
135 {
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 "
139                    "to write '";
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);
147     free(msg);
148     free(path_tmp);
149 }
150
151
152
153 extern uint32_t textfile_width(FILE * file)
154 {
155     int c = 0;
156     uint32_t c_count = 0;
157     uint32_t linemax = 0;
158     while (1)
159     {
160         c = try_fgetc(file, __func__);
161         if (EOF == c)
162         {
163             break;
164         }
165         c_count++;
166         exit_trouble(UINT32_MAX==c_count, __func__, "too large text file line");
167         if ('\n' == c)
168         {
169             if (c_count > linemax)
170             {
171                 linemax = c_count;
172             }
173             c_count = 0;
174         }
175     }
176   if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
177   {                                /* line / lack newline chars.            */
178       linemax = c_count;
179    }
180    exit_trouble(-1 == fseek(file, 0, SEEK_SET), __func__, "fseek");
181    return linemax;
182 }
183
184
185
186 extern uint8_t read_file_into_queue(FILE * file, char ** queue,
187                                     uint32_t * queue_size)
188 {
189     int test = try_fgetc(file, __func__);
190     if (EOF != test)
191     {
192         char * err_size = "Queue growing too large.";
193         do
194         {
195             char c = (char) test;
196             if ('\n' == c)
197             {
198                 c = '\0';
199             }
200             char * new_queue = try_malloc(*queue_size + 1, __func__);
201             memcpy(new_queue, *queue, *queue_size);
202             char * new_pos = new_queue + *queue_size;
203             * new_pos = c;
204             exit_err(*queue_size == UINT32_MAX, err_size);
205             *queue_size = *queue_size + 1;
206             free(*queue);
207             *queue = new_queue;
208         }
209         while (EOF != (test = try_fgetc(file, __func__)));
210         if (*queue_size && '\0' != (*queue)[*queue_size - 1])
211         {
212             char * new_queue = try_malloc(*queue_size + 1, __func__);
213             memcpy(new_queue, *queue, *queue_size);
214             new_queue[*queue_size] = '\0';
215             exit_err(*queue_size == UINT32_MAX, err_size);
216             *queue_size = *queue_size + 1;
217             free(*queue);
218             *queue = new_queue;
219         }
220         return 1;
221     }
222     return 0;
223 }
224
225
226
227 extern char * get_message_from_queue(char ** queue, uint32_t * queue_size)
228 {
229     char * message = NULL;
230     if (*queue_size)
231     {
232         size_t cutout_len = strlen(*queue);
233         uint8_t is_nullbyte_chunk = !cutout_len;
234         if (0 < cutout_len)
235         {
236             cutout_len++;
237             message = try_malloc(cutout_len, __func__);
238             memcpy(message, *queue, cutout_len);
239         }
240         for (;
241              cutout_len != *queue_size && '\0' == (*queue)[cutout_len];
242              cutout_len++);
243         *queue_size = *queue_size - cutout_len;
244         if (0 == *queue_size)
245         {
246             free(*queue);            /* NULL so read_file_into_queue() and    */
247             *queue = NULL;           /* cleanup() may free() this every time, */
248         }                            /* even when it's un-allocated.          */
249         else
250         {
251             char * new_queue = try_malloc(*queue_size, __func__);
252             memcpy(new_queue, &((*queue)[cutout_len]), *queue_size);
253             free(*queue);
254             *queue = new_queue;
255             if (is_nullbyte_chunk)
256             {
257                 return get_message_from_queue(queue, queue_size);
258             }
259         }
260     }
261     return message;
262 }