-
Notifications
You must be signed in to change notification settings - Fork 1
/
history.c
269 lines (228 loc) · 4.42 KB
/
history.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
/* $Id$
*
* history handling routines
*
* By Shigeya Suzuki, January 1994
* Copyright(c)1993 Shigeya Suzuki
*/
/* These routines are slow, but:
* (1) does not required to call very frequently.
* (2) We can keep the history file only in text format.
* (3) Does not waste memory.
*/
#include <config.h>
#include <sys/types.h>
#ifdef HAVE_SYS_FILE_H
# include <sys/file.h>
#endif
#include <sysexits.h>
#include <stdio.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifndef HAVE_MEMCMP
# define bcmp memcmp
#endif
#include "memory.h"
#include "history.h"
#include "logging.h"
/* globals
*/
static char * filename = NULL;
static int file_fd = -1;
static FILE *file = NULL;
void
openhistory(name, mode)
char *name;
char *mode;
{
FILE *fp;
int fd;
if ((fp = fopen(name, mode)) == NULL) {
logandexit(EX_NOINPUT,
"Can't open history file: %s", name);
return;
}
fd = fileno(fp);
#ifdef HAVE_FLOCK
flock(fd, LOCK_EX); /* lock the file */
#else
# ifdef HAVE_LOCKF
lockf(fd, F_LOCK, 0); /* lock the file */
# endif
#endif
filename = strsave(name);
file = fp;
file_fd = fd;
}
void
closehistory()
{
if (file != NULL) {
#ifdef HAVE_FLOCK
flock(file_fd, LOCK_UN);
#else
# ifdef HAVE_LOCKF
lockf(file_fd, F_ULOCK, 0);
# endif
#endif
fclose(file);
file = NULL;
file_fd = -1;
free(filename);
filename = NULL;
}
}
char*
nexttoken(pp)
char **pp;
{
char *p = *pp;
char *head = p;
while (*p && *p != ' ' && *p != '\t')
p++;
if (*p) {
*p++ = '\0';
*pp = p;
return head;
}
else {
*pp = p;
return head;
}
}
void
chop(p)
char* p;
{
int len = strlen(p);
if (len > 0)
p[len-1] = '\0';
}
int
parsehistoryentry(h, buf)
struct history *h;
char *buf;
{
char *p = buf, *v;
h->h_articlenum = -1;
h->h_messageid = NULL;
h->h_bodysum = -1;
h->h_subject = NULL;
v = nexttoken(&p);
if (v == 0)
return 0;
h->h_articlenum = atoi(v);
v = nexttoken(&p);
if (v == 0)
return 0;
h->h_bodysum = atoi(v);
v = nexttoken(&p);
if (v == 0)
return 0;
h->h_messageid = strsave(v);
if (p == NULL)
return 0;
h->h_subject = strsave(p);
chop(h->h_subject);
return 1;
}
struct history*
findhistory(article)
int article;
{
char buf[HISTORYBUFLEN];
char numbuf[20];
struct history h;
int numbuflen;
sprintf(numbuf, ARTICLENUMFORMAT, article);
numbuflen = strlen(numbuf);
rewind(file);
/* on compare, we assume first field is article number and fixed width.
*/
while (fgets(buf, sizeof(buf), file) != NULL)
if (!memcmp(buf, numbuf, numbuflen) && parsehistoryentry(&h, buf))
return (struct history*) memsave((char*)&h, sizeof(h));
return (struct history*)NULL;
}
void
appendhistory(num, sum, id, subj)
int num;
int sum;
char *id;
char *subj;
{
char *subjbuf;
char *p;
if (file == NULL) {
programerror();
}
fseek(file, 0L, SEEK_END);
subjbuf = strsave(subj);
for (p = subjbuf; *p; p++) /* need to remove LF conver to space */
if (*p == '\n')
*p = ' ';
fprintf(file, HISTORYFORMAT, num, sum, id, subj);
}
void
freehistory(h)
struct history* h;
{
if (h->h_messageid != NULL)
free(h->h_messageid);
if (h->h_subject != NULL)
free(h->h_subject);
free((char*)h);
}
/* fput and make check sum
*/
int
fputs_sum(buf, fp, sump)
char *buf;
FILE *fp;
int *sump;
{
unsigned char *p;
int sum = *sump;
int r = fputs(buf, fp);
for (p = (unsigned char*)buf; *p; p++)
sum = (sum + *p) & CHECKSUMMASK;
*sump = sum;
return r;
}
#ifdef TEST
main()
{
int i;
char buf[128];
char sbuf[128];
struct history * h;
logging_setprinterror(1);
openhistory("./histtest.dat", "a+");
printf("Writing..\n");
for (i=1; i<=1000; i++) {
sprintf(buf, "<AA%07d.%[email protected]>", i, i);
sprintf(sbuf, "(B-Class %d) Test Test Test", i);
appendhistory(i, 10000+i, buf, sbuf);
}
printf("Reading..\n");
for (i=1000; i>0; i--) {
h = findhistory(i);
if (h != NULL) {
printf("\t%d, %d, %s, %s\n", h->h_articlenum, h->h_bodysum,
h->h_messageid, h->h_subject);
if (h->h_articlenum != i || h->h_bodysum != i+10000) {
printf("find: %d BAD\n", i);
}
else {
printf("find: %d OK\n", i);
}
freehistory(h);
}
else {
printf("Can't find %d\n", i);
}
}
closehistory();
}
#endif