-
Notifications
You must be signed in to change notification settings - Fork 8
/
record.c
345 lines (292 loc) · 6.48 KB
/
record.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
Eagles Bulletin Board System
Copyright (C) 1995, Ray Rocker, rocker@datasync.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "server.h"
#include <ctype.h>
#include <unistd.h>
#ifdef NO_FLOCK
# define LOCK(fp) lockf(fileno(fp), F_LOCK, 0)
# define UNLOCK(fp) lockf(fileno(fp), F_ULOCK, 0)
#else
# include <sys/file.h>
# define LOCK(fp) flock(fileno(fp), LOCK_EX)
# define UNLOCK(fp) flock(fileno(fp), LOCK_UN)
#endif
#define IS_COMMENT(s) (*s == '#' || isspace(*s))
#define BBS_MAX_RECORD 4096
_match_first(rec, fld)
char *rec;
char *fld;
{
register char c1, c2;
while (*fld) {
c1 = *fld++;
c2 = *rec++;
if (toupper(c1) != toupper(c2)) return S_OK;
}
return ((*rec == ' ' || *rec == ':') ? S_RECEXISTS : S_OK);
}
_match_full(rec, fld)
char *rec;
char *fld;
{
register char c1, c2;
while (*fld) {
c1 = *fld++;
c2 = *rec++;
if (toupper(c1) != toupper(c2)) return S_OK;
}
return ((*rec == '\n' || *rec == '\0') ? S_RECEXISTS : S_OK);
}
/*ARGSUSED*/
_change_name(newrec, oldrec, newname)
char *newrec;
char *oldrec;
char *newname;
{
sprintf(newrec, "%s\n", newname);
return S_OK;
}
char *
_append_quoted(rec, str)
char *rec;
char *str;
{
while (*str) {
if (*str == ':' || *str == '\\') *rec++ = '\\';
*rec++ = *str++;
}
*rec++ = ':';
*rec = '\0';
return rec;
}
char *
_extract_quoted(rec, str, len)
char *rec;
char *str;
int len;
{
len--; /* leave space for terminating null */
while (*rec != ':' && *rec != '\n' && *rec != '\0') {
if (*rec == '\\') rec++;
if (len) {
*str++ = *rec;
len--;
}
rec++;
}
*str = '\0';
if (*rec == ':') rec++;
return rec;
}
_record_add(fname, testf, targ, formatf, farg)
char *fname;
int (*testf)();
void *targ;
int (*formatf)();
void *farg;
{
FILE *fp;
char rec[BBS_MAX_RECORD];
int rc;
if ((fp = fopen(fname, "r+")) == NULL) {
if ((fp = fopen(fname, "w")) == NULL) {
return S_SYSERR;
}
}
LOCK(fp);
if (testf) {
while (fgets(rec, sizeof rec, fp)) {
if (IS_COMMENT(rec)) continue;
rc = testf(rec, targ);
if (rc != S_OK) {
UNLOCK(fp);
fclose(fp);
return rc;
}
}
}
rc = formatf(rec, farg);
if (rc != S_OK) {
UNLOCK(fp);
fclose(fp);
return rc;
}
fseek(fp, 0, SEEK_END);
fputs(rec, fp);
UNLOCK(fp);
fclose(fp);
return S_OK;
}
_do_record_delete(fname, testf, targ, single)
char *fname;
int (*testf)();
void *targ;
int single;
{
FILE *fp;
char rec[BBS_MAX_RECORD];
int rc, len;
unsigned delsz = 0, savesz = 0;
if ((fp = fopen(fname, "r+")) == NULL) {
return S_NOSUCHREC;
}
LOCK(fp);
while (fgets(rec, sizeof rec, fp)) {
len = strlen(rec);
if (IS_COMMENT(rec) || (single && delsz)) rc = S_OK;
else rc = testf(rec, targ);
if (rc == S_OK) {
savesz += len;
if (delsz) {
fseek(fp, -(delsz+len), SEEK_CUR);
fputs(rec, fp);
fseek(fp, delsz, SEEK_CUR);
}
}
else {
delsz += len;
}
}
if (savesz == 0) unlink(fname);
else if (delsz) ftruncate(fileno(fp), savesz);
UNLOCK(fp);
fclose(fp);
return (delsz ? S_OK : S_NOSUCHREC);
}
_record_delete(fname, testf, targ)
char *fname;
int (*testf)();
void *targ;
{
return (_do_record_delete(fname, testf, targ, 1));
}
_record_delete_many(fname, testf, targ)
char *fname;
int (*testf)();
void *targ;
{
return (_do_record_delete(fname, testf, targ, 0));
}
_record_find(fname, testf, targ, formatf, farg)
char *fname;
int (*testf)();
void *targ;
int (*formatf)();
void *farg;
{
FILE *fp;
char rec[BBS_MAX_RECORD];
int rc;
if ((fp = fopen(fname, "r")) == NULL) {
return S_NOSUCHREC;
}
while (fgets(rec, sizeof rec, fp)) {
if (IS_COMMENT(rec)) continue;
rc = testf(rec, targ);
if (rc != S_OK) {
if (formatf) rc = formatf(rec, farg);
else rc = S_OK;
fclose(fp);
return rc;
}
}
fclose(fp);
return S_NOSUCHREC;
}
_record_enumerate(fname, start, enumf, earg)
char *fname;
int start;
int (*enumf)();
void *earg;
{
FILE *fp;
char rec[BBS_MAX_RECORD];
int indx = 0;
if ((fp = fopen(fname, "r")) == NULL) {
return 0;
}
while (fgets(rec, sizeof rec, fp)) {
if (IS_COMMENT(rec)) continue;
if (indx >= start) {
if (enumf(indx, rec, earg) == ENUM_QUIT) break;
}
indx++;
}
fclose(fp);
return indx;
}
/* This is one UGLY function, but it's as neat as I can make it! */
_record_replace(fname, testf, targ, replf, rarg)
char *fname;
int (*testf)();
void *targ;
int (*replf)();
void *rarg;
{
FILE *fp;
char rec[BBS_MAX_RECORD];
char newrec[BBS_MAX_RECORD];
char scratch[BBS_MAX_RECORD];
int rc = S_OK, len, newlen, difflen, chunk;
int savesz = 0;
if ((fp = fopen(fname, "r+")) == NULL) {
return S_NOSUCHREC;
}
LOCK(fp);
while (rc == S_OK && fgets(rec, sizeof rec, fp)) {
len = strlen(rec);
if (IS_COMMENT(rec)) rc = S_OK;
else rc = testf(rec, targ);
if (rc == S_OK) savesz += len;
}
if (rc == S_OK) {
UNLOCK(fp);
fclose(fp);
return S_NOSUCHREC;
}
rc = replf(newrec, rec, rarg);
if (rc != S_OK) {
UNLOCK(fp);
fclose(fp);
return rc;
}
newlen = strlen(newrec);
difflen = len - newlen;
if (difflen) chunk = fread(rec, 1, sizeof rec, fp);
else chunk = 0;
fseek(fp, -(chunk+len), SEEK_CUR);
fputs(newrec, fp);
if (difflen) {
savesz += newlen;
while (chunk) {
len = chunk+difflen;
newlen = chunk;
memcpy(scratch, rec, chunk);
if (len > 0) {
fseek(fp, len, SEEK_CUR);
chunk = fread(rec, 1, sizeof rec, fp);
}
else chunk = len = 0;
fseek(fp, -(chunk+len), SEEK_CUR);
fwrite(scratch, 1, newlen, fp);
savesz += newlen;
}
ftruncate(fileno(fp), savesz);
}
UNLOCK(fp);
fclose(fp);
return S_OK;
}