3 * mhmail.c -- simple mail program
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/signals.h>
15 static struct swit switches[] = {
19 { "cc addrs ...", 0 },
23 { "subject text", 0 },
35 static char tmpfil[BUFSIZ];
40 static void intrser (int);
44 main (int argc, char **argv)
47 int status, 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];
57 setlocale(LC_ALL, "");
59 invo_name = r1bindex (argv[0], '/');
61 /* foil search of user profile/context */
62 if (context_foil (NULL) == -1)
65 /* If no arguments, just incorporate new mail */
67 execlp (incproc, r1bindex (incproc, '/'), NULL);
68 adios (incproc, "unable to exec");
71 arguments = getarguments (invo_name, argc, argv, 0);
74 while ((cp = *argp++)) {
76 switch (smatch (++cp, switches)) {
78 ambigsw (cp, switches);
81 adios (NULL, "-%s unknown", cp);
84 snprintf (buf, sizeof(buf), "%s [addrs ... [switches]]",
86 print_help (buf, switches, 0);
89 print_version(invo_name);
93 if (!(from = *argp++) || *from == '-')
94 adios (NULL, "missing argument to %s", argp[-2]);
98 if (!(body = *argp++) || *body == '-')
99 adios (NULL, "missing argument to %s", argp[-2]);
107 if (!(subject = *argp++) || *subject == '-')
108 adios (NULL, "missing argument to %s", argp[-2]);
121 cclist = cclist ? add (cp, add (", ", cclist)) : getcpy (cp);
123 tolist = tolist ? add (cp, add (", ", tolist)) : getcpy (cp);
127 adios (NULL, "usage: %s addrs ... [switches]", invo_name);
129 tfile = m_mktemp2(NULL, invo_name, NULL, &out);
130 if (tfile == NULL) 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')
152 (i = fread (buf, sizeof(*buf), sizeof(buf), stdin)) > 0;
154 if (fwrite (buf, sizeof(*buf), i, out) != i)
155 adios (tmpfil, "error writing");
164 vec[nvec++] = r1bindex (postproc, '/');
165 vec[nvec++] = tmpfil;
167 vec[nvec++] = "-dist";
169 vec[nvec++] = "-queued";
172 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
175 if (child_id == NOTOK) {
176 /* report failure and then send it */
177 adios (NULL, "unable to fork");
178 } else if (child_id) {
180 if ((status = pidXwait(child_id, postproc))) {
181 fprintf (stderr, "Letter saved in dead.letter\n");
182 execl ("/bin/mv", "mv", tmpfil, "dead.letter", NULL);
183 execl ("/usr/bin/mv", "mv", tmpfil, "dead.letter", NULL);
188 done (status ? 1 : 0);
191 execvp (postproc, vec);
192 fprintf (stderr, "unable to exec ");
197 return 0; /* dead code to satisfy the compiler */
205 done (i != 0 ? 1 : 0);