Fix uip/whom.c for C89 compatibility
[mmh] / uip / burst.c
index 8d6e69b..12d1f9d 100644 (file)
@@ -7,12 +7,17 @@
 */
 
 #include <h/mh.h>
+#include <h/utils.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <locale.h>
+#include <sysexits.h>
 
 static struct swit switches[] = {
 #define VERBSW 0
        { "verbose", 0 },
 #define NVERBSW 1
-       { "noverbose", 0 },
+       { "noverbose", 2 },
 #define VERSIONSW 2
        { "Version", 0 },
 #define HELPSW 3
@@ -20,6 +25,8 @@ static struct swit switches[] = {
        { NULL, 0 }
 };
 
+char *version=VERSION;
+
 static char delim3[] = "-------";
 
 struct smsg {
@@ -45,9 +52,7 @@ main(int argc, char **argv)
        struct smsg *smsgs;
        struct msgs *mp;
 
-#ifdef LOCALE
        setlocale(LC_ALL, "");
-#endif
        invo_name = mhbasename(argv[0]);
 
        /* read user profile/context */
@@ -61,17 +66,17 @@ main(int argc, char **argv)
                        switch (smatch(++cp, switches)) {
                        case AMBIGSW:
                                ambigsw(cp, switches);
-                               done(1);
+                               exit(EX_USAGE);
                        case UNKWNSW:
-                               adios(NULL, "-%s unknown\n", cp);
+                               adios(EX_USAGE, NULL, "-%s unknown\n", cp);
 
                        case HELPSW:
                                snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", 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 VERBSW:
                                verbosw++;
@@ -83,9 +88,9 @@ main(int argc, char **argv)
                }
                if (*cp == '+' || *cp == '@') {
                        if (folder)
-                               adios(NULL, "only one folder at a time!");
+                               adios(EX_USAGE, NULL, "only one folder at a time!");
                        else
-                               folder = getcpy(expandfol(cp));
+                               folder = mh_xstrdup(expandfol(cp));
                } else {
                        msgs[msgp++] = cp;
                }
@@ -98,26 +103,23 @@ main(int argc, char **argv)
        maildir = toabsdir(folder);
 
        if (chdir(maildir) == NOTOK)
-               adios(maildir, "unable to change directory to");
+               adios(EX_SOFTWARE, maildir, "unable to change directory to");
 
        /* read folder and create message structure */
        if (!(mp = folder_read(folder)))
-               adios(NULL, "unable to read folder %s", folder);
+               adios(EX_IOERR, NULL, "unable to read folder %s", folder);
 
        /* check for empty folder */
        if (mp->nummsg == 0)
-               adios(NULL, "no messages in %s", folder);
+               adios(EX_DATAERR, NULL, "no messages in %s", folder);
 
        /* parse all the message ranges/sequences and set SELECTED */
        for (msgnum = 0; msgnum < msgp; msgnum++)
                if (!m_convert(mp, msgs[msgnum]))
-                       done(1);
+                       exit(EX_SOFTWARE);
        seq_setprev(mp);  /* set the previous-sequence */
 
-       smsgs = (struct smsg *)
-               calloc((size_t) (MAXFOLDER + 2), sizeof(*smsgs));
-       if (smsgs == NULL)
-               adios(NULL, "unable to allocate burst storage");
+       smsgs = mh_xcalloc(MAXFOLDER + 2, sizeof(*smsgs));
 
        hi = mp->hghmsg + 1;
 
@@ -131,12 +133,12 @@ main(int argc, char **argv)
                        } else if (numburst == 0) {
                                admonish(NULL, "message %d not in digest format", msgnum);
                        } else {
-                               adios(NULL, "burst() botch -- you lose big");
+                               adios(EX_SOFTWARE, NULL, "burst() botch -- you lose big");
                        }
                }
        }
 
-       free(smsgs);
+       mh_free0(&smsgs);
        context_replace(curfolder, folder);
 
        /*
@@ -150,8 +152,7 @@ main(int argc, char **argv)
        seq_save(mp);
        context_save();
        folder_free(mp);
-       done(0);
-       return 1;
+       return 0;
 }
 
 
@@ -172,7 +173,7 @@ find_delim(int msgnum, struct smsg *smsgs)
        ld3 = strlen(delim3);
 
        if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
-               adios(msgnam, "unable to read message");
+               adios(EX_IOERR, msgnam, "unable to read message");
 
        for (msgp = 0, pos = 0L; msgp <= MAXFOLDER; msgp++) {
                while (fgets(buffer, sizeof(buffer), in) && buffer[0] == '\n')
@@ -225,7 +226,7 @@ burst(struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
        struct msgs *mp;
 
        if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
-               adios(msgnam, "unable to read message");
+               adios(EX_IOERR, msgnam, "unable to read message");
 
        mode = fstat(fileno(in), &st) != NOTOK ?
                        (int)(st.st_mode & 0777) : m_gmprot();
@@ -237,7 +238,7 @@ burst(struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
        */
        if ((mp->hghmsg + numburst > mp->hghoff) && !(mp = folder_realloc(mp,
                        mp->lowoff, mp->hghmsg + numburst)))
-               adios(NULL, "unable to allocate folder storage");
+               adios(EX_OSERR, NULL, "unable to allocate folder storage");
        *mpp = mp;
 
        /*
@@ -249,7 +250,7 @@ burst(struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
        for (j=1, i=mp->hghmsg+1; j<=numburst; j++, i++) {
                snprintf(destfil, sizeof destfil, "%s/%d", maildir, i);
                if (!(out = fopen(destfil, "w"))) {
-                       adios(destfil, "unable to open");
+                       adios(EX_IOERR, destfil, "unable to open");
                }
                if (verbosw) {
                        printf("message %d of digest %d becomes message %d\n",
@@ -329,7 +330,7 @@ cpybrst(FILE *in, FILE *out, char *ifile, char *ofile, int len)
        }
 
        if (ferror(in) && !feof(in))
-               adios(ifile, "error reading");
+               adios(EX_IOERR, ifile, "error reading");
        if (ferror(out))
-               adios(ofile, "error writing");
+               adios(EX_IOERR, ofile, "error writing");
 }