]> diplodocus.org Git - nmh/blob - docs/historical/mh-nov-1983/cmds/inc.c
Removed --depth 1 from git clone invocation.
[nmh] / docs / historical / mh-nov-1983 / cmds / inc.c
1 #ifndef lint
2 static char sccsid[] = "@(#)inc.c 1.2 9/25/83";
3 #endif
4
5 #include "mh.h"
6 #include <stdio.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <errno.h>
10 #include <strings.h>
11 #include <signal.h>
12
13 char *anoyes[]; /* Std no/yes gans array */
14
15 extern int errno;
16 char scanl[];
17 struct msgs *mp;
18 FILE *in, *aud;
19 struct stat stbuf;
20 char *locknode;
21 int lockwait; /* Secs to wait for lock-Def in strings/lockdir.c */
22 #define LOCKWAIT (lockwait*5) /* If lock is this old, simply ignore it! */
23
24 struct swit switches[] = {
25 "audit audit-file", 0, /* 0 */
26 "ms ms-folder", 0, /* 1 */
27 "help", 4, /* 2 */
28 "file mail-file", 0, /* 3 */
29 0, 0
30 };
31
32 #ifdef UNIXCOMP
33 char unixtmp[] = "/tmp/mhXXXXXX";
34 #endif
35
36 main(argc, argv)
37 char *argv[];
38 {
39
40 char *newmail, maildir[128], *folder, *from, *audfile, *srcfile;
41 char mailspace[128];
42 char *myname, *savemail;
43 register char *cp;
44 register int i, msgnum;
45 long now;
46 char **ap;
47 char *arguments[50], **argp;
48 int done();
49 long time();
50 int pid, status, fildes;
51 extern char _sobuf[];
52
53 setbuf(stdout, _sobuf);
54 #ifdef NEWS
55 m_news();
56 #endif
57
58 from = 0; folder = 0; audfile = 0; srcfile = 0;
59 cp = r1bindex(argv[0], '/');
60 if((cp = m_find(cp)) != NULL) {
61 ap = brkstring(cp = getcpy(cp), " ", "\n");
62 ap = copyip(ap, arguments);
63 } else
64 ap = arguments;
65 copyip(argv+1, ap);
66 argp = arguments;
67 while(cp = *argp++) {
68 if(*cp == '-')
69 switch(smatch(++cp, switches)) {
70 case -2:ambigsw(cp, switches); /* ambiguous */
71 goto leave;
72 /* unknown */
73 case -1:fprintf(stderr, "inc: -%s unknown\n", cp);
74 goto leave;
75 case 0: if(!(audfile = *argp++)) { /* -audit */
76 missing: fprintf(stderr, "inc: Missing argument for %s switch\n", argp[-2]);
77 goto leave;
78 }
79 continue;
80 case 1: if(!(from = *argp++)) /* -ms */
81 goto missing;
82 continue;
83 case 2: /* -help */
84 help("inc [+folder] [switches]", switches);
85 goto leave;
86 case 3: /* -file */
87 if (!(srcfile = *argp++))
88 goto missing;
89 continue;
90 }
91 if(*cp == '+') {
92 if(folder) {
93 fprintf(stderr, "Only one folder at a time.\n");
94 goto leave;
95 } else
96 folder = cp + 1;
97 } else {
98 fprintf(stderr, "Bad arg: %s\n", argp[-1]);
99 fprintf(stderr, "Usage: inc [+folder] [-ms ms-folder] [-audit audit-file]\n");
100 goto leave;
101 }
102 }
103 if (from && srcfile) {
104 fprintf(stderr, "Only one of \"-ms\" and \"-file\" allowed\n");
105 goto leave;
106 }
107 if(from)
108 newmail = from;
109 else {
110 if((myname = getenv("USER")) == 0) {
111 fprintf(stderr,
112 "Environment Variable \"USER\" Must be set to your login name!\n");
113 done(1);
114 }
115 if (srcfile) {
116 newmail = srcfile;
117 goto statit;
118 }
119 newmail = concat(mailboxes, myname, 0);
120 if (stat(newmail, &stbuf) >= 0
121 && (stbuf.st_mode & S_IFMT) == S_IFDIR) {
122 strcpy(mailspace, newmail);
123 newmail = mailspace;
124 strcat(newmail, "/");
125 strcat(newmail, myname);
126 }
127 statit:
128 if(stat(newmail, &stbuf) < 0 ||
129 stbuf.st_size == 0) {
130 fprintf(stderr, "No Mail to incorporate.\n");
131 goto leave;
132 }
133 }
134 fildes = -1;
135 if (srcfile)
136 if ((fildes = open(srcfile, 0)) < 0) {
137 perror(srcfile);
138 goto leave;
139 }
140 if(!folder) {
141 folder = defalt;
142 if(from && strcmp(from, "inbox") == 0) {
143 cp = concat("Do you really want to convert from ",
144 from, " into ", folder, "?? ", 0);
145 if(!gans(cp, anoyes))
146 goto leave;
147 cndfree(cp);
148 }
149 }
150 copy(m_maildir(folder), maildir);
151 if(stat(maildir, &stbuf) < 0) {
152 if(errno != ENOENT) {
153 fprintf(stderr, "Error on folder ");
154 perror(maildir);
155 goto leave;
156 }
157 cp = concat("Create folder \"", maildir, "\"? ", 0);
158 if(!gans(cp, anoyes))
159 goto leave;
160 if(!makedir(maildir)) {
161 fprintf(stderr, "Can't create folder \"%s\"\n", maildir);
162 goto leave;
163 }
164 }
165 if(chdir(maildir) < 0) {
166 fprintf(stderr, "Can't chdir to: ");
167 perror(maildir);
168 goto leave;
169 }
170 if(!(mp = m_gmsg(folder))) {
171 fprintf(stderr, "Can't read folder!?\n");
172 goto leave;
173 }
174 /* Lock the mail file */
175 if(!from && !srcfile) {
176 signal(SIGINT, done);
177 cp = concat(lockdir, myname, ".lock", 0);
178 for(i = 0; i < lockwait; i += 2) {
179 if(link(newmail, cp) == -1) {
180 fprintf(stderr, "Mailbox busy...\n");
181 if(i == 0 && stat(newmail, &stbuf) >= 0)
182 if(stbuf.st_ctime + LOCKWAIT < time((long *)0)) {
183 unlink(cp);
184 fprintf(stderr, "Removing lock.\n");
185 continue;
186 }
187 sleep(2);
188 } else {
189 locknode = cp; /* We own the lock now! */
190 break;
191 }
192 }
193 if(i >= lockwait) {
194 fprintf(stderr, "Try again.\n");
195 done(1);
196 }
197 }
198
199 #ifdef UNIXCOMP
200 /*
201 * If trying to be compatible with standard
202 * UNIX mailing scheme, call the program unixtomh
203 * to convert the mailbox to some temporary name.
204 */
205
206 mktemp(unixtmp);
207 switch (pid = fork()) {
208 case 0:
209 if (srcfile) {
210 int nout;
211
212 if ((nout = creat(unixtmp, 0600)) < 0) {
213 perror(unixtmp);
214 _exit(1);
215 }
216 close(0);
217 dup(fildes);
218 close(fildes);
219 close(1);
220 dup(nout);
221 close(nout);
222 execl(unixtomh, "unixtomh", 0);
223 perror(unixtomh);
224 _exit(1);
225 }
226 execl(unixtomh, "unixtomh", newmail, unixtmp, 0);
227 perror(unixtomh);
228 _exit(1);
229 break;
230
231 case -1:
232 perror("fork");
233 goto leave;
234
235 default:
236 while (wait(&status) != pid)
237 ;
238 if (status != 0) {
239 fprintf(stderr, "unixtomh failed!?\n");
240 goto leave;
241 }
242 }
243 if (fildes >= 0)
244 close(fildes);
245 savemail = newmail;
246 newmail = unixtmp;
247 if((in = fopen(newmail, "r")) == NULL) {
248 fprintf(stderr, "Can't open "); perror(newmail);
249 goto leave;
250 }
251 #else
252 if (srcfile)
253 in = fdopen(fildes, "r");
254 else
255 if((in = fopen(newmail, "r")) == NULL) {
256 fprintf(stderr, "Can't open "); perror(newmail);
257 goto leave;
258 }
259 #endif
260
261 if(audfile) {
262 cp = m_maildir(audfile);
263 if((i = stat(cp, &stbuf)) < 0)
264 fprintf(stderr, "Creating Receive-Audit: %s\n", cp);
265 if((aud = fopen(cp, "a")) == NULL) {
266 fprintf(stderr, "Can't append to ");
267 perror(cp);
268 goto leave;
269 } else if(i < 0)
270 chmod(cp, 0600);
271 time(&now);
272 fputs("<<inc>> ", aud);
273 cp = cdate(&now);
274 cp[9] = ' ';
275 fputs(cp, aud);
276 if(from) {
277 fputs(" -ms ", aud);
278 fputs(from, aud);
279 }
280 putc('\n', aud);
281 }
282 printf("Incorporating new mail into %s...\n\n", folder);
283 fflush(stdout);
284 msgnum = mp->hghmsg;
285
286 while((i = scan(in, msgnum+1, msgnum+1, msgnum == mp->hghmsg))) {
287 if(i == -1) {
288 fprintf(stderr, "inc aborted!\n");
289 if(aud)
290 fputs("inc aborted!\n", aud);
291 goto leave;
292 }
293 if(aud)
294 fputs(scanl, aud);
295 fflush(stdout);
296 msgnum++;
297 }
298
299 fclose(in);
300 if(aud)
301 fclose(aud);
302
303 #ifdef UNIXCOMP
304 unlink(newmail);
305 newmail = savemail;
306 #endif
307
308 if(!from && !srcfile) {
309 if (unlink(newmail) < 0) {
310 if((i = creat(newmail, 0600)) >= 0)
311 close(i);
312 else
313 fprintf(stderr, "Error zeroing %s\n", newmail);
314 }
315 }
316
317 i = msgnum - mp->hghmsg;
318 if(!i)
319 fprintf(stderr, "[No messages incorporated.]\n");
320 else {
321 m_replace(pfolder, folder);
322 m_setcur(mp->hghmsg + 1);
323 }
324 leave:
325 m_update();
326 done(0);
327 }
328
329
330 done(status)
331 {
332 if(locknode);
333 unlink(locknode);
334 exit(status);
335 }