]>
diplodocus.org Git - nmh/blob - docs/historical/mh-nov-1983/cmds/inc.c
2 static char sccsid
[] = "@(#)inc.c 1.2 9/25/83";
13 char *anoyes
[]; /* Std no/yes gans array */
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! */
24 struct swit switches
[] = {
25 "audit audit-file", 0, /* 0 */
26 "ms ms-folder", 0, /* 1 */
28 "file mail-file", 0, /* 3 */
33 char unixtmp
[] = "/tmp/mhXXXXXX";
40 char *newmail
, maildir
[128], *folder
, *from
, *audfile
, *srcfile
;
42 char *myname
, *savemail
;
44 register int i
, msgnum
;
47 char *arguments
[50], **argp
;
50 int pid
, status
, fildes
;
53 setbuf(stdout
, _sobuf
);
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
);
69 switch(smatch(++cp
, switches
)) {
70 case -2:ambigsw(cp
, switches
); /* ambiguous */
73 case -1:fprintf(stderr
, "inc: -%s unknown\n", cp
);
75 case 0: if(!(audfile
= *argp
++)) { /* -audit */
76 missing
: fprintf(stderr
, "inc: Missing argument for %s switch\n", argp
[-2]);
80 case 1: if(!(from
= *argp
++)) /* -ms */
84 help("inc [+folder] [switches]", switches
);
87 if (!(srcfile
= *argp
++))
93 fprintf(stderr
, "Only one folder at a time.\n");
98 fprintf(stderr
, "Bad arg: %s\n", argp
[-1]);
99 fprintf(stderr
, "Usage: inc [+folder] [-ms ms-folder] [-audit audit-file]\n");
103 if (from
&& srcfile
) {
104 fprintf(stderr
, "Only one of \"-ms\" and \"-file\" allowed\n");
110 if((myname
= getenv("USER")) == 0) {
112 "Environment Variable \"USER\" Must be set to your login name!\n");
119 newmail
= concat(mailboxes
, myname
, 0);
120 if (stat(newmail
, &stbuf
) >= 0
121 && (stbuf
.st_mode
& S_IFMT
) == S_IFDIR
) {
122 strcpy(mailspace
, newmail
);
124 strcat(newmail
, "/");
125 strcat(newmail
, myname
);
128 if(stat(newmail
, &stbuf
) < 0 ||
129 stbuf
.st_size
== 0) {
130 fprintf(stderr
, "No Mail to incorporate.\n");
136 if ((fildes
= open(srcfile
, 0)) < 0) {
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
))
150 copy(m_maildir(folder
), maildir
);
151 if(stat(maildir
, &stbuf
) < 0) {
152 if(errno
!= ENOENT
) {
153 fprintf(stderr
, "Error on folder ");
157 cp
= concat("Create folder \"", maildir
, "\"? ", 0);
158 if(!gans(cp
, anoyes
))
160 if(!makedir(maildir
)) {
161 fprintf(stderr
, "Can't create folder \"%s\"\n", maildir
);
165 if(chdir(maildir
) < 0) {
166 fprintf(stderr
, "Can't chdir to: ");
170 if(!(mp
= m_gmsg(folder
))) {
171 fprintf(stderr
, "Can't read folder!?\n");
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)) {
184 fprintf(stderr
, "Removing lock.\n");
189 locknode
= cp
; /* We own the lock now! */
194 fprintf(stderr
, "Try again.\n");
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.
207 switch (pid
= fork()) {
212 if ((nout
= creat(unixtmp
, 0600)) < 0) {
222 execl(unixtomh
, "unixtomh", 0);
226 execl(unixtomh
, "unixtomh", newmail
, unixtmp
, 0);
236 while (wait(&status
) != pid
)
239 fprintf(stderr
, "unixtomh failed!?\n");
247 if((in
= fopen(newmail
, "r")) == NULL
) {
248 fprintf(stderr
, "Can't open "); perror(newmail
);
253 in
= fdopen(fildes
, "r");
255 if((in
= fopen(newmail
, "r")) == NULL
) {
256 fprintf(stderr
, "Can't open "); perror(newmail
);
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 ");
272 fputs("<<inc>> ", aud
);
282 printf("Incorporating new mail into %s...\n\n", folder
);
286 while((i
= scan(in
, msgnum
+1, msgnum
+1, msgnum
== mp
->hghmsg
))) {
288 fprintf(stderr
, "inc aborted!\n");
290 fputs("inc aborted!\n", aud
);
308 if(!from
&& !srcfile
) {
309 if (unlink(newmail
) < 0) {
310 if((i
= creat(newmail
, 0600)) >= 0)
313 fprintf(stderr
, "Error zeroing %s\n", newmail
);
317 i
= msgnum
- mp
->hghmsg
;
319 fprintf(stderr
, "[No messages incorporated.]\n");
321 m_replace(pfolder
, folder
);
322 m_setcur(mp
->hghmsg
+ 1);