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>
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];
57 setlocale(LC_ALL, "");
59 invo_name = mhbasename(argv[0]);
61 /* foil search of user profile/context */
62 if (context_foil(NULL) == -1)
65 /* Without arguments, exit. */
67 adios(NULL, "no interactive mail shell. Use inc/scan/show instead.");
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),
84 "%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",
99 if (!(body = *argp++) || *body == '-')
100 adios(NULL, "missing argument to %s",
109 if (!(subject = *argp++) || *subject == '-')
110 adios(NULL, "missing argument to %s",
124 cclist = cclist ? add(cp, add(", ", cclist)) :
127 tolist = tolist ? add(cp, add(", ", tolist)) :
132 adios(NULL, "usage: %s addrs ... [switches]", invo_name);
134 tfile = m_mktemp2("/tmp/", invo_name, NULL, &out);
136 adios("mhmail", "unable to create temporary file");
138 strncpy(tmpfil, tfile, sizeof(tmpfil));
140 SIGNAL2(SIGINT, intrser);
142 fprintf(out, "%sTo: %s\n", resent ? "Resent-" : "", tolist);
144 fprintf(out, "%sCc: %s\n", resent ? "Resent-" : "", cclist);
146 fprintf(out, "%sSubject: %s\n", resent ? "Resent-" : "", subject);
148 fprintf(out, "%sFrom: %s\n", resent ? "Resent-" : "", from);
153 fprintf(out, "%s", body);
154 if (*body && *(body + strlen(body) - 1) != '\n')
157 for (somebody = 0; (i = fread(buf, sizeof(*buf), sizeof(buf),
158 stdin)) > 0; somebody++)
159 if (fwrite(buf, sizeof(*buf), i, out) != i)
160 adios(tmpfil, "error writing");
169 vec[nvec++] = "spost";
170 vec[nvec++] = tmpfil;
172 vec[nvec++] = "-dist";
174 vec[nvec++] = "-queued";
177 if ((child_id = fork()) == NOTOK) {
178 /* report failure and then send it */
179 adios(NULL, "unable to fork");
181 } else if (child_id == 0) {
184 fprintf(stderr, "unable to exec ");
190 if ((status = pidXwait(child_id, *vec))) {
191 /* spost failed, save draft as dead.letter */
194 in = open(tmpfil, O_RDONLY);
195 out = creat("dead.letter", 0600);
196 if (in == -1 || out == -1) {
197 fprintf(stderr, "Letter left at %s.\n",
199 done(status ? 1 : 0);
201 cpydata(in, out, tmpfil, "dead.letter");
204 fprintf(stderr, "Letter saved in dead.letter\n");
207 done(status ? 1 : 0);
210 return 0; /* dead code to satisfy the compiler */
217 #ifndef RELIABLE_SIGNALS
223 done(i != 0 ? 1 : 0);