Fix uip/whom.c for C89 compatibility
[mmh] / uip / anno.c
index 0483dff..33d2b49 100644 (file)
 #include <fcntl.h>
 #include <errno.h>
 #include <utime.h>
+#include <unistd.h>
+#include <ctype.h>
+#include <sys/stat.h>
+#include <locale.h>
+#include <sysexits.h>
 
 static enum { MODE_ADD, MODE_DEL, MODE_LIST } mode = MODE_ADD;
 
@@ -48,6 +53,8 @@ static struct swit switches[] = {
        { NULL, 0 }
 };
 
+char *version=VERSION;
+
 /*
 ** static prototypes
 */
@@ -86,20 +93,19 @@ main(int argc, char **argv)
                        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 [+folder] [msgs] [switches]",
                                        invo_name);
                                print_help(buf, switches, 1);
-                               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 DELETESW:  /* delete annotations */
                                mode = MODE_DEL;
@@ -111,23 +117,23 @@ main(int argc, char **argv)
 
                        case COMPSW:
                                if (comp)
-                                       adios(NULL, "only one component at a time!");
+                                       adios(EX_USAGE, NULL, "only one component at a time!");
                                if (!(comp = *argp++) || *comp == '-')
-                                       adios(NULL, "missing argument to %s",
+                                       adios(EX_USAGE, NULL, "missing argument to %s",
                                                        argp[-2]);
                                continue;
 
                        case TEXTSW:
                                if (text)
-                                       adios(NULL, "only one body at a time!");
+                                       adios(EX_USAGE, NULL, "only one body at a time!");
                                if (!(text = *argp++) || *text == '-')
-                                       adios(NULL, "missing argument to %s",
+                                       adios(EX_USAGE, NULL, "missing argument to %s",
                                                        argp[-2]);
                                continue;
 
                        case NUMBERSW: /* number listing or delete by number */
                                if (mode == MODE_ADD) {
-                                       adios(NULL, "-number switch must appear after -list or -delete, only.");
+                                       adios(EX_USAGE, NULL, "-number switch must appear after -list or -delete, only.");
                                }
                                if (mode == MODE_LIST) {
                                        number = 1;
@@ -135,7 +141,7 @@ main(int argc, char **argv)
                                }
                                /* MODE_DEL */
                                if (number) {
-                                       adios(NULL, "only one number at a time!");
+                                       adios(EX_USAGE, NULL, "only one number at a time!");
                                }
                                if (*argp && strcmp(*argp, "all")==0) {
                                        number = -1;
@@ -143,11 +149,11 @@ main(int argc, char **argv)
                                        continue;
                                }
                                if (!*argp || !(number = atoi(*argp))) {
-                                       adios(NULL, "missing argument to %s",
+                                       adios(EX_USAGE, NULL, "missing argument to %s",
                                                        argp[-1]);
                                }
                                if (number < 0) {
-                                       adios(NULL, "invalid number (%d).",
+                                       adios(EX_USAGE, NULL, "invalid number (%d).",
                                                        number);
                                }
                                argp++;
@@ -175,12 +181,12 @@ 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 if (*cp == '/' || *cp == '.') {
                        if (file)
-                               adios(NULL, "only one file at a time!");
+                               adios(EX_USAGE, NULL, "only one file at a time!");
                        file = cp;
                } else {
                        app_msgarg(&msgs, cp);
@@ -188,13 +194,13 @@ main(int argc, char **argv)
        }
 
        if (file && (folder || msgs.size)) {
-               adios(NULL, "Don't intermix files and messages.");
+               adios(EX_USAGE, NULL, "Don't intermix files and messages.");
        }
        if (!datesw && !text) {
-               adios(NULL, "-nodate without -text is a no-op.");
+               adios(EX_USAGE, NULL, "-nodate without -text is a no-op.");
        }
        if (number && text) {
-               adios(NULL, "Don't combine -number with -text.");
+               adios(EX_USAGE, NULL, "Don't combine -number with -text.");
        }
 
        if (file) {
@@ -203,7 +209,7 @@ main(int argc, char **argv)
                else
                        annotate(file, comp, text, datesw, number,
                                        append, preserve);
-               exit(0);
+               exit(EX_OK);
        }
 
        if (!msgs.size)
@@ -213,21 +219,20 @@ main(int argc, char **argv)
        maildir = toabsdir(folder);
 
        if (chdir(maildir) == NOTOK)
-               adios(maildir, "unable to change directory to");
+               adios(EX_OSERR, 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 < msgs.size; msgnum++) {
                if (!m_convert(mp, msgs.msgs[msgnum])) {
-                       /* sysexits.h EX_USAGE */
-                       exit(1);
+                       exit(EX_SOFTWARE);
                }
        }
 
@@ -261,8 +266,7 @@ make_comp(unsigned char **ap)
                fflush(stdout);
 
                if (!fgets(buffer, sizeof buffer, stdin)) {
-                       /* sysexits.h EX_IOERR */
-                       exit(1);
+                       exit(EX_IOERR);
                }
                *ap = trimcpy(buffer);
        }
@@ -270,15 +274,15 @@ make_comp(unsigned char **ap)
        if ((cp = *ap + strlen(*ap) - 1) > *ap && *cp == ':')
                *cp = '\0';
        if (strlen(*ap) == 0)
-               adios(NULL, "null component name");
+               adios(EX_SOFTWARE, NULL, "null component name");
        if (**ap == '-')
-               adios(NULL, "invalid component name %s", *ap);
+               adios(EX_CONFIG, NULL, "invalid component name %s", *ap);
        if (strlen(*ap) >= NAMESZ)
-               adios(NULL, "too large component name %s", *ap);
+               adios(EX_SOFTWARE, NULL, "too large component name %s", *ap);
 
        for (cp = *ap; *cp; cp++)
                if (!isalnum(*cp) && *cp != '-')
-                       adios(NULL, "invalid component name %s", *ap);
+                       adios(EX_CONFIG, NULL, "invalid component name %s", *ap);
 }
 
 
@@ -299,11 +303,11 @@ annolist(char *file, unsigned char *comp, int number)
        int n;  /* number of bytes written */
 
        if ((fp = fopen(file, "r")) == NULL) {
-               adios(file, "unable to open");
+               adios(EX_IOERR, file, "unable to open");
        }
 
        /* We'll grow this buffer as needed. */
-       field = (char *)mh_xmalloc(field_size = 256);
+       field = mh_xcalloc(field_size = 256, sizeof(char));
 
        make_comp(&comp);
        length = strlen(comp); /* Convenience copy. */
@@ -323,8 +327,7 @@ annolist(char *file, unsigned char *comp, int number)
                                break;
                        }
                        if (++n >= field_size - 1) {
-                               field = (char *)mh_xrealloc(field,
-                                               field_size += 256);
+                               field = mh_xrealloc(field, field_size += 256);
                                cp = field + n - 1;
                        }
                }
