]>
diplodocus.org Git - nmh/blob - docs/historical/mh-nov-1983/cmds/mail.c
5 #define NUMTOS 10 /* Number of to's & cc's accepted */
9 char *subject
, *cc
, *body
;
11 struct swit switches
[] = {
26 int pid
, status
, top
, ccp
, sig(), somebody
= 0;
27 char buf
[BUFSIZ
], *tos
[NUMTOS
], *ccs
[NUMTOS
], **argp
;
30 ccp
= -1; /* -1 -> collecting TOs */
31 if(argc
== 1) { /* Just call inc to read mail */
32 execl("/bin/inc", "inc", 0);
33 execl("/usr/bin/inc", "inc", 0);
40 switch(smatch(++cp
, switches
)) {
41 case -2:ambigsw(cp
, switches
); /* ambiguous */
44 case -1:fprintf(stderr
, "send: -%s unknown\n", cp
);
47 case 0: if((body
= *argp
++) == 0) {
48 missing
: fprintf(stderr
, "Mail: Missing %s arg\n", cp
);
53 case 1: ccp
= 0; /* Now collecting ccs */
56 case 2: if((subject
= *argp
++) == 0)
60 case 3: help("mail [switches] users ...",
65 if(ccp
>= 0) { /* If getting ccs..*/
69 fprintf(stderr
, "Mail: Too many ccs\n");
72 } else { /* Else, to */
76 fprintf(stderr
, "Mail: Too many tos\n");
82 /* Create a mail temp file */
83 sprintf(tmpfil
, "/tmp/%s", makename("mail", ".tmp"));
84 if((out
= fopen(tmpfil
, "w")) == NULL
) {
88 signal(SIGINT
, sig
); /* Clean up if user <del>s out */
89 fprintf(out
, "To: "); /* Create to list */
90 for(i
= 0; i
< top
;) {
91 fprintf(out
, "%s", tos
[i
]);
97 fprintf(out
, "Cc: "); /* Create cc list if needed */
98 for(i
= 0; i
< ccp
;) {
99 fprintf(out
, "%s", ccs
[i
]);
105 if(subject
) /* Create subject if needed */
106 fprintf(out
, "Subject: %s\n", subject
);
110 if(body
) { /* Use body if I have it, */
112 fprintf(out
, "%s\n", body
);
113 } else { /* Otherwise, get a body */
114 while((cnt
= read(0, buf
, sizeof buf
)) > 0) {
116 if(!fwrite(buf
, cnt
, 1, out
)) {
123 if(ferror(out
)) { /* Check that all wrote well */
124 fprintf(stderr
, "Error writing tmp file\n");
128 if(!somebody
) /* If NO body, then don't send */
129 sig(); /* To be compatible with BELL mail */
130 while((i
= fork()) == -1) { /* Now, deliver the mail */
131 fprintf(stderr
, "Waiting for a fork...\n");
134 if(i
== 0) { /* Call deliverer in child */
135 execl(mh_deliver
, mh_deliver
, tmpfil
, 0);
139 signal(SIGINT
, SIG_IGN
);
140 while((pid
= wait(&status
)) != -1 && pid
!= i
) ;
141 if(status
) { /* And save mail if delivery failed */
142 signal(SIGINT
, SIG_DFL
);
143 fprintf(stderr
, "Letter saved in dead.letter\n");
144 execl("/bin/mv", "mv", tmpfil
, "dead.letter", 0);
145 execl("/usr/bin/mv", "mv", tmpfil
, "dead.letter", 0);