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];
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);
128 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
129 if ((out = fopen (tmpfil, "w")) == NULL)
130 adios (tmpfil, "unable to write");
131 chmod (tmpfil, 0600);
133 SIGNAL2 (SIGINT, intrser);
135 fprintf (out, "%sTo: %s\n", resent ? "Resent-" : "", tolist);
137 fprintf (out, "%scc: %s\n", resent ? "Resent-" : "", cclist);
139 fprintf (out, "%sSubject: %s\n", resent ? "Resent-" : "", subject);
141 fprintf (out, "%sFrom: %s\n", resent ? "Resent-" : "", from);
146 fprintf (out, "%s", body);
147 if (*body && *(body + strlen (body) - 1) != '\n')
151 (i = fread (buf, sizeof(*buf), sizeof(buf), stdin)) > 0;
153 if (fwrite (buf, sizeof(*buf), i, out) != i)
154 adios (tmpfil, "error writing");
163 vec[nvec++] = r1bindex (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 admonish (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", NULL);
187 done (status ? 1 : 0);
190 execvp (postproc, vec);
191 fprintf (stderr, "unable to exec ");
196 return 0; /* dead code to satisfy the compiler */
203 #ifndef RELIABLE_SIGNALS
209 done (i != 0 ? 1 : 0);