sed: use warn()/err() where appropriate
authormillert <millert@openbsd.org>
Wed, 17 Jul 2024 20:57:15 +0000 (20:57 +0000)
committermillert <millert@openbsd.org>
Wed, 17 Jul 2024 20:57:15 +0000 (20:57 +0000)
Use warn()/err() instead of sed's homegrown warning()/error() for
things other than parser problems.  The warning()/error() functions
display the file and line number in addition to the error message.
This also removes of the COMPILE/FATAL argument to error() since
now all calls to error() are for compilation/parsing issues.
OK op@ espie@

usr.bin/sed/compile.c
usr.bin/sed/defs.h
usr.bin/sed/extern.h
usr.bin/sed/main.c
usr.bin/sed/misc.c
usr.bin/sed/process.c

index f21fd0a..d13ef34 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: compile.c,v 1.52 2024/06/18 00:32:22 millert Exp $    */
+/*     $OpenBSD: compile.c,v 1.53 2024/07/17 20:57:15 millert Exp $    */
 
 /*-
  * Copyright (c) 1992 Diomidis Spinellis.
@@ -37,7 +37,7 @@
 #include <sys/stat.h>
 
 #include <ctype.h>
-#include <errno.h>
+#include <err.h>
 #include <fcntl.h>
 #include <limits.h>
 #include <regex.h>
@@ -153,7 +153,7 @@ compile_stream(struct s_command **link)
        for (;;) {
                if ((p = cu_getline(&lbuf, &bufsize)) == NULL) {
                        if (stack != 0)
-                               error(COMPILE, "unexpected EOF (pending }'s)");
+                               error("unexpected EOF (pending }'s)");
                        return (link);
                }
 
@@ -193,17 +193,16 @@ semicolon:        EATSPACE();
 
 nonsel:                /* Now parse the command */
                if (!*p)
-                       error(COMPILE, "command expected");
+                       error("command expected");
                cmd->code = *p;
                for (fp = cmd_fmts; fp->code; fp++)
                        if (fp->code == *p)
                                break;
                if (!fp->code)
-                       error(COMPILE, "invalid command code %c", *p);
+                       error("invalid command code %c", *p);
                if (naddr > fp->naddr)
-                       error(COMPILE,
-                           "command %c expects up to %d address(es), found %d",
-                           *p, fp->naddr, naddr);
+                       error("command %c expects up to %d address(es),"
+                           " found %d", *p, fp->naddr, naddr);
                switch (fp->args) {
                case NONSEL:                    /* ! */
                        p++;
@@ -226,7 +225,7 @@ nonsel:             /* Now parse the command */
                         */
                        cmd->nonsel = 1;
                        if (stack == 0)
-                               error(COMPILE, "unexpected }");
+                               error("unexpected }");
                        cmd2 = stack;
                        stack = cmd2->next;
                        cmd2->next = cmd;
@@ -240,19 +239,19 @@ nonsel:           /* Now parse the command */
                                goto semicolon;
                        }
                        if (*p)
-                               error(COMPILE,
-"extra characters at the end of %c command", cmd->code);
+                               error("extra characters at the end of %c"
+                                   " command", cmd->code);
                        break;
                case TEXT:                      /* a c i */
                        p++;
                        EATSPACE();
                        if (*p != '\\')
-                               error(COMPILE, "command %c expects \\ followed by"
-                                   " text", cmd->code);
+                               error("command %c expects \\ followed by text",
+                                   cmd->code);
                        p++;
                        EATSPACE();
                        if (*p)
