3 * mhmail.c -- simple mail program
12 static struct swit switches[] = {
16 { "cc addrs ...", 0 },
20 { "subject text", 0 },
32 static char tmpfil[BUFSIZ];
37 static RETSIGTYPE intrser (int);
41 main (int argc, char **argv)
44 int status, i, iscc = 0, nvec;
45 int queued = 0, resent = 0, somebody;
46 char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
47 char *from = NULL, *body = NULL, **argp, **arguments;
48 char *vec[5], buf[BUFSIZ];
52 setlocale(LC_ALL, "");
54 invo_name = r1bindex (argv[0], '/');
56 /* foil search of user profile/context */
57 if (context_foil (NULL) == -1)
60 /* If no arguments, just incorporate new mail */
62 execlp (incproc, r1bindex (incproc, '/'), NULL);
63 adios (incproc, "unable to exec");
66 arguments = getarguments (invo_name, argc, argv, 0);
69 while ((cp = *argp++)) {
71 switch (smatch (++cp, switches)) {
73 ambigsw (cp, switches);
76 adios (NULL, "-%s unknown", cp);
79 snprintf (buf, sizeof(buf), "%s [addrs ... [switches]]",
81 print_help (buf, switches, 0);
84 print_version(invo_name);
88 if (!(from = *argp++) || *from == '-')
89 adios (NULL, "missing argument to %s", argp[-2]);
93 if (!(body = *argp++) || *body == '-')
94 adios (NULL, "missing argument to %s", argp[-2]);
102 if (!(subject = *argp++) || *subject == '-')
103 adios (NULL, "missing argument to %s", argp[-2]);
116 cclist = cclist ? add (cp, add (", ", cclist)) : getcpy (cp);
118 tolist = tolist ? add (cp, add (", ", tolist)) : getcpy (cp);
122 adios (NULL, "usage: %s addrs ... [switches]", invo_name);
123 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
124 if ((out = fopen (tmpfil, "w")) == NULL)
125 adios (tmpfil, "unable to write");
126 chmod (tmpfil, 0600);
128 SIGNAL2 (SIGINT, intrser);
130 fprintf (out, "%sTo: %s\n", resent ? "Resent-" : "", tolist);
132 fprintf (out, "%scc: %s\n", resent ? "Resent-" : "", cclist);
134 fprintf (out, "%sSubject: %s\n", resent ? "Resent-" : "", subject);
136 fprintf (out, "%sFrom: %s\n", resent ? "Resent-" : "", from);
141 fprintf (out, "%s", body);
142 if (*body && *(body + strlen (body) - 1) != '\n')
146 (i = fread (buf, sizeof(*buf), sizeof(buf), stdin)) > 0;
148 if (fwrite (buf, sizeof(*buf), i, out) != i)
149 adios (tmpfil, "error writing");
158 vec[nvec++] = r1bindex (postproc, '/');
159 vec[nvec++] = tmpfil;
161 vec[nvec++] = "-dist";
163 vec[nvec++] = "-queued";
166 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
169 if (child_id == NOTOK) {
170 /* report failure and then send it */
171 admonish (NULL, "unable to fork");
172 } else if (child_id) {
174 if ((status = pidXwait(child_id, postproc))) {
175 fprintf (stderr, "Letter saved in dead.letter\n");
176 execl ("/bin/mv", "mv", tmpfil, "dead.letter", NULL);
177 execl ("/usr/bin/mv", "mv", tmpfil, "dead.letter", NULL);
182 done (status ? 1 : 0);
185 execvp (postproc, vec);
186 fprintf (stderr, "unable to exec ");
191 return 0; /* dead code to satisfy the compiler */
198 #ifndef RELIABLE_SIGNALS
204 done (i != 0 ? 1 : 0);