home · contact · privacy
Test return values of _all_ *printf() calls.
[plomrogue] / src / common / readwrite.c
1 /* src/common/readwrite.c */
2
3 #include "readwrite.h"
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()
8                     */
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() */
14
15
16
17 extern FILE * try_fopen(char * path, char * mode, char * f)
18 {
19     char * f_name = "try_fopen()";
20     char * msg1 = "Trouble in ";
21     char * msg2 = " with fopen() (mode '";
22     char * msg3 = "') on path '";
23     char * msg4 = "'.";
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);
31     free(msg);
32     return file_p;
33 }
34
35
36
37 extern void try_fclose(FILE * file, char * f)
38 {
39     exit_trouble(fclose(file), f, "fclose()");
40 }
41
42
43
44 extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream,
45                        char * f)
46 {
47     exit_trouble(0 == fwrite(ptr, size, nmemb, stream), f, "fwrite()");
48 }
49
50
51
52 extern void try_fputc(uint8_t c, FILE * file, char * f)
53 {
54     exit_trouble(EOF == fputc(c, file), f, "fputc()");
55 }
56
57
58
59 extern int try_fgetc(FILE * file, char * f)
60 {
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()");
64     return test;
65 }
66
67
68
69 extern char * try_fgets(char * line, int linemax, FILE * file, char * f)
70 {
71     char * test = fgets(line, linemax, file);
72     exit_trouble(NULL == test && ferror(file), f, "fgets()");
73     return test;
74 }
75
76
77
78 extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
79                                      char * f)
80 {
81     char * f_name = "try_fclose_unlink_rename()";
82     try_fclose(file, f);
83     char * msg1 = "Trouble in ";
84     char * msg4 = "'.";
85     if (!access(p2, F_OK))
86     {
87         char * msg2 = " with unlink() on path '";
88         uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg4)
89                         + strlen(f) + strlen(p2) + 1;
90         char * msg = try_malloc(size, f_name);
91         int test = sprintf(msg, "%s%s%s%s%s", msg1, f, msg2, p2, msg4);
92         exit_trouble(test < 0, f_name, "sprintf()");
93         exit_err(unlink(p2), msg);
94         free(msg);
95     }
96     char * msg2 = " with rename() from '";
97     char * msg3 = "' to '";
98     uint16_t size = strlen(msg1) + strlen(f) + strlen(msg2) + strlen(p1)
99                     + strlen(msg3) + strlen(p2) + strlen(msg4) + 1;
100     char * msg = try_malloc(size, f_name);
101     int test = sprintf(msg, "%s%s%s%s%s%s%s", msg1,f,msg2,p1,msg3,p2,msg4);
102     exit_trouble(test < 0, f_name, "sprintf()");
103     exit_err(rename(p1, p2), msg);
104     free(msg);
105 }
106
107
108
109 extern uint32_t textfile_width(FILE * file)
110 {
111     char * f_name = "textfile_width()";
112     int c = 0;
113     uint32_t c_count = 0;
114     uint32_t linemax = 0;
115     while (1)
116     {
117         c = try_fgetc(file, f_name);
118         if (EOF == c)
119         {
120             break;
121         }
122         c_count++;
123         exit_trouble(UINT32_MAX == c_count, f_name, "too large text file line");
124         if ('\n' == c)
125         {
126             if (c_count > linemax)
127             {
128                 linemax = c_count;
129             }
130             c_count = 0;
131         }
132     }
133   if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
134   {                                /* line / lack newline chars.            */
135       linemax = c_count;
136    }
137    exit_trouble(-1 == fseek(file, 0, SEEK_SET), f_name, "fseek()");
138    return linemax;
139 }