Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/tail Instead of declaring our own err() which is dif...



details:   https://anonhg.NetBSD.org/src/rev/c034fa8f0cef
branches:  trunk
changeset: 769202:c034fa8f0cef
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Sep 03 09:02:20 2011 +0000

description:
Instead of declaring our own err() which is different than the standard one,
and using it incorrectly in a few places because of confusion (does it print
errno or not?), declare two versions following the standard ones xerrx and,
xerr, and use those as appropriate, implementing them using them vwarn and
vwarnx.

diffstat:

 usr.bin/tail/extern.h  |   6 +++---
 usr.bin/tail/forward.c |  14 +++++++-------
 usr.bin/tail/misc.c    |  26 +++++++++++++++++++-------
 usr.bin/tail/read.c    |  12 ++++++------
 usr.bin/tail/reverse.c |  16 +++++++++-------
 usr.bin/tail/tail.c    |  10 +++++-----
 6 files changed, 49 insertions(+), 35 deletions(-)

diffs (truncated from 311 to 300 lines):

diff -r a69bf23d9ebd -r c034fa8f0cef usr.bin/tail/extern.h
--- a/usr.bin/tail/extern.h     Sat Sep 03 01:56:44 2011 +0000
+++ b/usr.bin/tail/extern.h     Sat Sep 03 09:02:20 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: extern.h,v 1.9 2009/04/13 23:33:25 lukem Exp $ */
+/*     $NetBSD: extern.h,v 1.10 2011/09/03 09:02:20 christos Exp $     */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -43,8 +43,8 @@
 int displaybytes(FILE *, off_t);
 int displaylines(FILE *, off_t);
 
-void err(int fatal, const char *fmt, ...)
-     __attribute__((__format__(__printf__, 2, 3)));
+void xerr(int fatal, const char *fmt, ...) __printflike(2, 3);
+void xerrx(int fatal, const char *fmt, ...) __printflike(2, 3);
 void ierr(void);
 void oerr(void);
 
diff -r a69bf23d9ebd -r c034fa8f0cef usr.bin/tail/forward.c
--- a/usr.bin/tail/forward.c    Sat Sep 03 01:56:44 2011 +0000
+++ b/usr.bin/tail/forward.c    Sat Sep 03 09:02:20 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: forward.c,v 1.29 2009/04/13 23:33:25 lukem Exp $       */
+/*     $NetBSD: forward.c,v 1.30 2011/09/03 09:02:20 christos Exp $    */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)forward.c  8.1 (Berkeley) 6/6/93";
 #endif
