]> diplodocus.org Git - nmh/blob - docs/historical/mh-jun-1982/progs/inc.c
Replaced use of snprintf() with memcpy()/memmove().
[nmh] / docs / historical / mh-jun-1982 / progs / inc.c
1 #ifdef COMMENT
2 Proprietary Rand Corporation, 1981.
3 Further distribution of this software
4 subject to the terms of the Rand
5 license agreement.
6 #endif
7
8 #include "../mh.h"
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <errno.h>
13 #include <strings.h>
14 #include <signal.h>
15 #include "scansub.h"
16
17 extern char *sprintf();
18
19 extern struct swit anoyes[]; /* Std no/yes gans array */
20
21 char scanl[];
22 struct msgs *mp;
23 FILE *in, *aud;
24 struct stat stbuf;
25 char *locknode;
26 int lockwait; /* Secs to wait for lock-Def in strings/lockdir.c */
27 #define LOCKWAIT (lockwait*5) /* If lock is this old, simply ignore it! */
28
29 int timeflag;
30 int numflag;
31
32 extern int errno; /* MLW 4bsd does not have errno defined in errno.h */
33 extern char _sobuf[]; /* MLW standard out buffer */
34
35 struct swit switches[] = {
36 "audit audit-file", 0, /* 0 */
37 "ms ms-folder", 0, /* 1 */
38 "help", 4, /* 2 */
39 "changecur", 0, /* 3 */
40 "nochangecur", 0, /* 4 */
41 "time", 0, /* 5 */
42 "notime", 0, /* 6 */
43 "numdate", 0, /* 7 */
44 "nonumdate", 0, /* 8 */
45 0, 0
46 };
47
48 /*ARGSUSED*/
49 main(argc, argv)
50 char *argv[];
51 {
52
53 char *newmail, maildir[128], *folder, *from, *audfile;
54 char *myname;
55 int change_cur;
56 register char *cp;
57 register int i, msgnum;
58 long now;
59 char **ap;
60 char *arguments[50], **argp;
61 int done();
62 long time();
63
64 invo_name = argv[0];
65 setbuf(stdout, _sobuf);
66 #ifdef NEWS
67 m_news();
68 #endif
69
70 change_cur = 1; /* Default */
71 from = 0; folder = 0; audfile = 0;
72 cp = r1bindex(argv[0], '/');
73 if((cp = m_find(cp)) != NULL) {
74 ap = brkstring(cp = getcpy(cp), " ", "\n");
75 ap = copyip(ap, arguments);
76 } else
77 ap = arguments;
78 VOID copyip(argv+1, ap);
79 argp = arguments;
80 while(cp = *argp++) {
81 if(*cp == '-')
82 switch(smatch(++cp, switches)) {
83 case -2:ambigsw(cp, switches); /* ambiguous */
84 goto leave;
85 /* unknown */
86 case -1:fprintf(stderr, "inc: -%s unknown\n", cp);
87 goto leave;
88 case 0: if(!(audfile = *argp++)) { /* -audit */
89 missing: fprintf(stderr, "inc: Missing argument for %s switch\n", argp[-2]);
90 goto leave;
91 }
92 continue;
93 case 1: if(!(from = *argp++)) /* -ms */
94 goto missing;
95 continue;
96 case 2: /* -help */
97 help("inc [+folder] [switches]", switches);
98 goto leave;
99 case 3:
100 change_cur = 1;
101 continue;
102 case 4:
103 change_cur = 0;
104 continue;
105 case 5: timeflag = 1; continue; /* -time */
106 case 6: timeflag = 0; continue; /* -notime */
107 case 7: numflag = 1; continue; /* -numdate */
108 case 8: numflag = 0; continue; /* -nonumdate */
109 }
110 if(*cp == '+') {
111 if(folder) {
112 fprintf(stderr, "Only one folder at a time.\n");
113 goto leave;
114 } else
115 folder = path(cp+1, TFOLDER);
116 } else {
117 fprintf(stderr, "Bad arg: %s\n", argp[-1]);
118 fprintf(stderr, "Usage: inc [+folder] [-ms ms-folder] [-audit audit-file]\n");
119 goto leave;
120 }
121 }
122 if(!m_find("path")) free(path("./", TFOLDER));
123 if(from)
124 newmail = from;
125 else {
126 if((myname = getenv("USER")) == 0) {
127 fprintf(stderr,
128 "Environment Variable \"USER\" Must be set to your login name!\n");
129 done(1);
130 }
131 newmail = concat(mailboxes, "/", myname, NULLCP);
132 /*** VOID copy(mailbox, copy(mypath, newmail)); ***/
133 if(stat(newmail, &stbuf) < 0 ||
134 stbuf.st_size == 0) {
135 fprintf(stderr, "No Mail to incorporate.\n");
136 goto leave;
137 }
138 }
139 if(!folder) {
140 folder = defalt;
141 if(from && strcmp(from, "inbox") == 0) {
142 cp = concat("Do you really want to convert from ",
143 from, " into ", folder, "?? ", NULLCP);
144 if(!gans(cp, anoyes))
145 goto leave;
146 cndfree(cp);
147 }
148 }
149 VOID copy(m_maildir(folder), maildir);
150 if(stat(maildir, &stbuf) < 0) {
151 if(errno != ENOENT) {
152 fprintf(stderr, "Error on folder ");
153 perror(maildir);
154 goto leave;
155 }
156 cp = concat("Create folder \"", maildir, "\"? ", NULLCP);
157 if(!gans(cp, anoyes))
158 goto leave;
159 if(!makedir(maildir)) {
160 fprintf(stderr, "Can't create folder \"%s\"\n", maildir);
161 goto leave;
162 }
163 }
164 if(chdir(maildir) < 0) {
165 fprintf(stderr, "Can't chdir to: ");
166 perror(maildir);
167 goto leave;
168 }
169 if(!(mp = m_gmsg(folder))) {
170 fprintf(stderr, "Can't read folder!?\n");
171 goto leave;
172 }
173 /* Lock the mail file */
174 if(!from) {
175 VOID signal(SIGINT, done);
176 cp = concat(lockdir, "/", myname, NULLCP);
177 for(i = 0; i < lockwait; i += 2) {
178 if(link(newmail, cp) == -1) {
179 fprintf(stderr, "Mailbox busy...\n");
180 if(i == 0 && stat(newmail, &stbuf) >= 0)
181 if(stbuf.st_ctime + LOCKWAIT < time((long *)0)) {
182 VOID unlink(cp);
183 fprintf(stderr, "Removing lock.\n");
184 continue;
185 }
186 sleep(2);
187 } else {
188 locknode = cp; /* We own the lock now! */
189 break;
190 }
191 }
192 if(i >= lockwait) {
193 fprintf(stderr, "Try again.\n");
194 done(1);
195 }
196 }
197 if((in = fopen(newmail, "r")) == NULL) {
198 fprintf(stderr, "Can't open "); perror(newmail);
199 goto leave;
200 }
201 if(audfile) {
202 cp = m_maildir(audfile);
203 if((i = stat(cp, &stbuf)) < 0)
204 fprintf(stderr, "Creating Receive-Audit: %s\n", cp);
205 if((aud = fopen(cp, "a")) == NULL) {
206 fprintf(stderr, "Can't append to ");
207 perror(cp);
208 goto leave;
209 } else if(i < 0)
210 VOID chmod(cp, 0600);
211 now = time((long *)0);
212 fputs("<<inc>> ", aud);
213 cp = cdate(&now);
214 cp[9] = ' ';
215 fputs(cp, aud);
216 if(from) {
217 fputs(" -ms ", aud);
218 fputs(from, aud);
219 }
220 putc('\n', aud);
221 }
222 printf("Incorporating new mail into %s...\n\n", folder);
223 VOID fflush(stdout);
224 msgnum = mp->hghmsg;
225
226 while((i = scan(in, msgnum+1, msgnum+1, msgnum == mp->hghmsg,
227 (timeflag ? DOTIME : 0)
228 | (numflag ? NUMDATE : 0), 0))) {
229 if(i == -1) {
230 fprintf(stderr, "inc aborted!\n");
231 if(aud)
232 fputs("inc aborted!\n", aud);
233 goto leave;
234 }
235 if(i == -2) {
236 fprintf(stderr,
237 "More than %d messages. Inc aborted!\n",
238 MAXFOLDER);
239 fprintf(stderr,"%s not zero'd\n", newmail);
240 goto leave;
241 }
242 if(aud)
243 fputs(scanl, aud);
244 VOID fflush(stdout);
245 msgnum++;
246 }
247
248 VOID fclose(in);
249 if(aud)
250 VOID fclose(aud);
251
252 if(!from) {
253 if((i = creat(newmail, 0600)) >= 0) /* Zap .mail file */
254 VOID close(i);
255 else
256 fprintf(stderr, "Error zeroing %s\n", newmail);
257 } else
258 printf("%s not zero'd\n", newmail);
259
260 i = msgnum - mp->hghmsg;
261 /* printf("%d new message%s\n", i, i==1? "":"s"); */
262 if(!i)
263 fprintf(stderr, "[No messages incorporated.]\n");
264 else {
265 m_replace(pfolder, folder);
266 if (change_cur)
267 m_setcur(mp->hghmsg + 1);
268 }
269 leave:
270 m_update();
271 done(0);
272 }
273
274
275 done(status)
276 {
277 if(locknode);
278 VOID unlink(locknode);
279 exit(sta