@@ -341,7 +344,7 @@ annolist(char *file, unsigned char *comp, int number)
 
        } while (*field && *field != '-');
 
-       free(field);
+       mh_free0(&field);
        fclose(fp);
 
        return;
@@ -398,7 +401,7 @@ annotate(char *file, unsigned char *comp, char *text, int datesw,
        fclose(tmp);
 
        if ((tmpfd = open(tmpfil, O_RDONLY)) == NOTOK) {
-               adios(tmpfil, "unable to open for re-reading");
+               adios(EX_IOERR, tmpfil, "unable to open for re-reading");
        }
        lseek(fd, (off_t) 0, SEEK_SET);
 
@@ -407,7 +410,7 @@ annotate(char *file, unsigned char *comp, char *text, int datesw,
        **  so the file has to be truncated or it will contain garbage.
        */
        if (mode == MODE_DEL && ftruncate(fd, 0) == -1) {
-               adios(tmpfil, "unable to truncate.");
+               adios(EX_IOERR, tmpfil, "unable to truncate.");
        }
        cpydata(tmpfd, fd, tmpfil, file);
        close(tmpfd);
@@ -455,9 +458,9 @@ dodel(int fd, unsigned char *comp, char *text, FILE *tmp, int number)
        ** This buffer is grown as needed later.
        */
        if ((fp = fdopen(fd, "r")) == NULL) {
-               adios(NULL, "unable to fdopen file.");
+               adios(EX_IOERR, NULL, "unable to fdopen file.");
        }
-       field = (char *)mh_xmalloc(field_size);
+       field = mh_xcalloc(field_size, sizeof(char));
 
        /*
        **  Copy lines from the input file to the temporary file
@@ -485,8 +488,7 @@ dodel(int fd, unsigned char *comp, char *text, FILE *tmp, int number)
                        }
 
                        if (++n >= field_size - 1) {
-                               field = (char *) mh_xrealloc(field,
-                                               field_size *= 2);
+                               field = mh_xrealloc(field, field_size *= 2);
                                cp = field + n - 1;
                        }
                }
@@ -537,12 +539,12 @@ dodel(int fd, unsigned char *comp, char *text, FILE *tmp, int number)
                /* Copy it. */
                if ((n = fputs(field, tmp)) == EOF ||
                                (c=='\n' && fputc('\n', tmp)==EOF)) {
-                       adios(NULL, "unable to write temporary file.");
+                       adios(EX_CANTCREAT, NULL, "unable to write temporary file.");
                }
 
        } while (*field && *field != '-');
 
-       free(field);
+       mh_free0(&field);
 
        fflush(tmp);
        fflush(fp); /* The underlying fd will be closed by lkclose() */
@@ -554,7 +556,7 @@ dodel(int fd, unsigned char *comp, char *text, FILE *tmp, int number)
        ** not the pointer.
        */
        if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1) {
-               adios(NULL, "can't seek.");
+               adios(EX_IOERR, NULL, "can't seek.");
        }
 }
 
@@ -578,7 +580,7 @@ doadd(int fd, unsigned char *comp, char *text, FILE *tmp, int datesw,
                ** read in.  This buffer is grown as needed later.
                */
                if ((fp = fdopen(fd, "r")) == NULL) {
-                       adios(NULL, "unable to fdopen file.");
+                       adios(EX_IOERR, NULL, "unable to fdopen file.");
                }
                /* Find the end of the headers. */
                if ((c = getc(fp)) == '\n') {
@@ -636,7 +638,7 @@ doadd(int fd, unsigned char *comp, char *text, FILE *tmp, int datesw,
        */
        if (append) {
                if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1) {
-                       adios(NULL, "can't seek.");
+                       adios(EX_IOERR, NULL, "can't seek.");
                }
        }
 }