-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsys-or-exit.c
182 lines (155 loc) · 3.21 KB
/
sys-or-exit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* System call with exit on failure. */
/* These <system_call>_or_exit functions all have the same type
arguments in the same order as <system_call>, with no extra
arguments. They are not documented here since they are almost
identical to the system call. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
/* For "unlink". */
#include <unistd.h>
#include "error-msg.h"
#include "sys-or-exit.h"
static int n_mallocs;
void *
malloc_or_exit (size_t s)
{
void * v;
v = malloc (s);
if (! v) {
error ("out of memory");
}
n_mallocs++;
return v;
}
void *
calloc_or_exit (size_t s, size_t size)
{
void * v;
v = calloc (s, size);
if (! v) {
error ("out of memory");
}
n_mallocs++;
return v;
}
void *
realloc_or_exit (void * v, size_t s)
{
if (! v) {
n_mallocs++;
}
v = realloc (v, s);
if (! v) {
error ("out of memory");
}
return v;
}
void
fseek_or_exit (FILE * file, long offset, int whence)
{
if (fseek (file, offset, whence) == -1) {
error ("file seek failed: %s", strerror (errno));
}
}
void
fread_or_exit (void * buffer, size_t size, size_t nmemb, FILE * stream)
{
if (fread (buffer, size, nmemb, stream) != nmemb) {
if (ferror (stream)) {
error ("file read failed: %s", strerror (errno));
}
else if (feof (stream)) {
error ("file read failed: unexpected end of file");
}
else {
bug (HERE, "problems with fread, feof or ferror");
}
}
}
void
fwrite_or_exit (const void * buffer, size_t size, size_t nmemb, FILE * stream)
{
if (fwrite (buffer, size, nmemb, stream) != nmemb) {
error ("file write failed: %s", strerror (errno));
}
}
FILE *
fopen_or_exit (const char * path, const char * mode)
{
FILE * file;
file = fopen (path, mode);
if (! file) {
error ("could not open file \"%s\": %s",
path, strerror (errno));
}
return file;
}
void
stat_or_exit (const char * file_name, struct stat * buf)
{
if (stat (file_name, buf) == -1) {
error ("stat failed on %s: %s", file_name, strerror (errno));
}
}
void
fclose_or_exit (FILE * f)
{
int status;
status = fclose (f);
if (status != 0) {
error ("fclose failed: %s", strerror (errno));
}
}
or_exit_t
free_or_exit (void * memory)
{
if (! memory) {
fprintf (stderr, "%s:%d: Attempt to free null pointer.\n",
__FILE__, __LINE__);
return or_exit_null_pointer;
}
free (memory);
n_mallocs--;
return or_exit_ok;
}
char *
strdup_or_exit (const char * todup)
{
char * dupped;
dupped = strdup (todup);
if (! dupped) {
error ("strdup failed: %s", strerror (errno));
}
n_mallocs++;
return dupped;
}
void
chmod_or_exit (const char * path, mode_t mode)
{
int status;
status = chmod (path, mode);
if (status) {
error ("Could not chmod (%s, %o): %s", path, mode, strerror (errno));
}
}
void
unlink_or_exit (const char * path)
{
int status;
status = unlink (path);
if (status) {
error ("Could not unlink (%s): %s", path, strerror (errno));
}
}
/* Check for memory leaks. */
void
memory_check ()
{
if (n_mallocs != 0) {
fprintf (stderr, "%s:%d: n_mallocs = %d\n",
__FILE__, __LINE__, n_mallocs);
}
}