diff options
author | cheloha <cheloha@cvs.openbsd.org> | 2018-03-05 16:53:40 +0000 |
---|---|---|
committer | cheloha <cheloha@cvs.openbsd.org> | 2018-03-05 16:53:40 +0000 |
commit | 11ac39bb26cf4cd4a9ba55c2aed6eaf4b038e7c1 (patch) | |
tree | 9d3ccf7331a1e336223cb76e3df9f1f5d49ed1d5 | |
parent | c47b38b25549b2bc07e204030c4cd5827ea17af2 (diff) |
Add fatal() and fatalx() and put them to use.
... but don't use them for pledge errors or usage errors.
They are convenience wrappers that check if sflag is set before
logging an error. They always exit with status 2.
We were not honoring sflag in special.c at all. Now we do.
ok tb@
-rw-r--r-- | usr.bin/cmp/cmp.c | 43 | ||||
-rw-r--r-- | usr.bin/cmp/extern.h | 6 | ||||
-rw-r--r-- | usr.bin/cmp/misc.c | 29 | ||||
-rw-r--r-- | usr.bin/cmp/special.c | 10 |
4 files changed, 49 insertions, 39 deletions
diff --git a/usr.bin/cmp/cmp.c b/usr.bin/cmp/cmp.c index ec95882c1a6..3af0d0caa66 100644 --- a/usr.bin/cmp/cmp.c +++ b/usr.bin/cmp/cmp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmp.c,v 1.16 2016/10/28 07:22:59 schwarze Exp $ */ +/* $OpenBSD: cmp.c,v 1.17 2018/03/05 16:53:39 cheloha Exp $ */ /* $NetBSD: cmp.c,v 1.7 1995/09/08 03:22:56 tls Exp $ */ /* @@ -84,29 +84,16 @@ main(int argc, char *argv[]) special = 1; fd1 = 0; file1 = "stdin"; - } else if ((fd1 = open(file1, O_RDONLY, 0)) < 0) { - if (sflag) - return ERR_EXIT; - else - err(ERR_EXIT, "%s", file1); - } + } else if ((fd1 = open(file1, O_RDONLY, 0)) == -1) + fatal("%s", file1); if (strcmp(file2 = argv[1], "-") == 0) { - if (special) { - if (sflag) - return ERR_EXIT; - else - errx(ERR_EXIT, - "standard input may only be specified once"); - } + if (special) + fatalx("standard input may only be specified once"); special = 1; fd2 = 0; file2 = "stdin"; - } else if ((fd2 = open(file2, O_RDONLY, 0)) < 0) { - if (sflag) - return ERR_EXIT; - else - err(ERR_EXIT, "%s", file2); - } + } else if ((fd2 = open(file2, O_RDONLY, 0)) == -1) + fatal("%s", file2); if (pledge("stdio", NULL) == -1) err(ERR_EXIT, "pledge"); @@ -115,21 +102,13 @@ main(int argc, char *argv[]) skip2 = argc == 4 ? strtoll(argv[3], NULL, 0) : 0; if (!special) { - if (fstat(fd1, &sb1)) { - if (sflag) - return ERR_EXIT; - else - err(ERR_EXIT, "%s", file1); - } + if (fstat(fd1, &sb1) == -1) + fatal("%s", file1); if (!S_ISREG(sb1.st_mode)) special = 1; else { - if (fstat(fd2, &sb2)) { - if (sflag) - return ERR_EXIT; - else - err(ERR_EXIT, "%s", file2); - } + if (fstat(fd2, &sb2) == -1) + fatal("%s", file2); if (!S_ISREG(sb2.st_mode)) special = 1; } diff --git a/usr.bin/cmp/extern.h b/usr.bin/cmp/extern.h index 8e58af437f8..43e98530b25 100644 --- a/usr.bin/cmp/extern.h +++ b/usr.bin/cmp/extern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: extern.h,v 1.5 2007/02/23 16:00:04 millert Exp $ */ +/* $OpenBSD: extern.h,v 1.6 2018/03/05 16:53:39 cheloha Exp $ */ /* $NetBSD: extern.h,v 1.2 1995/09/08 03:22:57 tls Exp $ */ /*- @@ -39,5 +39,9 @@ void c_regular(int, char *, off_t, off_t, int, char *, off_t, off_t); void c_special(int, char *, off_t, int, char *, off_t); void diffmsg(char *, char *, off_t, off_t); void eofmsg(char *); +void fatal(const char *, ...) + __attribute__((__noreturn__, __format__ (printf, 1, 2))); +void fatalx(const char *, ...) + __attribute__((__noreturn__, __format__ (printf, 1, 2))); extern int lflag, sflag; diff --git a/usr.bin/cmp/misc.c b/usr.bin/cmp/misc.c index b04ee6a3273..2304b734800 100644 --- a/usr.bin/cmp/misc.c +++ b/usr.bin/cmp/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.6 2011/01/19 13:01:25 okan Exp $ */ +/* $OpenBSD: misc.c,v 1.7 2018/03/05 16:53:39 cheloha Exp $ */ /* $NetBSD: misc.c,v 1.2 1995/09/08 03:22:58 tls Exp $ */ /*- @@ -33,6 +33,7 @@ #include <sys/types.h> #include <err.h> +#include <stdarg.h> #include <stdio.h> #include <stdlib.h> @@ -54,3 +55,29 @@ diffmsg(char *file1, char *file2, off_t byte, off_t line) file1, file2, (long long)byte, (long long)line); exit(DIFF_EXIT); } + +void +fatal(const char *fmt, ...) +{ + va_list ap; + + if (!sflag) { + va_start(ap, fmt); + vwarn(fmt, ap); + va_end(ap); + } + exit(ERR_EXIT); +} + +void +fatalx(const char *fmt, ...) +{ + va_list ap; + + if (!sflag) { + va_start(ap, fmt); + vwarnx(fmt, ap); + va_end(ap); + } + exit(ERR_EXIT); +} diff --git a/usr.bin/cmp/special.c b/usr.bin/cmp/special.c index c7b747598fd..d6896f4f3b7 100644 --- a/usr.bin/cmp/special.c +++ b/usr.bin/cmp/special.c @@ -1,4 +1,4 @@ -/* $OpenBSD: special.c,v 1.7 2011/01/19 13:01:25 okan Exp $ */ +/* $OpenBSD: special.c,v 1.8 2018/03/05 16:53:39 cheloha Exp $ */ /* $NetBSD: special.c,v 1.2 1995/09/08 03:23:00 tls Exp $ */ /*- @@ -48,9 +48,9 @@ c_special(int fd1, char *file1, off_t skip1, int fd2, char *file2, off_t skip2) int dfound; if ((fp1 = fdopen(fd1, "r")) == NULL) - err(ERR_EXIT, "%s", file1); + fatal("%s", file1); if ((fp2 = fdopen(fd2, "r")) == NULL) - err(ERR_EXIT, "%s", file2); + fatal("%s", file2); dfound = 0; while (skip1--) @@ -79,9 +79,9 @@ c_special(int fd1, char *file1, off_t skip1, int fd2, char *file2, off_t skip2) } eof: if (ferror(fp1)) - err(ERR_EXIT, "%s", file1); + fatal("%s", file1); if (ferror(fp2)) - err(ERR_EXIT, "%s", file2); + fatal("%s", file2); if (feof(fp1)) { if (!feof(fp2)) eofmsg(file1); |