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 /* Without arguments, exit. */
65 adios(NULL, "no interactive mail shell. Use inc/scan/show instead.");
68 arguments = getarguments(invo_name, argc, argv, 0);
71 while ((cp = *argp++)) {
73 switch (smatch(++cp, switches)) {
75 ambigsw(cp, switches);
78 adios(NULL, "-%s unknown", cp);
81 snprintf(buf, sizeof(buf),
82 "%s addrs... [switches]",
84 print_help(buf, switches, 0);
87 print_version(invo_name);
91 if (!(from = *argp++) || *from == '-')
92 adios(NULL, "missing argument to %s",
97 if (!(body = *argp++) || *body == '-')
98 adios(NULL, "missing argument to %s",
107 if (!(subject = *argp++) || *subject == '-')
108 adios(NULL, "missing argument to %s",
122 cclist = cclist ? add(cp, add(", ", cclist)) :
125 tolist = tolist ? add(cp, add(", ", tolist)) :
130 adios(NULL, "usage: %s addrs ... [switches]", invo_name);
132 tfile = m_mktemp2("/tmp/", invo_name, NULL, &out);
134 adios("mhmail", "unable to create temporary file");
136 strncpy(tmpfil, tfile, sizeof(tmpfil));
138 SIGNAL2(SIGINT, intrser);
140 fprintf(out, "%sTo: %s\n", resent ? "Resent-" : "", tolist);
142 fprintf(out, "%sCc: %s\n", resent ? "Resent-" : "", cclist);
144 fprintf(out, "%sSubject: %s\n", resent ? "Resent-" : "", subject);
146 fprintf(out, "%sFrom: %s\n", resent ? "Resent-" : "", from);
151 fprintf(out, "%s", body);
152 if (*body && *(body + strlen(body) - 1) != '\n')
155 for (somebody = 0; (i = fread(buf, sizeof(*buf), sizeof(buf),
156 stdin)) > 0; somebody++)
157 if (fwrite(buf, sizeof(*buf), i, out) != i)
158 adios(tmpfil, "error writing");
167 vec[nvec++] = "spost";
168 vec[nvec++] = tmpfil;
170 vec[nvec++] = "-dist";
172 vec[nvec++] = "-queued";
175 if ((child_id = fork()) == 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, *vec))) {
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",
189 done(status ? 1 : 0);
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);