Replace mh_xmalloc() with mh_xcalloc()
[mmh] / uip / rcvdist.c
index 2ba6097..deeccb8 100644 (file)
 #include <h/rcvmail.h>
 #include <h/tws.h>
 #include <h/utils.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <locale.h>
+#include <sysexits.h>
 
 static struct swit switches[] = {
 #define FORMSW  0
@@ -30,7 +34,7 @@ static char tmpfil[BUFSIZ] = "";
 ** prototypes
 */
 static void rcvdistout(FILE *, char *, char *);
-static void unlink_done(int) NORETURN;
+void unlink_done();
 
 
 int
@@ -42,7 +46,9 @@ main(int argc, char **argv)
        FILE *fp;
        char *tfile = NULL;
 
-       done=unlink_done;
+       if (atexit(unlink_done) != 0) {
+               adios(EX_OSERR, NULL, "atexit failed");
+       }
 
        setlocale(LC_ALL, "");
        invo_name = mhbasename(argv[0]);
@@ -58,7 +64,7 @@ main(int argc, char **argv)
                        switch (smatch(++cp, switches)) {
                        case AMBIGSW:
                                ambigsw(cp, switches);
-                               done(1);
+                               exit(EX_USAGE);
                        case UNKWNSW:
                                vec[vecp++] = --cp;
                                continue;
@@ -66,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);
-                               done(1);
+                               exit(argc == 2 ? EX_OK : EX_USAGE);
                        case VERSIONSW:
                                print_version(invo_name);
-                               done(1);
+                               exit(argc == 2 ? EX_OK : EX_USAGE);
 
                        case FORMSW:
                                if (!(form = *argp++) || *form == '-') {
-                                       adios(NULL, "missing argument to %s",
+                                       adios(EX_USAGE, NULL, "missing argument to %s",
                                                        argp[-2]);
                                }
                                continue;
@@ -83,27 +89,27 @@ main(int argc, char **argv)
        }
 
        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);
-       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);
-       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) {
-               done(1);
+               exit(EX_IOERR);
        }
 
        vec[0] = "spost";
@@ -114,7 +120,7 @@ main(int argc, char **argv)
        execvp(*vec, vec);
        fprintf(stderr, "unable to exec ");
        perror(*vec);
-       _exit(1);
+       _exit(EX_OSERR);
        return 0;  /* dead code to satisfy the compiler */
 }
 
@@ -150,34 +156,30 @@ static char *addrcomps[] = {
 static void
 rcvdistout(FILE *inb, char *form, char *addrs)
 {
-       register int char_read = 0, format_len, i, state;
-       register char *tmpbuf, **nxtbuf, **ap;
+       int char_read = 0, format_len, i, state;
+       char *tmpbuf, **nxtbuf, **ap;
        char *cp, *scanl, name[NAMESZ];
-       register struct comp *cptr, **savecomp;
+       struct comp *cptr, **savecomp;
        FILE *out;
 
        if (!(out = fopen(drft, "w"))) {
-               adios(drft, "unable to create");
+               adios(EX_CANTCREAT, drft, "unable to create");
        }
 
        /* get new format string */
        cp = new_fs(form ? form : rcvdistcomps, NULL);
        format_len = strlen(cp);
        ncomps = fmt_compile(cp, &fmt) + 1;
-       if (!(nxtbuf = compbuffers =
-                       (char **) calloc((size_t) ncomps, sizeof(char *)))) {
-               adios(NULL, "unable to allocate component buffers");
-       }
-       if (!(savecomp = used_buf =
-                       (struct comp **) calloc((size_t) (ncomps + 1),
-                       sizeof(struct comp *)))) {
-               adios(NULL, "unable to allocate component buffer stack");
-       }
+       nxtbuf = compbuffers =
+                       (char **) mh_xcalloc((size_t) ncomps, sizeof(char *));
+       savecomp = used_buf =
+                       (struct comp **) mh_xcalloc((size_t) (ncomps + 1),
+                       sizeof(struct comp *));
        savecomp += ncomps + 1;
        *--savecomp = 0;
 
        for (i = ncomps; i--;) {
-               *nxtbuf++ = mh_xmalloc(SBUFSIZ);
+               *nxtbuf++ = mh_xcalloc(SBUFSIZ, sizeof(char));
        }
        nxtbuf = compbuffers;
        tmpbuf = *nxtbuf++;
@@ -244,20 +246,20 @@ rcvdistout(FILE *inb, char *form, char *addrs)
                        goto finished;
 
                default:
-                       adios(NULL, "m_getfld() returned %d", state);
+                       adios(EX_SOFTWARE, NULL, "m_getfld() returned %d", state);
                }
        }
 finished: ;
 
        i = format_len + char_read + 256;
-       scanl = mh_xmalloc((size_t) i + 2);
+       scanl = mh_xcalloc((size_t) i + 2, sizeof(char));
        dat[0] = dat[1] = dat[2] = dat[4] = 0;
        dat[3] = OUTPUTLINELEN;
        fmt_scan(fmt, scanl, i, dat);
        fputs(scanl, out);
 
        if (ferror(out)) {
-               adios(drft, "error writing");
+               adios(EX_IOERR, drft, "error writing");
        }
        fclose(out);
 
@@ -274,8 +276,8 @@ finished: ;
 }
 
 
-static void
-unlink_done(int status)
+void
+unlink_done()
 {
        if (*backup) {
                unlink(backup);
@@ -286,5 +288,4 @@ unlink_done(int status)
        if (*tmpfil) {
                unlink(tmpfil);
        }
-       exit(status ? RCV_MBX : RCV_MOK);
 }