2 Proprietary Rand Corporation
, 1981.
3 Further distribution of
this software
4 subject to the terms of the Rand
12 extern char *sprintf();
14 #define NUMTOS 10 /* Number of to's & cc's accepted */
20 struct swit switches
[] = {
35 int pid
, status
, top
, ccp
, sig(), somebody
= 0;
36 char buf
[BUFSIZ
], *tos
[NUMTOS
], *ccs
[NUMTOS
], **argp
;
40 ccp
= -1; /* -1 -> collecting TOs */
41 if(argc
== 1) { /* Just call inc to read mail */
42 execlp("inc", "inc", 0);
49 switch(smatch(++cp
, switches
)) {
50 case -2:ambigsw(cp
, switches
); /* ambiguous */
53 case -1:fprintf(stderr
, "send: -%s unknown\n", cp
);
56 case 0: if((body
= *argp
++) == 0) {
57 missing
: fprintf(stderr
, "Mail: Missing %s arg\n", cp
);
62 case 1: ccp
= 0; /* Now collecting ccs */
65 case 2: if((subject
= *argp
++) == 0)
69 case 3: help("mail [switches] users ...",
74 if(ccp
>= 0) { /* If getting ccs..*/
78 fprintf(stderr
, "Mail: Too many ccs\n");
81 } else { /* Else, to */
85 fprintf(stderr
, "Mail: Too many tos\n");
91 /* Create a mail temp file */
92 VOID
sprintf(tmpfil
, "/tmp/%s", makename("mail", ".tmp"));
93 if((out
= fopen(tmpfil
, "w")) == NULL
) {
97 VOID
signal(SIGINT
, sig
); /* Clean up if user <del>s out */
98 fprintf(out
, "To: "); /* Create to list */
99 for(i
= 0; i
< top
;) {
100 fprintf(out
, "%s", tos
[i
]);
106 fprintf(out
, "Cc: "); /* Create cc list if needed */
107 for(i
= 0; i
< ccp
;) {
108 fprintf(out
, "%s", ccs
[i
]);
114 if(subject
) /* Create subject if needed */
115 fprintf(out
, "Subject: %s\n", subject
);
119 if(body
) { /* Use body if I have it, */
121 fprintf(out
, "%s\n", body
);
122 } else { /* Otherwise, get a body */
123 while((cnt
= read(0, buf
, sizeof buf
)) > 0) {
125 if(!fwrite(buf
, cnt
, 1, out
)) {
132 if(ferror(out
)) { /* Check that all wrote well */
133 fprintf(stderr
, "Error writing tmp file\n");
137 if(!somebody
) /* If NO body, then don't send */
138 sig(); /* To be compatible with BELL mail */
139 while((i
= fork()) == -1) { /* Now, deliver the mail */
140 fprintf(stderr
, "Waiting for a fork...\n");
143 if(i
== 0) { /* Call deliverer in child */
144 execl(mh_deliver
, r1bindex(mh_deliver
, '/'), tmpfil
, 0);
148 VOID
signal(SIGINT
, SIG_IGN
);
149 while((pid
= wait(&status
)) != -1 && pid
!= i
) ;
150 if(status
) { /* And save mail if delivery failed */
151 VOID
signal(SIGINT
, SIG_DFL
);
152 fprintf(stderr
, "Letter saved in dead.letter\n");
153 execl("/bin/mv", "mv", tmpfil
, "dead.letter", 0);
154 execl("/usr/bin/mv", "mv", tmpfil
, "dead.letter", 0);