-                               error(COMPILE, "extra characters after \\ at the"
+                               error("extra characters after \\ at the"
                                    " end of %c command", cmd->code);
                        cmd->t = compile_text();
                        break;
@@ -262,7 +261,7 @@ nonsel:             /* Now parse the command */
                        p++;
                        EATSPACE();
                        if (*p == '\0')
-                               error(COMPILE, "filename expected");
+                               error("filename expected");
                        cmd->t = duptoeol(p, "w command", NULL);
                        if (aflag) {
                                cmd->u.fd = -1;
@@ -271,14 +270,14 @@ nonsel:           /* Now parse the command */
                        else if ((cmd->u.fd = open(p,
                            O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
                            DEFFILEMODE)) == -1)
-                               error(FATAL, "%s: %s", p, strerror(errno));
+                               err(1, "%s", p);
                        break;
                case RFILE:                     /* r */
                        pledge_rpath = 1;
                        p++;
                        EATSPACE();
                        if (*p == '\0')
-                               error(COMPILE, "filename expected");
+                               error("filename expected");
                        cmd->t = duptoeol(p, "read command", NULL);
                        break;
                case BRANCH:                    /* b t */
@@ -298,7 +297,7 @@ nonsel:             /* Now parse the command */
                        EATSPACE();
                        cmd->t = duptoeol(p, "label", &p);
                        if (strlen(cmd->t) == 0)
-                               error(COMPILE, "empty label");
+                               error("empty label");
                        enterlabel(cmd);
                        if (*p == ';') {
                                p++;
@@ -308,12 +307,12 @@ nonsel:           /* Now parse the command */
                case SUBST:                     /* s */
                        p++;
                        if (*p == '\0' || *p == '\\')
-                               error(COMPILE, "substitute pattern can not be"
+                               error("substitute pattern can not be"
                                    " delimited by newline or backslash");
                        cmd->u.s = xmalloc(sizeof(struct s_subst));
                        p = compile_re(p, &cmd->u.s->re);
                        if (p == NULL)
-                               error(COMPILE, "unterminated substitute pattern");
+                               error("unterminated substitute pattern");
                        --p;
                        p = compile_subst(p, cmd->u.s);
                        p = compile_flags(p, cmd->u.s);
@@ -334,7 +333,7 @@ nonsel:             /* Now parse the command */
                                goto semicolon;
                        }
                        if (*p)
-                               error(COMPILE, "extra text at the end of a"
+                               error("extra text at the end of a"
                                    " transform command");
                        break;
                }
@@ -359,9 +358,9 @@ compile_delimited(char *p, char *d)
        if (c == '\0')
                return (NULL);
        else if (c == '\\')
-               error(COMPILE, "\\ can not be used as a string delimiter");
+               error("\\ can not be used as a string delimiter");
        else if (c == '\n')
-               error(COMPILE, "newline can not be used as a string delimiter");
+               error("newline can not be used as a string delimiter");
 
        while (p[0]) {
                /* Unescaped delimiter: We are done. */
@@ -393,7 +392,7 @@ compile_delimited(char *p, char *d)
                 * It may contain the delimiter without escaping.
                 */
                else if ((d = compile_ccl(&p, d)) == NULL)
-                       error(COMPILE, "unbalanced brackets ([])");
+                       error("unbalanced brackets ([])");
        }
        return NULL;
 }
@@ -453,7 +452,7 @@ compile_re(char *p, regex_t **repp)
        }
        *repp = xmalloc(sizeof(regex_t));
        if (p && (eval = regcomp(*repp, re, Eflag ? REG_EXTENDED : 0)) != 0)
-               error(COMPILE, "RE error: %s", strregerror(eval, *repp));
+               error("RE error: %s", strregerror(eval, *repp));
        if (maxnsub < (*repp)->re_nsub)
                maxnsub = (*repp)->re_nsub;
        free(re);
@@ -519,8 +518,8 @@ compile_subst(char *p, struct s_subst *s)
                                        ref = *p - '0';
                                        if (s->re != NULL &&
                                            ref > s->re->re_nsub)
-                                               error(COMPILE,
-"\\%c not defined in the RE", *p);
+                                               error("\\%c not defined in the"
+                                                   " RE", *p);
                                        if (s->maxbref < ref)
                                                s->maxbref = ref;
                                } else if (*p == '&' || *p == '\\')
@@ -532,14 +531,14 @@ compile_subst(char *p, struct s_subst *s)
                                s->new = xrealloc(text, size);
                                return (p);
                        } else if (*p == '\n') {
-                               error(COMPILE,
-"unescaped newline inside substitute pattern");
+                               error("unescaped newline inside substitute"
+                                   " pattern");
                        }
                        *sp++ = *p;
                }
                size += sp - op;
        } while ((p = cu_getline(&lbuf, &bufsize)));
