"SfR Fresh" - the SfR Freeware/Shareware Archive 
Member "tulp-4.2.1/src/lp.c" of archive tulp-4.2.1.tar.gz:
As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting with prefixed line numbers.
Alternatively you can here view or download the uninterpreted source code file.
That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
1 /*
2 * TULP - Unix Mailing List manager
3 * (sub-set of FRECP's Bitnet Listserv tool)
4 *
5 * Copyright (C) 1991-2000 Kimmo Suominen, Christophe Wolfhugel
6 *
7 * Please read the files COPYRIGHT and AUTHORS for the extended
8 * copyrights refering to this file.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 1, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <dirent.h>
33 #include <sys/times.h>
34 #include "conf.h"
35 #include "ext.h"
36 #include "str.h"
37 #include <limits.h>
38 #ifdef FAKESYSLOG
39 # include "fakesyslog.h"
40 #else
41 # include <syslog.h>
42 #endif /* FAKESYSLOG */
43
44 RCSID("@(#)lp.c,v 1.7 2000/03/07 22:16:30 kim Exp")
45
46 extern char From[], To[];
47
48 struct ITEM {
49 struct ITEM *next;
50 char *value;
51 };
52
53 static struct ITEM *commentList = NULL, *commentPtr = NULL;
54 static struct ITEM *editorList = NULL, *editorPtr = NULL;
55 static struct ITEM *ownerList = NULL, *ownerPtr = NULL;
56 static struct ITEM *userList = NULL, *userPtr = NULL;
57
58 static char review[16], send[16], subscription[16];
59 static char lname[64], replyto[128], errorsto[128];
60
61 static char buf2[512];
62
63 int IsEditor(char *s)
64 {
65 struct ITEM *p;
66
67 for (p = editorList; p != NULL; p = p->next) {
68 if (strspacecmp(p->value, s) == 0)
69 return (1);
70 }
71 return (0);
72 }
73
74 int IsOwner(char *s)
75 {
76 struct ITEM *p;
77
78 for (p = ownerList; p != NULL; p = p->next) {
79 if (strspacecmp(p->value, s) == 0)
80 return (1);
81 }
82 return (0);
83 }
84
85 int IsUser(char *s)
86 {
87 struct ITEM *p;
88
89 for (p = userList; p != NULL; p = p->next) {
90 if (strspacecmp(p->value, s) == 0)
91 return (1);
92 }
93 return (0);
94 }
95
96 void RewindUserList()
97 {
98 userPtr = userList;
99 }
100
101 char *GetUser(char *s)
102 {
103 struct ITEM *p;
104
105 if (userPtr == NULL)
106 return (NULL);
107 p = userPtr;
108 userPtr = userPtr->next;
109 if (s != NULL)
110 strcpy(s, p->value);
111 return (p->value);
112 }
113
114 void RewindCommentList()
115 {
116 commentPtr = commentList;
117 }
118
119 char *GetComment(char *s)
120 {
121 struct ITEM *p;
122
123 if (commentPtr == NULL)
124 return (NULL);
125 p = commentPtr;
126 commentPtr = commentPtr->next;
127 if (s != NULL)
128 strcpy(s, p->value);
129 return (p->value);
130 }
131
132 void RewindEditorList()
133 {
134 editorPtr = editorList;
135 }
136
137 char *GetEditor(char *s)
138 {
139 struct ITEM *p;
140
141 if (editorPtr == NULL)
142 return (NULL);
143 p = editorPtr;
144 editorPtr = editorPtr->next;
145 if (s != NULL)
146 strcpy(s, p->value);
147 return (p->value);
148 }
149
150 void RewindOwnerList()
151 {
152 ownerPtr = ownerList;
153 }
154
155 char *GetOwner(char *s)
156 {
157 struct ITEM *p;
158
159 if (ownerPtr == NULL)
160 return (NULL);
161 p = ownerPtr;
162 ownerPtr = ownerPtr->next;
163 if (s != NULL)
164 strcpy(s, p->value);
165 return (p->value);
166 }
167
168 /*
169 * Returns 0 if new entry, 1 if updated.
170 */
171
172 int AddItem(struct ITEM **head, char *s)
173 {
174 struct ITEM *p, *q, *r;
175
176 q = NULL;
177 for (p = *head; p != NULL; p = p->next) {
178 if (p->value[0] == '#') {
179 q = p;
180 continue;
181 }
182 if (stradrcmp(s, p->value) == 0) {
183 p->value = (char *) realloc(p->value, strlen(s) + 1);
184 strcpy(p->value, s);
185 return (1);
186 }
187 if (stradrcmp(s, p->value) < 0) {
188 r = (struct ITEM *) malloc(sizeof(struct ITEM));
189 r->value = (char *) malloc(strlen(s) + 1);
190 if (q == NULL)
191 *head = r;
192 else
193 q->next = r;
194 r->next = p;
195 strcpy(r->value, s);
196 return (0);
197 }
198 q = p;
199 }
200 p = (struct ITEM *) malloc(sizeof(struct ITEM));
201 p->value = (char *) malloc(strlen(s) + 1);
202 p->next = NULL;
203 strcpy(p->value, s);
204 if (q != NULL)
205 q->next = p;
206 else
207 *head = p;
208 return (0);
209 }
210
211 /*
212 * Returns 0 if OK, -1 if key not found.
213 */
214
215 int DelItem(struct ITEM **head, char *s)
216 {
217 struct ITEM *p, *q;
218
219 q = NULL;
220 for (p = *head; p != NULL; p = p->next) {
221 if (strspacecmp(s, p->value) == 0) {
222 if (q == NULL)
223 *head = p->next;
224 else
225 q->next = p->next;
226 free(p->value);
227 free(p);
228 return (0);
229 }
230 q = p;
231 }
232 return (-1);
233 }
234
235 int DelUser(char *s)
236 {
237 return (DelItem(&userList, s));
238 }
239
240 int AddUser(char *s)
241 {
242 return (AddItem(&userList, s));
243 }
244
245 void AddEditor(char *s)
246 {
247 AddItem(&editorList, s);
248 }
249
250 void AddOwner(char *s)
251 {
252 AddItem(&ownerList, s);
253 }
254
255 void DelList(struct ITEM **head)
256 {
257 struct ITEM *p;
258
259 while (*head != NULL) {
260 p = *head;
261 *head = p->next;
262 free(p->value);
263 free(p);
264 }
265 *head = NULL;
266 }
267
268 #define DELIM "# =\r\n\t"
269
270 void parseComment(char *s)
271 {
272 char *kw;
273
274 kw = strtok(buf2, DELIM);
275 if (kw == NULL)
276 return;
277 if (strcasecmp(kw, "owner") == 0) {
278 kw = strtok(NULL, DELIM);
279 if (kw != NULL)
280 AddItem(&ownerList, kw);
281 return;
282 }
283 if (strcasecmp(kw, "editor") == 0) {
284 kw = strtok(NULL, DELIM);
285 if (kw != NULL)
286 AddItem(&editorList, kw);
287 return;
288 }
289 if (strcasecmp(kw, "review") == 0) {
290 kw = strtok(NULL, DELIM);
291 if (kw != NULL)
292 strcpy(review, kw);
293 return;
294 }
295 if (strcasecmp(kw, "send") == 0) {
296 kw = strtok(NULL, DELIM);
297 if (kw != NULL)
298 strcpy(send, kw);
299 return;
300 }
301 if (strcasecmp(kw, "subscription") == 0) {
302 kw = strtok(NULL, DELIM);
303 if (kw != NULL)
304 strcpy(subscription, kw);
305 return;
306 }
307 if (strcasecmp(kw, "reply-to") == 0) {
308 kw = strtok(NULL, DELIM);
309 if (kw != NULL)
310 strcpy(replyto, kw);
311 return;
312 }
313 if (strcasecmp(kw, "errors-to") == 0) {
314 kw = strtok(NULL, DELIM);
315 if (kw != NULL)
316 strcpy(errorsto, kw);
317 return;
318 }
319 }
320
321 /*
322 * Returns 0 on success, -1 is list.u file not found.
323 */
324
325 int ReadUserList(char *s)
326 {
327 FILE *f;
328
329 strcpy(review, "all");
330 strcpy(send, "all");
331 strcpy(subscription, "open");
332 strcpy(replyto, "list,respect");
333 errorsto[0] = 0;
334 sprintf(lname, "%s.u", s);
335 f = fopen(lname, "r");
336 if (f == NULL) {
337 syslog(LOG_ERR, "can't open %s", lname);
338 return (-1);
339 }
340 while (fgets(buf2, 256, f) != NULL) {
341 strtok(buf2, "\r\n");
342 if (buf2[0] == 0)
343 continue;
344 if (buf2[0] == '#') {
345 AddItem(&commentList, buf2);
346 parseComment(buf2);
347 continue;
348 }
349 AddUser(buf2);
350 }
351 fclose(f);
352 return (0);
353 }
354
355 void WriteUserList()
356 {
357 FILE *f;
358
359 f = fopen("tmpfile", "w");
360 RewindCommentList();
361 RewindUserList();
362 while (GetComment(buf2) != NULL)
363 fprintf(f, "%s\n", buf2);
364 while (GetUser(buf2) != NULL)
365 fprintf(f, "%s\n", buf2);
366 fclose(f);
367 rename("tmpfile", lname);
368 unlink("tmpfile");
369 }
370
371 void CloseUserList()
372 {
373 DelList(&userList);
374 DelList(&editorList);
375 DelList(&ownerList);
376 DelList(&commentList);
377 }
378
379 char *GetSubscription()
380 {
381 return (subscription);
382 }
383
384 char *GetReview()
385 {
386 return (review);
387 }
388
389 char *GetSend()
390 {
391 return (send);
392 }
393
394 char *GetReplyTo()
395 {
396 return (replyto);
397 }
398
399 char *GetErrorsTo()
400 {
401 return (errorsto);
402 }