#include <h/utils.h>
#include <signal.h>
#include <fcntl.h>
-
+#include <unistd.h>
+#include <sys/stat.h>
+#include <locale.h>
+#include <sysexits.h>
static struct swit switches[] = {
#define BODYSW 0
{ NULL, 0 }
};
+char *version=VERSION;
+
static char tmpfil[BUFSIZ];
/*
int
main(int argc, char **argv)
{
- pid_t child_id;
int status, iscc = 0, nvec;
char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
char *from = NULL, *body = NULL, **argp, **arguments;
/* Without arguments, exit. */
if (argc == 1) {
- adios(NULL, "no interactive mail shell. Use inc/scan/show instead.");
+ adios(EX_USAGE, NULL, "no interactive mail shell. Use inc/scan/show instead.");
}
context_read();
switch (smatch(++cp, switches)) {
case AMBIGSW:
ambigsw(cp, switches);
- /* sysexits.h EX_USAGE */
- exit(1);
+ exit(EX_USAGE);
case UNKWNSW:
- adios(NULL, "-%s unknown", cp);
+ adios(EX_USAGE, NULL, "-%s unknown", cp);
case HELPSW:
snprintf(buf, sizeof(buf),
"%s addrs... [switches]",
invo_name);
print_help(buf, switches, 0);
- exit(0);
+ exit(argc == 2 ? EX_OK : EX_USAGE);
case VERSIONSW:
print_version(invo_name);
- exit(0);
+ exit(argc == 2 ? EX_OK : EX_USAGE);
case FROMSW:
if (!(from = *argp++) || *from == '-')
- adios(NULL, "missing argument to %s",
+ adios(EX_USAGE, NULL, "missing argument to %s",
argp[-2]);
continue;
case BODYSW:
if (!(body = *argp++) || *body == '-')
- adios(NULL, "missing argument to %s",
+ adios(EX_USAGE, NULL, "missing argument to %s",
argp[-2]);
continue;
case SUBJSW:
if (!(subject = *argp++) || *subject == '-')
- adios(NULL, "missing argument to %s",
+ adios(EX_USAGE, NULL, "missing argument to %s",
argp[-2]);
continue;
}
}
if (iscc)
cclist = cclist ? add(cp, add(", ", cclist)) :
- getcpy(cp);
+ mh_xstrdup(cp);
else
tolist = tolist ? add(cp, add(", ", tolist)) :
- getcpy(cp);
+ mh_xstrdup(cp);
}
if (tolist == NULL)
- adios(NULL, "usage: %s addrs ... [switches]", invo_name);
+ adios(EX_USAGE, NULL, "usage: %s addrs ... [switches]", invo_name);
tfile = m_mktemp2("/tmp/", invo_name, NULL, &out);
if (tfile == NULL)
- adios("mhmail", "unable to create temporary file");
+ adios(EX_CANTCREAT, "mhmail", "unable to create temporary file");
chmod(tfile, 0600);
strncpy(tmpfil, tfile, sizeof(tmpfil));
}
empty = 0;
if (fputs(buf, out) == EOF) {
- adios(tmpfil, "error writing");
+ adios(EX_IOERR, tmpfil, "error writing");
}
}
if (empty) {
unlink(tmpfil);
- adios(NULL, "not sending message with empty body");
+ adios(EX_DATAERR, NULL, "not sending message with empty body");
}
}
fclose(out);
vec[nvec++] = tmpfil;
vec[nvec] = NULL;
- if ((child_id = fork()) == NOTOK) {
- /* report failure and then send it */
- adios(NULL, "unable to fork");
+ if ((status = execprog(*vec, vec))) {
+ /* spost failed, save draft as dead.letter */
+ int in, out;
- } else if (child_id == 0) {
- /* child process */
- execvp(*vec, vec);
- fprintf(stderr, "unable to exec ");
- perror(*vec);
- _exit(-1);
-
- } else {
- /* parent process */
- if ((status = pidXwait(child_id, *vec))) {
- /* spost failed, save draft as dead.letter */
- int in, out;
-
- in = open(tmpfil, O_RDONLY);
- out = creat("dead.letter", 0600);
- if (in == -1 || out == -1) {
- fprintf(stderr, "Letter left at %s.\n",
- tmpfil);
- /* sysexits.h exit-status from spost */
- exit(status ? 1 : 0);
- }
- cpydata(in, out, tmpfil, "dead.letter");
- close(in);
- close(out);
- fprintf(stderr, "Letter saved in dead.letter\n");
+ in = open(tmpfil, O_RDONLY);
+ out = creat("dead.letter", 0600);
+ if (in == -1 || out == -1) {
+ fprintf(stderr, "Letter left at %s.\n",
+ tmpfil);
+ exit(status);
}
- unlink(tmpfil);
- /* sysexits.h exit status from spost */
- exit(status ? 1 : 0);
+ cpydata(in, out, tmpfil, "dead.letter");
+ close(in);
+ close(out);
+ fprintf(stderr, "Letter saved in dead.letter\n");
}
-
- return 0; /* dead code to satisfy the compiler */
+ unlink(tmpfil);
+ exit(status);
}
intrser(int i)
{
unlink(tmpfil);
- exit(i != 0 ? 1 : 0);
+ _exit(EX_IOERR);
}