-       error(COMPILE, "unterminated substitute in regular expression");
+       error("unterminated substitute in regular expression");
 }
 
 /*
@@ -560,7 +559,7 @@ compile_flags(char *p, struct s_subst *s)
                switch (*p) {
                case 'g':
                        if (gn)
-                               error(COMPILE, "more than one number or 'g' in"
+                               error("more than one number or 'g' in"
                                    " substitute flags");
                        gn = 1;
                        s->n = 0;
@@ -576,20 +575,20 @@ compile_flags(char *p, struct s_subst *s)
                case '4': case '5': case '6':
                case '7': case '8': case '9':
                        if (gn)
-                               error(COMPILE, "more than one number or 'g' in"
+                               error("more than one number or 'g' in"
                                    " substitute flags");
                        gn = 1;
                        l = strtol(p, &p, 10);
                        if (l <= 0 || l >= INT_MAX)
-                               error(COMPILE,
-                                   "number in substitute flags out of range");
+                               error("number in substitute flags out of"
+                                   " range");
                        s->n = (int)l;
                        continue;
                case 'w':
                        p++;
                        EATSPACE();
                        if (*p == '\0')
-                               error(COMPILE, "filename expected");
+                               error("filename expected");
                        s->wfile = duptoeol(p, "s command w flag", NULL);
                        *p = '\0';
                        if (aflag)
@@ -597,11 +596,10 @@ compile_flags(char *p, struct s_subst *s)
                        else if ((s->wfd = open(s->wfile,
                            O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
                            DEFFILEMODE)) == -1)
-                               error(FATAL, "%s: %s", s->wfile, strerror(errno));
+                               err(1, "%s", s->wfile);
                        return (p);
                default:
-                       error(COMPILE,
-                           "bad flag in substitute command: '%c'", *p);
+                       error("bad flag in substitute command: '%c'", *p);
                        break;
                }
                p++;
@@ -621,20 +619,20 @@ compile_tr(char *old, char **transtab)
        memset(check, 0, sizeof(check));
        delimiter = *old;
        if (delimiter == '\\')
-               error(COMPILE, "\\ can not be used as a string delimiter");
+               error("\\ can not be used as a string delimiter");
        else if (delimiter == '\n' || delimiter == '\0')
-               error(COMPILE, "newline can not be used as a string delimiter");
+               error("newline can not be used as a string delimiter");
 
        new = old++;
        do {
                if ((new = strchr(new + 1, delimiter)) == NULL)
-                       error(COMPILE, "unterminated transform source string");
+                       error("unterminated transform source string");
        } while (*(new - 1) == '\\' && *(new -2) != '\\');
        *new = '\0';
        end = new++;
        do {
                if ((end = strchr(end + 1, delimiter)) == NULL)
-                       error(COMPILE, "unterminated transform target string");
+                       error("unterminated transform target string");
        } while (*(end -1) == '\\' && *(end -2) != '\\');
        *end = '\0';
 
@@ -649,24 +647,22 @@ compile_tr(char *old, char **transtab)
                        if (*old == 'n')
                                *old = '\n';
                        else if (*old != delimiter && *old != '\\')
-                               error(COMPILE, "Unexpected character after "
-                                   "backslash");
+                               error("Unexpected character after backslash");
                }
                if (*new == '\\') {
                        new++;
                        if (*new == 'n')
                                *new = '\n';
                        else if (*new != delimiter && *new != '\\')
-                               error(COMPILE, "Unexpected character after "
-                                   "backslash");
+                               error("Unexpected character after backslash");
                }
                if (check[(u_char) *old] == 1)
-                       error(COMPILE, "Repeated character in source string");
+                       error("Repeated character in source string");
                check[(u_char) *old] = 1;
                (*transtab)[(u_char) *old++] = *new++;
        }
        if (*old != '\0' || *new != '\0')
-               error(COMPILE, "transform strings are not the same length");
+               error("transform strings are not the same length");
        return end + 1;
 }
 
@@ -724,7 +720,7 @@ compile_addr(char *p, struct s_addr *a)
        case '/':                               /* Context address */
                p = compile_re(p, &a->u.r);
                if (p == NULL)
