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 },
32 static char tmpfil[BUFSIZ];
37 static void intrser(int);
41 main(int argc, char **argv)
44 int status, iscc = 0, nvec;
47 char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
48 char *from = NULL, *body = NULL, **argp, **arguments;
49 char *vec[5], buf[BUFSIZ];
54 setlocale(LC_ALL, "");
56 invo_name = mhbasename(argv[0]);
58 /* foil search of user profile/context */
59 if (context_foil(NULL) == -1)
62 /* Without arguments, exit. */
64 adios(NULL, "no interactive mail shell. Use inc/scan/show instead.");
67 arguments = getarguments(invo_name, argc, argv, 0);
70 while ((cp = *argp++)) {
72 switch (smatch(++cp, switches)) {
74 ambigsw(cp, switches);
77 adios(NULL, "-%s unknown", cp);
80 snprintf(buf, sizeof(buf),
81 "%s addrs... [switches]",
83 print_help(buf, switches, 0);
86 print_version(invo_name);
90 if (!(from = *argp++) || *from == '-')
91 adios(NULL, "missing argument to %s",
96 if (!(body = *argp++) || *body == '-')
97 adios(NULL, "missing argument to %s",
106 if (!(subject = *argp++) || *subject == '-')
107 adios(NULL, "missing argument to %s",
113 cclist = cclist ? add(cp, add(", ", cclist)) :
116 tolist = tolist ? add(cp, add(", ", tolist)) :
121 adios(NULL, "usage: %s addrs ... [switches]", invo_name);
123 tfile = m_mktemp2("/tmp/", invo_name, NULL, &out);
125 adios("mhmail", "unable to create temporary file");
127 strncpy(tmpfil, tfile, sizeof(tmpfil));
129 SIGNAL2(SIGINT, intrser);
131 fprintf(out, "To: %s\n", tolist);
133 fprintf(out, "Cc: %s\n", cclist);
135 fprintf(out, "Subject: %s\n", subject);
137 fprintf(out, "From: %s\n", from);
141 fprintf(out, "%s", body);
142 if (*body && *(body + strlen(body) - 1) != '\n')
145 for (somebody = 0; (i = fread(buf, sizeof(*buf), sizeof(buf),
146 stdin)) > 0; somebody++)
147 if (fwrite(buf, sizeof(*buf), i, out) != i)
148 adios(tmpfil, "error writing");
157 vec[nvec++] = "spost";
158 vec[nvec++] = tmpfil;
161 if ((child_id = fork()) == NOTOK) {
162 /* report failure and then send it */
163 adios(NULL, "unable to fork");
165 } else if (child_id == 0) {
168 fprintf(stderr, "unable to exec ");
174 if ((status = pidXwait(child_id, *vec))) {
175 /* spost failed, save draft as dead.letter */
178 in = open(tmpfil, O_RDONLY);
179 out = creat("dead.letter", 0600);
180 if (in == -1 || out == -1) {
181 fprintf(stderr, "Letter left at %s.\n",
183 done(status ? 1 : 0);
185 cpydata(in, out, tmpfil, "dead.letter");
188 fprintf(stderr, "Letter saved in dead.letter\n");
191 done(status ? 1 : 0);
194 return 0; /* dead code to satisfy the compiler */
202 done(i != 0 ? 1 : 0);