-__RCSID("$NetBSD: forward.c,v 1.29 2009/04/13 23:33:25 lukem Exp $");
+__RCSID("$NetBSD: forward.c,v 1.30 2011/09/03 09:02:20 christos Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -180,7 +180,7 @@
        if (fflag) {
                kq = kqueue();
                if (kq < 0)
-                       err(1, "kqueue");
+                       xerr(1, "kqueue");
                action = ADD_EVENTS;
        }
 
@@ -225,7 +225,7 @@
 
                case USE_KQUEUE:
                        if (kevent(kq, NULL, 0, ev, 1, NULL) < 0)
-                               err(1, "kevent");
+                               xerr(1, "kevent");
 
                        if (ev[0].filter == EVFILT_VNODE) {
                                /* file was rotated, wait until it reappears */
@@ -305,7 +305,7 @@
                start = mmap(NULL, (size_t)mmap_size, PROT_READ,
                             MAP_FILE|MAP_SHARED, fileno(fp), mmap_offset);
                if (start == MAP_FAILED) {
-                       err(0, "%s: %s", fname, strerror(EFBIG));
+                       xerr(0, "%s", fname);
                        return (1);
                }
 
@@ -326,7 +326,7 @@
                        break;
 
                if (munmap(start, mmap_size)) {
-                       err(0, "%s: %s", fname, strerror(errno));
+                       xerr(0, "%s", fname);
                        return (1);
                }
 
@@ -344,7 +344,7 @@
        WR(p, mmap_size - mmap_remaining);
        file_remaining += mmap_size - mmap_remaining;
        if (munmap(start, mmap_size)) {
-               err(0, "%s: %s", fname, strerror(errno));
+               xerr(0, "%s", fname);
                return (1);
        }
 
diff -r a69bf23d9ebd -r c034fa8f0cef usr.bin/tail/misc.c
--- a/usr.bin/tail/misc.c       Sat Sep 03 01:56:44 2011 +0000
+++ b/usr.bin/tail/misc.c       Sat Sep 03 09:02:20 2011 +0000
@@ -35,7 +35,7 @@
 #if 0
 static char sccsid[] = "@(#)misc.c     8.1 (Berkeley) 6/6/93";
 #endif
-__RCSID("$NetBSD: misc.c,v 1.6 2003/08/07 11:16:02 agc Exp $");
+__RCSID("$NetBSD: misc.c,v 1.7 2011/09/03 09:02:20 christos Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -46,32 +46,44 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <err.h>
 
 #include "extern.h"
 
 void
 ierr(void)
 {
-       err(0, "%s: %s", fname, strerror(errno));
+       xerr(0, "%s", fname);
 }
 
 void
 oerr(void)
 {
-       err(1, "stdout: %s", strerror(errno));
+       xerr(1, "stdout");
 }
 
 void
-err(int fatal, const char *fmt, ...)
+xerr(int fatal, const char *fmt, ...)
 {
        va_list ap;
 
        va_start(ap, fmt);
-       (void)fprintf(stderr, "tail: ");
-       (void)vfprintf(stderr, fmt, ap);
+       vwarn(fmt, ap);
        va_end(ap);
-       (void)fprintf(stderr, "\n");
        if (fatal)
                exit(1);
        rval = 1;
 }
+
+void
+xerrx(int fatal, const char *fmt, ...)
+{
+       va_list ap;
+
+       va_start(ap, fmt);
+       vwarnx(fmt, ap);
+       va_end(ap);
+       if (fatal)
+               exit(1);
+       rval = 1;
+}
diff -r a69bf23d9ebd -r c034fa8f0cef usr.bin/tail/read.c
--- a/usr.bin/tail/read.c       Sat Sep 03 01:56:44 2011 +0000
+++ b/usr.bin/tail/read.c       Sat Sep 03 09:02:20 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: read.c,v 1.15 2009/04/13 23:33:25 lukem Exp $  */
+/*     $NetBSD: read.c,v 1.16 2011/09/03 09:02:20 christos Exp $       */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)read.c     8.1 (Berkeley) 6/6/93";
 #endif
-__RCSID("$NetBSD: read.c,v 1.15 2009/04/13 23:33:25 lukem Exp $");
+__RCSID("$NetBSD: read.c,v 1.16 2011/09/03 09:02:20 christos Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -71,7 +71,7 @@
        char *sp;
 
        if ((sp = p = malloc(off)) == NULL)
-               err(1, "%s", strerror(errno));
+               xerr(1, "malloc");
 
        for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF;) {
                *p = ch;
@@ -145,7 +145,7 @@
 
        p = NULL;
        if ((lines = malloc(off * sizeof(*lines))) == NULL)
-               err(1, "%s", strerror(errno));
+               xerr(1, "malloc");
 
        memset(lines, 0, sizeof(*lines) * off);
 
@@ -155,7 +155,7 @@
        while ((ch = getc(fp)) != EOF) {
                if (++cnt > blen) {
                        if ((n = realloc(sp, blen + 1024)) == NULL)
-                               err(1, "%s", strerror(errno));
+                               xerr(1, "realloc");
                        sp = n;
                        blen += 1024;
                        p = sp + cnt - 1;
@@ -165,7 +165,7 @@
                        if (lines[recno].blen < cnt) {
                                if ((n = realloc(lines[recno].l,
                                    cnt + 256)) == NULL)
-                                       err(1, "%s", strerror(errno));
+                                       xerr(1, "realloc");
                                lines[recno].l = n;
                                lines[recno].blen = cnt + 256;
                        }
diff -r a69bf23d9ebd -r c034fa8f0cef usr.bin/tail/reverse.c
--- a/usr.bin/tail/reverse.c    Sat Sep 03 01:56:44 2011 +0000
+++ b/usr.bin/tail/reverse.c    Sat Sep 03 09:02:20 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: reverse.c,v 1.20 2009/04/13 23:33:25 lukem Exp $       */
+/*     $NetBSD: reverse.c,v 1.21 2011/09/03 09:02:20 christos Exp $    */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)reverse.c  8.1 (Berkeley) 6/6/93";
 #endif
-__RCSID("$NetBSD: reverse.c,v 1.20 2009/04/13 23:33:25 lukem Exp $");
+__RCSID("$NetBSD: reverse.c,v 1.21 2011/09/03 09:02:20 christos Exp $");
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -115,13 +115,15 @@
 
        if ((uint64_t)size > SIZE_T_MAX) {
                        /* XXX: need a cleaner way to check this on amd64 */
-               err(0, "%s: %s", fname, strerror(EFBIG));
+               errno = EFBIG;
+               xerr(0, "%s", fname);
                return;
        }
 
        if ((start = mmap(NULL, (size_t)size, PROT_READ,
            MAP_FILE|MAP_SHARED, fileno(fp), (off_t)0)) == (caddr_t)-1) {
-               err(0, "%s: %s", fname, strerror(EFBIG));
+               errno = EFBIG;
+               xerr(0, "%s", fname);
                return;
        }
        p = start + size - 1;
@@ -142,7 +144,7 @@
        if (llen)
                WR(p, llen);
        if (munmap(start, (size_t)sbp->st_size))
-               err(0, "%s: %s", fname, strerror(errno));
+               xerr(0, "%s", fname);
 }
 
 typedef struct bf {
@@ -181,7 +183,7 @@
                if (enomem) {
                        if (!mark) {
                                errno = ENOMEM;
-                               err(1, NULL);
+                               xerr(1, NULL);
                        }
                        tl = tl->next;
                        enomem += tl->len;
@@ -191,7 +193,7 @@
                                free(tl);
                        if (!mark) {
                                errno = ENOMEM;
-                               err(1, NULL);
+                               xerr(1, NULL);
                        }
                        tl = mark;
                        enomem += tl->len;
diff -r a69bf23d9ebd -r c034fa8f0cef usr.bin/tail/tail.c
--- a/usr.bin/tail/tail.c       Sat Sep 03 01:56:44 2011 +0000
+++ b/usr.bin/tail/tail.c       Sat Sep 03 09:02:20 2011 +0000
@@ -40,7 +40,7 @@
 #if 0
 static char sccsid[] = "@(#)tail.c     8.1 (Berkeley) 6/6/93";
 #endif
-__RCSID("$NetBSD: tail.c,v 1.14 2009/04/13 23:33:25 lukem Exp $");
+__RCSID("$NetBSD: tail.c,v 1.15 2011/09/03 09:02:20 christos Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -87,7 +87,7 @@
                usage();                                                \
        off = strtoll(optarg, &p, 10) * (units);                        \
        if (*p)                                                         \
-               err(1, "illegal offset -- %s", optarg);                 \
+               xerrx(1, "illegal offset -- %s", optarg);               \
        switch(optarg[0]) {                                             \
        case '+':                                                       \
                if (off)                                                \
@@ -133,7 +133,7 @@
        argv += optind;
 
        if (fflag && argc > 1)
-               err(1, "-f and -F options only appropriate for a single file");
+               xerrx(1, "-f and -F options only appropriate for a single file");
 
        /*
         * If displaying in reverse, don't permit follow option, and convert
@@ -235,7 +235,7 @@
                        /* Malloc space for dash, new option and argument. */
                        len = strlen(*argv);
                        if ((start = p = malloc(len + 3)) == NULL)
-                               err(1, "%s", strerror(errno));
+                               xerr(1, "malloc");
                        *p++ = '-';



Home | Main Index | Thread Index | Old Index