-                       error(COMPILE, "unterminated regular expression");
+                       error("unterminated regular expression");
                a->type = AT_RE;
                return (p);
 
@@ -738,7 +734,7 @@ compile_addr(char *p, struct s_addr *a)
                a->u.l = strtoul(p, &end, 10);
                return (end);
        default:
-               error(COMPILE, "expected context address");
+               error("expected context address");
                return (NULL);
        }
 }
@@ -798,7 +794,7 @@ fixuplabel(struct s_command *cp, struct s_command *end)
                                break;
                        }
                        if ((cp->u.c = findlabel(cp->t)) == NULL)
-                               error(COMPILE, "undefined label '%s'", cp->t);
+                               error("undefined label '%s'", cp->t);
                        free(cp->t);
                        break;
                case '{':
@@ -823,7 +819,7 @@ enterlabel(struct s_command *cp)
        lhp = &labels[h & LHMASK];
        for (lh = *lhp; lh != NULL; lh = lh->lh_next)
                if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
-                       error(COMPILE, "duplicate label '%s'", cp->t);
+                       error("duplicate label '%s'", cp->t);
        lh = xmalloc(sizeof *lh);
        lh->lh_next = *lhp;
        lh->lh_hash = h;
index 9c09fff..e85adcb 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: defs.h,v 1.10 2022/12/26 19:16:02 jmc Exp $ */
+/*     $OpenBSD: defs.h,v 1.11 2024/07/17 20:57:15 millert Exp $ */
 /*-
  * Copyright (c) 1992 Diomidis Spinellis.
  * Copyright (c) 1992, 1993
@@ -133,12 +133,6 @@ typedef struct {
        size_t blen;            /* Backing memory length. */
 } SPACE;
 
-/*
- * Error severity codes:
- */
-#define        FATAL           1       /* Exit immediately with 1 */
-#define        COMPILE         2       /* Print error, count and finish script */
-
 /*
  * Round up to the nearest multiple of _POSIX2_LINE_MAX
  */
index 2d28ef8..2ecb30a 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: extern.h,v 1.15 2024/06/18 00:32:22 millert Exp $ */
+/*     $OpenBSD: extern.h,v 1.16 2024/07/17 20:57:16 millert Exp $ */
 /*-
  * Copyright (c) 1992 Diomidis Spinellis.
  * Copyright (c) 1992, 1993
@@ -49,8 +49,8 @@ void   cfclose(struct s_command *, struct s_command *);
 void    compile(void);
 void    cspace(SPACE *, const char *, size_t, enum e_spflag);
 char   *cu_getline(char **, size_t *);
-__dead void error(int, const char *, ...);
-void   warning(const char *, ...);
+__dead void error(const char *, ...) __attribute__((__format__ (printf, 1, 2)));
+void   warning(const char *, ...) __attribute__((__format__ (printf, 1, 2)));
 int     mf_getline(SPACE *, enum e_spflag);
 int     lastline(void);
 void    finish_file(void);
index 979cac6..f47af6f 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: main.c,v 1.46 2024/07/17 03:05:19 millert Exp $       */
+/*     $OpenBSD: main.c,v 1.47 2024/07/17 20:57:16 millert Exp $       */
 
 /*-
  * Copyright (c) 1992 Diomidis Spinellis.
@@ -38,6 +38,7 @@
 #include <sys/stat.h>
 
 #include <ctype.h>
+#include <err.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <limits.h>
@@ -166,10 +167,10 @@ main(int argc, char *argv[])
 
        if (inplace != NULL) {
                if (pledge("stdio rpath wpath cpath fattr chown", NULL) == -1)
-                       error(FATAL, "pledge: %s", strerror(errno));
+                       err(1, "pledge");
        } else {
                if (pledge("stdio rpath wpath cpath", NULL) == -1)
-                       error(FATAL, "pledge: %s", strerror(errno));
+                       err(1, "pledge");
        }
 
        /* First usage case; script is the first arg */
