]>
diplodocus.org Git - nmh/blob - docs/historical/2.9BSD/cmds/inc.c
9 char *anoyes
[]; /* Std no/yes gans array */
17 int lockwait
; /* Secs to wait for lock-Def in strings/lockdir.c */
18 #define LOCKWAIT (lockwait*5) /* If lock is this old, simply ignore it! */
20 struct swit switches
[] = {
21 "audit audit-file", 0, /* 0 */
22 "ms ms-folder", 0, /* 1 */
24 "file mail-file", 0, /* 3 */
29 char unixtmp
[] = "/tmp/mhXXXXXX";
36 char *newmail
, maildir
[128], *folder
, *from
, *audfile
, *srcfile
;
38 char *myname
, *savemail
;
40 register int i
, msgnum
;
43 char *arguments
[50], **argp
;
46 int pid
, status
, fildes
;
49 setbuf(stdout
, _sobuf
);
54 from
= 0; folder
= 0; audfile
= 0; srcfile
= 0;
55 cp
= r1bindex(argv
[0], '/');
56 if((cp
= m_find(cp
)) != NULL
) {
57 ap
= brkstring(cp
= getcpy(cp
), " ", "\n");
58 ap
= copyip(ap
, arguments
);
65 switch(smatch(++cp
, switches
)) {
66 case -2:ambigsw(cp
, switches
); /* ambiguous */
69 case -1:fprintf(stderr
, "inc: -%s unknown\n", cp
);
71 case 0: if(!(audfile
= *argp
++)) { /* -audit */
72 missing
: fprintf(stderr
, "inc: Missing argument for %s switch\n", argp
[-2]);
76 case 1: if(!(from
= *argp
++)) /* -ms */
80 help("inc [+folder] [switches]", switches
);
83 if (!(srcfile
= *argp
++))
89 fprintf(stderr
, "Only one folder at a time.\n");
94 fprintf(stderr
, "Bad arg: %s\n", argp
[-1]);
95 fprintf(stderr
, "Usage: inc [+folder] [-ms ms-folder] [-audit audit-file]\n");
99 if (from
&& srcfile
) {
100 fprintf(stderr
, "Only one of \"-ms\" and \"-file\" allowed\n");
106 if((myname
= getenv("USER")) == 0) {
108 "Environment Variable \"USER\" Must be set to your login name!\n");
115 newmail
= concat(mailboxes
, myname
, 0);
116 if (stat(newmail
, &stbuf
) >= 0
117 && (stbuf
.st_mode
& S_IFMT
) == S_IFDIR
) {
118 strcpy(mailspace
, newmail
);
120 strcat(newmail
, "/");
121 strcat(newmail
, myname
);
124 if(stat(newmail
, &stbuf
) < 0 ||
125 stbuf
.st_size
== 0) {
126 fprintf(stderr
, "No Mail to incorporate.\n");
132 if ((fildes
= open(srcfile
, 0)) < 0) {
138 if(from
&& strcmp(from
, "inbox") == 0) {
139 cp
= concat("Do you really want to convert from ",
140 from
, " into ", folder
, "?? ", 0);
141 if(!gans(cp
, anoyes
))
146 copy(m_maildir(folder
), maildir
);
147 if(stat(maildir
, &stbuf
) < 0) {
148 if(errno
!= ENOENT
) {
149 fprintf(stderr
, "Error on folder ");
153 cp
= concat("Create folder \"", maildir
, "\"? ", 0);
154 if(!gans(cp
, anoyes
))
156 if(!makedir(maildir
)) {
157 fprintf(stderr
, "Can't create folder \"%s\"\n", maildir
);
161 if(chdir(maildir
) < 0) {
162 fprintf(stderr
, "Can't chdir to: ");
166 if(!(mp
= m_gmsg(folder
))) {
167 fprintf(stderr
, "Can't read folder!?\n");
170 /* Lock the mail file */
171 if(!from
&& !srcfile
) {
172 signal(SIGINT
, done
);
173 cp
= concat(lockdir
, myname
, 0);
174 for(i
= 0; i
< lockwait
; i
+= 2) {
175 if(link(newmail
, cp
) == -1) {
176 fprintf(stderr
, "Mailbox busy...\n");
177 if(i
== 0 && stat(newmail
, &stbuf
) >= 0)
178 if(stbuf
.st_ctime
+ LOCKWAIT
< time((long *)0)) {
180 fprintf(stderr
, "Removing lock.\n");
185 locknode
= cp
; /* We own the lock now! */
190 fprintf(stderr
, "Try again.\n");
197 * If trying to be compatible with standard
198 * UNIX mailing scheme, call the program unixtomh
199 * to convert the mailbox to some temporary name.
203 switch (pid
= fork()) {
208 if ((nout
= creat(unixtmp
, 0600)) < 0) {
218 execl(unixtomh
, "unixtomh", 0);
222 execl(unixtomh
, "unixtomh", newmail
, unixtmp
, 0);
232 while (wait(&status
) != pid
)
235 fprintf(stderr
, "unixtomh failed!?\n");
243 if((in
= fopen(newmail
, "r")) == NULL
) {
244 fprintf(stderr
, "Can't open "); perror(newmail
);
249 in
= fdopen(fildes
, "r");
251 if((in
= fopen(newmail
, "r")) == NULL
) {
252 fprintf(stderr
, "Can't open "); perror(newmail
);
258 cp
= m_maildir(audfile
);
259 if((i
= stat(cp
, &stbuf
)) < 0)
260 fprintf(stderr
, "Creating Receive-Audit: %s\n", cp
);
261 if((aud
= fopen(cp
, "a")) == NULL
) {
262 fprintf(stderr
, "Can't append to ");
268 fputs("<<inc>> ", aud
);
278 printf("Incorporating new mail into %s...\n\n", folder
);
282 while((i
= scan(in
, msgnum
+1, msgnum
+1, msgnum
== mp
->hghmsg
))) {
284 fprintf(stderr
, "inc aborted!\n");
286 fputs("inc aborted!\n", aud
);
304 if(!from
&& !srcfile
) {
305 if (unlink(newmail
) < 0) {
306 if((i
= creat(newmail
, 0600)) >= 0)
309 fprintf(stderr
, "Error zeroing %s\n", newmail
);
313 i
= msgnum
- mp
->hghmsg
;
315 fprintf(stderr
, "[No messages incorporated.]\n");
317 m_replace(pfolder
, folder
);
318 m_setcur(mp
->hghmsg
+ 1);