|
|
1.1 root 1: #include <sys/types.h>
2: #include <dir.h>
3: #include <sys/stat.h>
4: #include <stdio.h>
5: #include "defs.h"
6: #include "scanmail.h"
7:
8: /* import */
9: extern char *strcat(), *strcpy(), *strncpy();
10:
11: /* init the file */
12: void
13: sminit (smp, file, name)
14: struct scanmail *smp;
15: char *file;
16: char *name;
17: {
18: char *p;
19: struct stat buf;
20: char dir[128];
21: char *strrchr();
22: char islog = FALSE;
23: int fd;
24:
25: /* look for a mail log */
26: if ((fd=open(MAILLOG, 0)) >= 0)
27: islog = TRUE;
28: close(fd);
29:
30: /* defaults */
31: strcpy (smp->sm_match, "From ");
32: smp->sm_log = FALSE;
33:
34: /* change directory to avoid screwing with namei */
35: strcpy(dir, MAILDIR);
36: strcat(dir, "/");
37: strcat(dir, name);
38: if (file == NULL || strcmp(dir, file) == 0) {
39:
40: /* use the standard mail directory if none is specified */
41: chdir (MAILDIR);
42: if (islog) {
43: smp->sm_log = TRUE;
44: strcpy (smp->sm_file, MAILNAME);
45: strcpy (smp->sm_match, "delivered ");
46: strcat (smp->sm_match, name);
47: strcat (smp->sm_match, " From ");
48: } else {
49: strcpy (smp->sm_file, name);
50: }
51: } else {
52:
53: /* use whatever was specified */
54: if ((p = strrchr(file, '/')) != NULL) {
55: strcpy (smp->sm_file, ++p);
56: strncpy (dir, file, p - file);
57: dir[p-file] = '\0';
58: chdir (dir);
59: } else {
60: strcpy (smp->sm_file, file);
61: }
62: }
63:
64: /* get the end point */
65: smp->sm_pos = (stat(smp->sm_file, &buf) < 0)? 0 : buf.st_size;
66: }
67:
68: /* get the next header line (if the file has changed) */
69: char *
70: smnext (smp, fp)
71: struct scanmail *smp;
72: FILE *fp;
73: {
74: struct stat buf;
75: static char line[128];
76: int len;
77: char *sp=NULL, *mp, *ep;
78: FILE *f;
79: char *strchr();
80:
81: /* return if it hasn't gotten longer */
82: if (stat(smp->sm_file, &buf) < 0)
83: return 0;
84: if (buf.st_size <= smp->sm_pos) {
85: smp->sm_pos = buf.st_size;
86: return 0;
87: }
88:
89: /* it's grown, read it line by line */
90: f = (fp != NULL && smp->sm_log) ? fp : fopen (smp->sm_file, "r");
91: fseek (f, smp->sm_pos, 0);
92: len = strlen(smp->sm_match);
93: while (fgets(line, sizeof line, f) != NULL) {
94:
95: /* remember how far we've read */
96: smp->sm_pos += strlen (line);
97:
98: /* look for a match */
99: if (strncmp(line, smp->sm_match, strlen(smp->sm_match)) == 0) {
100:
101: /* get the sender's name and machine */
102: ep = mp = line+len;
103: while (1) {
104: sp = mp;
105: mp = ep;
106: if ((ep = strchr(ep, '!')) == NULL)
107: break;
108: ep++;
109: }
110: for (ep = sp; *ep != ' ' && *ep != '\0'; ep++)
111: ;
112: *ep = '\0';
113: break;
114: }
115: }
116: if (fp == NULL || !smp->sm_log)
117: fclose (f);
118: return sp;
119: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.