@@ -184,27 +185,27 @@ main(int argc, char *argv[])
        if (*argv) {
                if (!pledge_wpath && inplace == NULL) {
                        if (pledge("stdio rpath", NULL) == -1)
-                               error(FATAL, "pledge: %s", strerror(errno));
+                               err(1, "pledge");
                }
                for (; *argv; argv++)
                        add_file(*argv);
        } else {
                if (!pledge_wpath && !pledge_rpath) {
                        if (pledge("stdio", NULL) == -1)
-                               error(FATAL, "pledge: %s", strerror(errno));
+                               err(1, "pledge");
                } else if (pledge_rpath) {
                        if (pledge("stdio rpath", NULL) == -1)
-                               error(FATAL, "pledge: %s", strerror(errno));
+                               err(1, "pledge");
                } else if (pledge_wpath) {
                        if (pledge("stdio wpath cpath", NULL) == -1)
-                               error(FATAL, "pledge: %s", strerror(errno));
+                               err(1, "pledge");
                }
                add_file(NULL);
        }
        process();
        cfclose(prog, NULL);
        if (fclose(stdout))
-               error(FATAL, "stdout: %s", strerror(errno));
+               err(1, "stdout");
        exit (rval);
 }
 
@@ -234,8 +235,7 @@ again:
                switch (script->type) {
                case CU_FILE:
                        if ((f = fopen(script->s, "r")) == NULL)
-                               error(FATAL,
-                                   "%s: %s", script->s, strerror(errno));
+                               err(1, "%s", script->s);
                        fname = script->s;
                        state = ST_FILE;
                        goto again;
@@ -310,7 +310,7 @@ finish_file(void)
                fclose(infile);
                if (*oldfname != '\0') {
                        if (rename(fname, oldfname) != 0) {
-                               warning("rename(): %s", strerror(errno));
+                               warn("rename %s to %s", fname, oldfname);
                                unlink(tmpfname);
                                exit(1);
                        }
@@ -321,7 +321,7 @@ finish_file(void)
                                fclose(outfile);
                        outfile = NULL;
                        if (rename(tmpfname, fname) != 0) {
-                               warning("rename(): %s", strerror(errno));
+                               warn("rename %s to %s", tmpfname, fname);
                                unlink(tmpfname);
                                exit(1);
                        }
@@ -350,7 +350,7 @@ mf_getline(SPACE *sp, enum e_spflag spflag)
                /* stdin? */
                if (files->fname == NULL) {
                        if (inplace != NULL)
-                               error(FATAL, "-i may not be used with stdin");
+                               errx(1, "-i may not be used with stdin");
                        infile = stdin;
                        fname = "stdin";
                        outfile = stdout;
@@ -382,32 +382,34 @@ mf_getline(SPACE *sp, enum e_spflag spflag)
                fname = files->fname;
                if (inplace != NULL) {
                        if (stat(fname, &sb) != 0)
-                               error(FATAL, "%s: %s", fname,
-                                   strerror(errno ? errno : EIO));
+                               err(1, "%s", fname);
                        if (!S_ISREG(sb.st_mode))
-                               error(FATAL, "%s: %s %s", fname,
+                               errx(1, "%s: %s %s", fname,
                                    "in-place editing only",
                                    "works for regular files");
                        if (*inplace != '\0') {
-                               strlcpy(oldfname, fname,
+                               (void)strlcpy(oldfname, fname,
                                    sizeof(oldfname));
                                len = strlcat(oldfname, inplace,
                                    sizeof(oldfname));
-                               if (len > sizeof(oldfname))
-                                       error(FATAL, "%s: name too long", fname);
+                               if (len >= sizeof(oldfname))
+                                       errc(1, ENAMETOOLONG, "%s", fname);
                        }
-                       strlcpy(dirbuf, fname, sizeof(dirbuf));
+                       len = strlcpy(dirbuf, fname, sizeof(dirbuf));
+                       if (len >= sizeof(dirbuf))
+                               errc(1, ENAMETOOLONG, "%s", fname);
                        len = snprintf(tmpfname, sizeof(tmpfname),
                            "%s/sedXXXXXXXXXX", dirname(dirbuf));
                        if (len >= sizeof(tmpfname))
-                               error(FATAL, "%s: name too long", fname);
+                               errc(1, ENAMETOOLONG, "%s", fname);
                        if ((fd = mkstemp(tmpfname)) == -1)
-                               error(FATAL, "%s: %s", fname, strerror(errno));
+                               err(1, "%s", fname);
                        (void)fchown(fd, sb.st_uid, sb.st_gid);
                        (void)fchmod(fd, sb.st_mode & ALLPERMS);
                        if ((outfile = fdopen(fd, "w")) == NULL) {
+                               warn("%s", fname);
                                unlink(tmpfname);
-                               error(FATAL, "%s", fname);
+                               exit(1);
                        }
                        outfname = tmpfname;
                        linenum = 0;
@@ -417,7 +419,7 @@ mf_getline(SPACE *sp, enum e_spflag spflag)
                        outfname = "stdout";
                }
                if ((infile = fopen(fname, "r")) == NULL) {
-                       warning("%s", strerror(errno));
+                       warn("%s", fname);
                        rval = 1;
                        continue;
                }
@@ -433,7 +435,7 @@ mf_getline(SPACE *sp, enum e_spflag spflag)
         */
        len = getline(&p, &psize, infile);
        if ((ssize_t)len == -1)
-               error(FATAL, "%s: %s", fname, strerror(errno));
+               err(1, "%s", fname);
        if (len != 0 && p[len - 1] == '\n') {
                sp->append_newline = 1;
                len--;
index 99ff1fd..ccc34bf 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: misc.c,v 1.12 2017/01/20 10:26:16 krw Exp $   */
+/*     $OpenBSD: misc.c,v 1.13 2024/07/17 20:57:16 millert Exp $       */
 
 /*-
  * Copyright (c) 1992 Diomidis Spinellis.
@@ -35,7 +35,7 @@
 
 #include <sys/types.h>
 
-#include <errno.h>
+#include <err.h>
 #include <regex.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -54,7 +54,7 @@ xmalloc(size_t size)
        void *p;
 
        if ((p = malloc(size)) == NULL)
-               error(FATAL, "%s", strerror(errno));
+               err(1, NULL);
        return (p);
 }
 
@@ -64,7 +64,7 @@ xreallocarray(void *o, size_t nmemb, size_t size)
        void *p;
 
        if ((p = reallocarray(o, nmemb, size)) == NULL)
-               error(FATAL, "%s", strerror(errno));
+               err(1, NULL);
        return (p);
 }
 
@@ -76,7 +76,7 @@ xrealloc(void *p, size_t size)
 {
 
        if ((p = realloc(p, size)) == NULL)
-               error(FATAL, "%s", strerror(errno));
+               err(1, NULL);
        return (p);
 }
 
@@ -102,16 +102,12 @@ strregerror(int errcode, regex_t *preg)
  * Error reporting function
  */
 __dead void
-error(int severity, const char *fmt, ...)
+error(const char *fmt, ...)
 {
        va_list ap;
 
+       (void)fprintf(stderr, "sed: %lu: %s: ", linenum, fname);
        va_start(ap, fmt);
-       (void)fprintf(stderr, "sed: ");
-       switch (severity) {
-       case COMPILE:
-               (void)fprintf(stderr, "%lu: %s: ", linenum, fname);
-       }
        (void)vfprintf(stderr, fmt, ap);
        va_end(ap);
        (void)fprintf(stderr, "\n");
@@ -123,9 +119,8 @@ warning(const char *fmt, ...)
 {
        va_list ap;
 
+       (void)fprintf(stderr, "sed: %lu: %s: ", linenum, fname);
        va_start(ap, fmt);
-       (void)fprintf(stderr, "sed: ");
-       (void)fprintf(stderr, "%lu: %s: ", linenum, fname);
        (void)vfprintf(stderr, fmt, ap);
        va_end(ap);
        (void)fprintf(stderr, "\n");
index 308ab71..c2d7894 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: process.c,v 1.36 2024/06/18 00:32:22 millert Exp $    */
+/*     $OpenBSD: process.c,v 1.37 2024/07/17 20:57:16 millert Exp $    */
 
 /*-
  * Copyright (c) 1992 Diomidis Spinellis.
@@ -38,7 +38,7 @@
 #include <sys/uio.h>
 
 #include <ctype.h>
-#include <errno.h>
+#include <err.h>
 #include <fcntl.h>
 #include <limits.h>
 #include <regex.h>
@@ -226,12 +226,10 @@ redirect:
                                if (cp->u.fd == -1 && (cp->u.fd = open(cp->t,
                                    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
                                    DEFFILEMODE)) == -1)
-                                       error(FATAL, "%s: %s",
-                                           cp->t, strerror(errno));
+                                       err(1, "%s", cp->t);
                                if ((size_t)write(cp->u.fd, ps, psl) != psl ||
                                    write(cp->u.fd, "\n", 1) != 1)
-                                       error(FATAL, "%s: %s",
-                                           cp->t, strerror(errno));
+                                       err(1, "%s", cp->t);
                                break;
                        case 'x':
                                if (hs == NULL)
@@ -346,8 +344,7 @@ substitute(struct s_command *cp)
        if (re == NULL) {
                if (defpreg != NULL && cp->u.s->maxbref > defpreg->re_nsub) {
                        linenum = cp->u.s->linenum;
-                       error(COMPILE, "\\%d not defined in the RE",
-                           cp->u.s->maxbref);
+                       error("\\%d not defined in the RE", cp->u.s->maxbref);
                }
        }
        if (!regexec_e(re, ps, 0, 0, 0, psl))
@@ -431,10 +428,10 @@ substitute(struct s_command *cp)
        if (cp->u.s->wfile && !pd) {
                if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile,
                    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1)
-                       error(FATAL, "%s: %s", cp->u.s->wfile, strerror(errno));
+                       err(1, "%s", cp->u.s->wfile);
                if ((size_t)write(cp->u.s->wfd, ps, psl) != psl ||
                    write(cp->u.s->wfd, "\n", 1) != 1)
-                       error(FATAL, "%s: %s", cp->u.s->wfile, strerror(errno));
+                       err(1, "%s", cp->u.s->wfile);
        }
        return (1);
 }
@@ -473,7 +470,7 @@ flush_appends(void)
                        break;
                }
        if (ferror(outfile))
-               error(FATAL, "%s: %s", outfname, strerror(errno ? errno : EIO));
+               err(1, "%s", outfname);
        appendx = sdone = 0;
 }
 
@@ -513,7 +510,7 @@ lputs(char *s, size_t len)
        (void)fputc('$', outfile);
        (void)fputc('\n', outfile);
        if (ferror(outfile))
-               error(FATAL, "%s: %s", outfname, strerror(errno ? errno : EIO));
+               err(1, "%s", outfname);
 }
 
 static inline int
@@ -524,7 +521,7 @@ regexec_e(regex_t *preg, const char *string, int eflags,
 
        if (preg == NULL) {
                if (defpreg == NULL)
-                       error(FATAL, "first RE may not be empty");
+                       errx(1, "first RE may not be empty");
        } else
                defpreg = preg;
 
@@ -540,7 +537,7 @@ regexec_e(regex_t *preg, const char *string, int eflags,
        case REG_NOMATCH:
                return (0);
        }
-       error(FATAL, "RE error: %s", strregerror(eval, defpreg));
+       errx(1, "RE error: %s", strregerror(eval, defpreg));
 }
 
 /*
@@ -624,13 +621,12 @@ cfclose(struct s_command *cp, struct s_command *end)
                switch (cp->code) {
                case 's':
                        if (cp->u.s->wfd != -1 && close(cp->u.s->wfd))
-                               error(FATAL,
-                                   "%s: %s", cp->u.s->wfile, strerror(errno));
+                               err(1, "%s", cp->u.s->wfile);
                        cp->u.s->wfd = -1;
                        break;
                case 'w':
                        if (cp->u.fd != -1 && close(cp->u.fd))
-                               error(FATAL, "%s: %s", cp->t, strerror(errno));
+                               err(1, "%s", cp->t);
                        cp->u.fd = -1;
                        break;
                case '{':