3 * mhmail.c -- simple mail program
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
13 #include <h/signals.h>
16 static struct swit switches[] = {
20 { "cc addrs ...", 0 },
24 { "subject text", 0 },
36 static char tmpfil[BUFSIZ];
41 static RETSIGTYPE intrser (int);
45 main (int argc, char **argv)
48 int status, i, iscc = 0, nvec;
49 int queued = 0, resent = 0, somebody;
50 char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
51 char *from = NULL, *body = NULL, **argp, **arguments;
52 char *vec[5], buf[BUFSIZ];
56 setlocale(LC_ALL, "");
58 invo_name = r1bindex (argv[0], '/');
60 /* foil search of user profile/context */
61 if (context_foil (NULL) == -1)
64 /* If no arguments, just incorporate new mail */
66 execlp (incproc, r1bindex (incproc, '/'), NULL);
67 adios (incproc, "unable to exec");
70 arguments = getarguments (invo_name, argc, argv, 0);
73 while ((cp = *argp++)) {
75 switch (smatch (++cp, switches)) {
77 ambigsw (cp, switches);
80 adios (NULL, "-%s unknown", cp);
83 snprintf (buf, sizeof(buf), "%s [addrs ... [switches]]",
85 print_help (buf, switches, 0);
88 print_version(invo_name);
92 if (!(from = *argp++) || *from == '-')
93 adios (NULL, "missing argument to %s", argp[-2]);
97 if (!(body = *argp++) || *body == '-')
98 adios (NULL, "missing argument to %s", argp[-2]);
106 if (!(subject = *argp++) || *subject == '-')
107 adios (NULL, "missing argument to %s", argp[-2]);
120 cclist = cclist ? add (cp, add (", ", cclist)) : getcpy (cp);
122 tolist = tolist ? add (cp, add (", ", tolist)) : getcpy (cp);
126 adios (NULL, "usage: %s addrs ... [switches]", invo_name);
127 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
128 if ((out = fopen (tmpfil, "w")) == NULL)
129 adios (tmpfil, "unable to write");
130 chmod (tmpfil, 0600);
132 SIGNAL2 (SIGINT, intrser);
134 fprintf (out, "%sTo: %s\n", resent ? "Resent-" : "", tolist);
136 fprintf (out, "%scc: %s\n", resent ? "Resent-" : "", cclist);
138 fprintf (out, "%sSubject: %s\n", resent ? "Resent-" : "", subject);
140 fprintf (out, "%sFrom: %s\n", resent ? "Resent-" : "", from);
145 fprintf (out, "%s", body);
146 if (*body && *(body + strlen (body) - 1) != '\n')
150 (i = fread (buf, sizeof(*buf), sizeof(buf), stdin)) > 0;
152 if (fwrite (buf, sizeof(*buf), i, out) != i)
153 adios (tmpfil, "error writing");
162 vec[nvec++] = r1bindex (postproc, '/');
163 vec[nvec++] = tmpfil;
165 vec[nvec++] = "-dist";
167 vec[nvec++] = "-queued";
170 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
173 if (child_id == NOTOK) {
174 /* report failure and then send it */
175 admonish (NULL, "unable to fork");
176 } else if (child_id) {
178 if ((status = pidXwait(child_id, postproc))) {
179 fprintf (stderr, "Letter saved in dead.letter\n");
180 execl ("/bin/mv", "mv", tmpfil, "dead.letter", NULL);
181 execl ("/usr/bin/mv", "mv", tmpfil, "dead.letter", NULL);
186 done (status ? 1 : 0);
189 execvp (postproc, vec);
190 fprintf (stderr, "unable to exec ");
195 return 0; /* dead code to satisfy the compiler */
202 #ifndef RELIABLE_SIGNALS
208 done (i != 0 ? 1 : 0);