Use sysexits.h for better exit-codes
[mmh] / uip / rcvdist.c
index 4e052cc..4027ea2 100644 (file)
@@ -14,6 +14,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 #include <locale.h>
 #include <unistd.h>
 #include <sys/stat.h>
 #include <locale.h>
+#include <sysexits.h>
 
 static struct swit switches[] = {
 #define FORMSW  0
 
 static struct swit switches[] = {
 #define FORMSW  0
@@ -46,7 +47,7 @@ main(int argc, char **argv)
        char *tfile = NULL;
 
        if (atexit(unlink_done) != 0) {
        char *tfile = NULL;
 
        if (atexit(unlink_done) != 0) {
-               adios(NULL, "atexit failed");
+               adios(EX_OSERR, NULL, "atexit failed");
        }
 
        setlocale(LC_ALL, "");
        }
 
        setlocale(LC_ALL, "");
@@ -63,7 +64,7 @@ main(int argc, char **argv)
                        switch (smatch(++cp, switches)) {
                        case AMBIGSW:
                                ambigsw(cp, switches);
                        switch (smatch(++cp, switches)) {
                        case AMBIGSW:
                                ambigsw(cp, switches);
-                               exit(1);
+                               exit(EX_USAGE);
                        case UNKWNSW:
                                vec[vecp++] = --cp;
                                continue;
                        case UNKWNSW:
                                vec[vecp++] = --cp;
                                continue;
@@ -71,14 +72,14 @@ main(int argc, char **argv)
                        case HELPSW:
                                snprintf(buf, sizeof(buf), "%s [switches] [switches for spost] address ...", invo_name);
                                print_help(buf, switches, 1);
                        case HELPSW:
                                snprintf(buf, sizeof(buf), "%s [switches] [switches for spost] address ...", invo_name);
                                print_help(buf, switches, 1);
-                               exit(0);
+                               exit(argc == 2 ? EX_OK : EX_USAGE);
                        case VERSIONSW:
                                print_version(invo_name);
                        case VERSIONSW:
                                print_version(invo_name);
-                               exit(0);
+                               exit(argc == 2 ? EX_OK : EX_USAGE);
 
                        case FORMSW:
                                if (!(form = *argp++) || *form == '-') {
 
                        case FORMSW:
                                if (!(form = *argp++) || *form == '-') {
-                                       adios(NULL, "missing argument to %s",
+                                       adios(EX_USAGE, NULL, "missing argument to %s",
                                                        argp[-2]);
                                }
                                continue;
                                                        argp[-2]);
                                }
                                continue;
@@ -88,28 +89,27 @@ main(int argc, char **argv)
        }
 
        if (!addrs) {
        }
 
        if (!addrs) {
-               adios(NULL, "usage: %s [switches] [switches for spost] address ...", invo_name);
+               adios(EX_USAGE, NULL, "usage: %s [switches] [switches for spost] address ...", invo_name);
        }
 
        umask(~m_gmprot());
 
        tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
        }
 
        umask(~m_gmprot());
 
        tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
-       if (tfile == NULL) adios("rcvdist", "unable to create temporary file");
+       if (tfile == NULL) adios(EX_CANTCREAT, "rcvdist", "unable to create temporary file");
        strncpy(tmpfil, tfile, sizeof(tmpfil));
 
        cpydata(fileno(stdin), fileno(fp), "message", tmpfil);
        fseek(fp, 0L, SEEK_SET);
 
        tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
        strncpy(tmpfil, tfile, sizeof(tmpfil));
 
        cpydata(fileno(stdin), fileno(fp), "message", tmpfil);
        fseek(fp, 0L, SEEK_SET);
 
        tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
-       if (tfile == NULL) adios("forw", "unable to create temporary file");
+       if (tfile == NULL) adios(EX_CANTCREAT, "forw", "unable to create temporary file");
        strncpy(drft, tfile, sizeof(tmpfil));
 
        rcvdistout(fp, form, addrs);
        fclose(fp);
 
        if (distout(drft, tmpfil, backup) == NOTOK) {
        strncpy(drft, tfile, sizeof(tmpfil));
 
        rcvdistout(fp, form, addrs);
        fclose(fp);
 
        if (distout(drft, tmpfil, backup) == NOTOK) {
-               /* sysexits.h EX_DATAERR */
-               exit(1);
+               exit(EX_IOERR);
        }
 
        vec[0] = "spost";
        }
 
        vec[0] = "spost";
@@ -120,7 +120,7 @@ main(int argc, char **argv)
        execvp(*vec, vec);
        fprintf(stderr, "unable to exec ");
        perror(*vec);
        execvp(*vec, vec);
        fprintf(stderr, "unable to exec ");
        perror(*vec);
-       _exit(1);
+       _exit(EX_OSERR);
        return 0;  /* dead code to satisfy the compiler */
 }
 
        return 0;  /* dead code to satisfy the compiler */
 }
 
@@ -163,7 +163,7 @@ rcvdistout(FILE *inb, char *form, char *addrs)
        FILE *out;
 
        if (!(out = fopen(drft, "w"))) {
        FILE *out;
 
        if (!(out = fopen(drft, "w"))) {
-               adios(drft, "unable to create");
+               adios(EX_CANTCREAT, drft, "unable to create");
        }
 
        /* get new format string */
        }
 
        /* get new format string */
@@ -172,12 +172,12 @@ rcvdistout(FILE *inb, char *form, char *addrs)
        ncomps = fmt_compile(cp, &fmt) + 1;
        if (!(nxtbuf = compbuffers =
                        (char **) calloc((size_t) ncomps, sizeof(char *)))) {
        ncomps = fmt_compile(cp, &fmt) + 1;
        if (!(nxtbuf = compbuffers =
                        (char **) calloc((size_t) ncomps, sizeof(char *)))) {
-               adios(NULL, "unable to allocate component buffers");
+               adios(EX_OSERR, NULL, "unable to allocate component buffers");
        }
        if (!(savecomp = used_buf =
                        (struct comp **) calloc((size_t) (ncomps + 1),
                        sizeof(struct comp *)))) {
        }
        if (!(savecomp = used_buf =
                        (struct comp **) calloc((size_t) (ncomps + 1),
                        sizeof(struct comp *)))) {
-               adios(NULL, "unable to allocate component buffer stack");
+               adios(EX_OSERR, NULL, "unable to allocate component buffer stack");
        }
        savecomp += ncomps + 1;
        *--savecomp = 0;
        }
        savecomp += ncomps + 1;
        *--savecomp = 0;
@@ -250,7 +250,7 @@ rcvdistout(FILE *inb, char *form, char *addrs)
                        goto finished;
 
                default:
                        goto finished;
 
                default:
-                       adios(NULL, "m_getfld() returned %d", state);
+                       adios(EX_SOFTWARE, NULL, "m_getfld() returned %d", state);
                }
        }
 finished: ;
                }
        }
 finished: ;
@@ -263,7 +263,7 @@ finished: ;
        fputs(scanl, out);
 
        if (ferror(out)) {
        fputs(scanl, out);
 
        if (ferror(out)) {
-               adios(drft, "error writing");
+               adios(EX_IOERR, drft, "error writing");
        }
        fclose(out);
 
        }
        fclose(out);