2 ** mhmail.c -- simple mail program
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
10 #include <h/signals.h>
14 static struct swit switches[] = {
18 { "cc addrs ...", 0 },
22 { "subject text", 0 },
34 static char tmpfil[BUFSIZ];
39 static RETSIGTYPE intrser(int);
43 main(int argc, char **argv)
46 int status, i, iscc = 0, nvec;
47 int queued = 0, resent = 0, somebody;
48 char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
49 char *from = NULL, *body = NULL, **argp, **arguments;
50 char *vec[5], buf[BUFSIZ];
55 setlocale(LC_ALL, "");
57 invo_name = mhbasename(argv[0]);
59 /* foil search of user profile/context */
60 if (context_foil(NULL) == -1)
63 /* If no arguments, just incorporate new mail */
65 execlp(incproc, mhbasename(incproc), NULL);
66 adios(incproc, "unable to exec");
69 arguments = getarguments(invo_name, argc, argv, 0);
72 while ((cp = *argp++)) {
74 switch (smatch(++cp, switches)) {
76 ambigsw(cp, switches);
79 adios(NULL, "-%s unknown", cp);
82 snprintf(buf, sizeof(buf), "%s [addrs ... [switches]]", invo_name);
83 print_help(buf, switches, 0);
86 print_version(invo_name);
90 if (!(from = *argp++) || *from == '-')
91 adios(NULL, "missing argument to %s", argp[-2]);
95 if (!(body = *argp++) || *body == '-')
96 adios(NULL, "missing argument to %s", argp[-2]);
104 if (!(subject = *argp++) || *subject == '-')
105 adios(NULL, "missing argument to %s", argp[-2]);
118 cclist = cclist ? add(cp, add(", ", cclist)) :
121 tolist = tolist ? add(cp, add(", ", tolist)) :
126 adios(NULL, "usage: %s addrs ... [switches]", invo_name);
128 tfile = m_mktemp2(NULL, invo_name, NULL, &out);
130 adios("mhmail", "unable to create temporary file");
132 strncpy(tmpfil, tfile, sizeof(tmpfil));
134 SIGNAL2(SIGINT, intrser);
136 fprintf(out, "%sTo: %s\n", resent ? "Resent-" : "", tolist);
138 fprintf(out, "%scc: %s\n", resent ? "Resent-" : "", cclist);
140 fprintf(out, "%sSubject: %s\n", resent ? "Resent-" : "", subject);
142 fprintf(out, "%sFrom: %s\n", resent ? "Resent-" : "", from);
147 fprintf(out, "%s", body);
148 if (*body && *(body + strlen(body) - 1) != '\n')
151 for (somebody = 0; (i = fread(buf, sizeof(*buf), sizeof(buf),
152 stdin)) > 0; somebody++)
153 if (fwrite(buf, sizeof(*buf), i, out) != i)
154 adios(tmpfil, "error writing");
163 vec[nvec++] = mhbasename(postproc);
164 vec[nvec++] = tmpfil;
166 vec[nvec++] = "-dist";
168 vec[nvec++] = "-queued";
171 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
174 if (child_id == NOTOK) {
175 /* report failure and then send it */
176 adios(NULL, "unable to fork");
177 } else if (child_id) {
179 if ((status = pidXwait(child_id, postproc))) {
180 fprintf(stderr, "Letter saved in dead.letter\n");
181 execl("/bin/mv", "mv", tmpfil, "dead.letter", NULL);
182 execl("/usr/bin/mv", "mv", tmpfil, "dead.letter",
188 done(status ? 1 : 0);
191 execvp(postproc, vec);
192 fprintf(stderr, "unable to exec ");
197 return 0; /* dead code to satisfy the compiler */
204 #ifndef RELIABLE_SIGNALS
210 done(i != 0 ? 1 : 0);