-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFILES.C
More file actions
216 lines (165 loc) · 4.88 KB
/
FILES.C
File metadata and controls
216 lines (165 loc) · 4.88 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// File Routines v1.2, Programmed by Yarpen of Swirl
// Copyright (C) 1997, Maciej Sini’o
// Some tricks from id Software and their QuakeC compiler, thanx!
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <errno.h>
#include <types.h>
#include "shutup.h"
static void *F_SafeMalloc(long size, const char *file, unsigned line);
#define SMALLOC(s) F_SafeMalloc(s, __FILE__, __LINE__)
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
static void *F_SafeMalloc(long size, const char *file, unsigned line)
{
void *tmp;
tmp = malloc(size);
if (!tmp)
shutUp("Cannot allocate memory! I need %lu bytes! [%s - %d]\n", size,
file, line);
return tmp;
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
int SafeOpenW(char *fname)
{
int handle;
umask(0);
handle = open(fname, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
if (handle == -1)
shutUp("File %s opening error: %s!", strupr(fname), strerror(errno));
return handle;
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
int SafeOpenR(char *fname)
{
int handle;
handle = open(fname, O_RDONLY | O_BINARY);
if (handle == -1)
shutUp("File %s opening error: %s!", strupr(fname), strerror(errno));
return handle;
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
FILE *SafeFOpen(char *fname, const char *tryb)
{
FILE *temp;
temp = fopen(fname, tryb);
if (!temp)
shutUp("File %s opening error: %s!", strupr(fname), strerror(errno));
return temp;
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
void SafeRead(int handle, void *buf, udword ile)
{
if (read(handle, buf, ile) != ile)
shutUp("File reading error: %s!", strerror(errno));
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
void SafeWrite(int handle, void *buf, udword ile)
{
if (write(handle, buf, ile) != ile)
shutUp("File writing error: %s!", strerror(errno));
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
void SafeFRead(void *buf, udword ile, FILE *f)
{
if (fread(buf, ile, 1, f) != 1)
shutUp("File reading error at %d: %s!", ftell(f), strerror(errno));
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
void SafeFWrite(void *buf, udword ile, FILE *f)
{
if (fwrite(buf, ile, 1, f) != 1)
shutUp("File writing error at %d: %s!", ftell(f), strerror(errno));
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
udword fsize(FILE *f)
{
udword temp;
udword len;
temp = ftell(f);
fseek(f, 0, SEEK_END);
len = ftell(f);
fseek(f, temp, SEEK_SET);
return len;
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
udword SafeSize(int handle)
{
struct stat fileinfo;
if (fstat(handle, &fileinfo) == -1)
shutUp("Error fstating!");
return fileinfo.st_size;
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
void *SafeLoad(char *fname)
{
int handle;
udword len;
void *buf;
handle = SafeOpenR(fname);
len = SafeSize(handle);
buf = (void *)SMALLOC(len);
SafeRead(handle, buf, len);
close(handle);
return buf;
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
udword SafeLoadS(char *fname, void **bufptr)
{
int handle;
udword len;
void *buf;
handle = SafeOpenR(fname);
len = SafeSize(handle);
buf = (void *)SMALLOC(len + 1);
((char *)buf)[len] = 0;
SafeRead(handle, buf, len);
close(handle);
*bufptr = buf;
return len;
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
void *SafeFLoad(char *fname)
{
FILE *f;
udword len;
void *buf;
f = SafeFOpen(fname, "rb");
len = fsize(f);
buf = (void *)SMALLOC(len);
SafeFRead(buf, len, f);
fclose(f);
return buf;
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
udword SafeFLoadS(char *fname, void **bufptr)
{
FILE *f;
udword len;
void *buf;
f = SafeFOpen(fname, "rb");
len = fsize(f);
buf = (void *)SMALLOC(len + 1);
((char *)buf)[len] = 0;
SafeFRead(buf, len, f);
fclose(f);
*bufptr = buf;
return len;
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
void SafeSave(char *fname, void *buf, udword ile)
{
int handle;
handle = SafeOpenW(fname);
SafeWrite(handle, buf, ile);
close(handle);
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
void SafeFSave(char *fname, void *buf, udword ile)
{
FILE *f;
f = SafeFOpen(fname, "wb");
SafeFWrite(buf, ile, f);
fclose(f);
}