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>
17 static struct swit switches[] = {
21 { "cc addrs ...", 0 },
25 { "subject text", 0 },
37 static char tmpfil[BUFSIZ];
42 static RETSIGTYPE intrser (int);
46 main (int argc, char **argv)
49 int status, i, iscc = 0, nvec;
50 int queued = 0, resent = 0, somebody;
51 char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
52 char *from = NULL, *body = NULL, **argp, **arguments;
53 char *vec[5], buf[BUFSIZ];
58 setlocale(LC_ALL, "");
60 invo_name = r1bindex (argv[0], '/');
62 /* foil search of user profile/context */
63 if (context_foil (NULL) == -1)
66 /* If no arguments, just incorporate new mail */
68 execlp (incproc, r1bindex (incproc, '/'), NULL);
69 adios (incproc, "unable to exec");
72 arguments = getarguments (invo_name, argc, argv, 0);
75 while ((cp = *argp++)) {
77 switch (smatch (++cp, switches)) {
79 ambigsw (cp, switches);
82 adios (NULL, "-%s unknown", cp);
85 snprintf (buf, sizeof(buf), "%s [addrs ... [switches]]",
87 print_help (buf, switches, 0);
90 print_version(invo_name);
94 if (!(from = *argp++) || *from == '-')
95 adios (NULL, "missing argument to %s", argp[-2]);
99 if (!(body = *argp++) || *body == '-')
100 adios (NULL, "missing argument to %s", argp[-2]);
108 if (!(subject = *argp++) || *subject == '-')
109 adios (NULL, "missing argument to %s", argp[-2]);
122 cclist = cclist ? add (cp, add (", ", cclist)) : getcpy (cp);
124 tolist = tolist ? add (cp, add (", ", tolist)) : getcpy (cp);
128 adios (NULL, "usage: %s addrs ... [switches]", invo_name);
130 tfile = m_mktemp2(NULL, invo_name, NULL, &out);
131 if (tfile == NULL) adios("mhmail", "unable to create temporary file");
133 strncpy (tmpfil, tfile, sizeof(tmpfil));
135 SIGNAL2 (SIGINT, intrser);
137 fprintf (out, "%sTo: %s\n", resent ? "Resent-" : "", tolist);
139 fprintf (out, "%scc: %s\n", resent ? "Resent-" : "", cclist);
141 fprintf (out, "%sSubject: %s\n", resent ? "Resent-" : "", subject);
143 fprintf (out, "%sFrom: %s\n", resent ? "Resent-" : "", from);
148 fprintf (out, "%s", body);
149 if (*body && *(body + strlen (body) - 1) != '\n')
153 (i = fread (buf, sizeof(*buf), sizeof(buf), stdin)) > 0;
155 if (fwrite (buf, sizeof(*buf), i, out) != i)
156 adios (tmpfil, "error writing");
165 vec[nvec++] = r1bindex (postproc, '/');
166 vec[nvec++] = tmpfil;
168 vec[nvec++] = "-dist";
170 vec[nvec++] = "-queued";
173 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
176 if (child_id == NOTOK) {
177 /* report failure and then send it */
178 adios (NULL, "unable to fork");
179 } else if (child_id) {
181 if ((status = pidXwait(child_id, postproc))) {
182 fprintf (stderr, "Letter saved in dead.letter\n");
183 execl ("/bin/mv", "mv", tmpfil, "dead.letter", NULL);
184 execl ("/usr/bin/mv", "mv", tmpfil, "dead.letter", NULL);
189 done (status ? 1 : 0);
192 execvp (postproc, vec);
193 fprintf (stderr, "unable to exec ");
198 return 0; /* dead code to satisfy the compiler */
205 #ifndef RELIABLE_SIGNALS
211 done (i != 0 ? 1 : 0);