1 /* src/common/readwrite.h:
3 * Routines for reading and writing files.
9 #include <stdint.h> /* uint8_t, uint32_t */
10 #include <stdio.h> /* FILE */
14 /* Wrappers to fopen(), fclose(), fgets() and fwrite() from function called "f",
15 * calling exit_err() upon error with appropriate error messages.
17 extern FILE * try_fopen(char * path, char * mode, const char * f);
18 extern void try_fclose(FILE * file, const char * f);
19 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
21 extern void try_fputc(uint8_t c, FILE * file, const char * f);
23 /* Wrapper to calling fgetc() and fgets() from function "f". The return code is
24 * returned unless ferror() indicates an error (i.e. to signify an end of file,
25 * fgetc() may return an EOF and fgets() a NULL). try_fgetc() calls clearerr()
26 * on "file" before fgetc(), because some Unixes fgetc() remember old EOFs and
27 * only return those until explicitely cleared.
29 extern int try_fgetc(FILE * file, const char * f);
30 extern char * try_fgets(char * line, int size, FILE * file, const char * f);
32 /* Return "path" + suffix "_tmp". Value is malloc'd, must be freed externally.*/
33 extern char * build_temp_path(char * path);
35 /* Write to "path_tmp" "path" + "_tmp" and return a new file at that "path_tmp"
36 * open for writing. "path_tmp" is malloc()'d, must be freed externally.
38 extern FILE * atomic_write_start(char * path, char ** path_tmp);
40 /* Finish atomic writing started in atomic_write_start(). Wraps successive calls
41 * of fclose() on "file", then unlink() on file at path "path" if it exists,
42 * then rename() from "path_tmp" to "path", then free() on "path_tmp".
44 extern void atomic_write_finish(FILE * file, char * path, char * path_tmp);
46 /* Check for temp file leftover of atomic writing of "path", abort if found. */
47 extern void detect_atomic_leftover(char * path);
49 /* Return largest line length from "file" (including newline chars). */
50 extern uint32_t textfile_width(FILE * file);