Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/nl - document non-literal format string



details:   https://anonhg.NetBSD.org/src/rev/4ff0c1c7925e
branches:  trunk
changeset: 768419:4ff0c1c7925e
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Aug 16 12:00:46 2011 +0000

description:
- document non-literal format string
- use err/errx
- prototypes

diffstat:

 usr.bin/nl/Makefile |    4 +-
 usr.bin/nl/nl.c     |  131 ++++++++++++++++++++-------------------------------
 2 files changed, 54 insertions(+), 81 deletions(-)

diffs (truncated from 301 to 300 lines):

diff -r a3beb382b593 -r 4ff0c1c7925e usr.bin/nl/Makefile
--- a/usr.bin/nl/Makefile       Tue Aug 16 11:49:17 2011 +0000
+++ b/usr.bin/nl/Makefile       Tue Aug 16 12:00:46 2011 +0000
@@ -1,5 +1,7 @@
-#      $NetBSD: Makefile,v 1.3 2006/10/08 17:52:29 peter Exp $
+#      $NetBSD: Makefile,v 1.4 2011/08/16 12:00:46 christos Exp $
 
 PROG=  nl
 
+COPTS.nl.c += -Wno-format-nonliteral
+
 .include <bsd.prog.mk>
diff -r a3beb382b593 -r 4ff0c1c7925e usr.bin/nl/nl.c
--- a/usr.bin/nl/nl.c   Tue Aug 16 11:49:17 2011 +0000
+++ b/usr.bin/nl/nl.c   Tue Aug 16 12:00:46 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nl.c,v 1.10 2009/04/12 23:37:12 lukem Exp $    */
+/*     $NetBSD: nl.c,v 1.11 2011/08/16 12:00:46 christos Exp $ */
 
 /*-
  * Copyright (c) 1999 The NetBSD Foundation, Inc.
@@ -33,7 +33,7 @@
 #ifndef lint
 __COPYRIGHT("@(#) Copyright (c) 1999\
  The NetBSD Foundation, Inc.  All rights reserved.");
-__RCSID("$NetBSD: nl.c,v 1.10 2009/04/12 23:37:12 lukem Exp $");
+__RCSID("$NetBSD: nl.c,v 1.11 2011/08/16 12:00:46 christos Exp $");
 #endif    
 
 #include <errno.h>
@@ -44,6 +44,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <err.h>
 
 typedef enum {
        number_all,             /* number all lines */
@@ -83,10 +84,9 @@
 #define INT_STRLEN_MAXIMUM \
        ((sizeof (int) * CHAR_BIT - 1) * 302 / 1000 + 2)
 
-static void    filter __P((void));
-int            main __P((int, char *[]));
-static void    parse_numbering __P((const char *, int));
-static void    usage __P((void));
+static void    filter(void);
+static void    parse_numbering(const char *, int);
+static void    usage(void) __attribute__((__noreturn__));
 
 /*
  * Pointer to dynamically allocated input line buffer, and its size.
@@ -98,6 +98,7 @@
  * Dynamically allocated buffer suitable for string representation of ints.
  */
 static char *intbuffer;
+static size_t intbuffersize;
 
 /*
  * Configurable parameters.
@@ -129,15 +130,12 @@
 
 
 int
-main(argc, argv)
-       int argc;
-       char *argv[];
+main(int argc, char *argv[])
 {
        int c;
        long val;
        unsigned long uval;
        char *ep;
-       size_t intbuffersize;
 
        (void)setlocale(LC_ALL, "");
 
@@ -163,10 +161,9 @@
                                delim[1] = optarg[1];
                        /* at most two delimiter characters */
                        if (optarg[2] != '\0') {
-                               (void)fprintf(stderr,
-                                   "nl: invalid delim argument -- %s\n",
+                               errx(EXIT_FAILURE,
+                                   "invalid delim argument -- %s",
                                    optarg);
-                               exit(EXIT_FAILURE);
                                /* NOTREACHED */
                        }
                        break;
@@ -180,22 +177,18 @@
                        errno = 0;
                        val = strtol(optarg, &ep, 10);
                        if ((ep != NULL && *ep != '\0') ||
-                        ((val == LONG_MIN || val == LONG_MAX) && errno != 0)) {
-                               (void)fprintf(stderr,
-                                   "invalid incr argument -- %s\n", optarg);
-                               exit(EXIT_FAILURE);
-                       }
+                        ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
+                               errx(EXIT_FAILURE,
+                                   "invalid incr argument -- %s", optarg);
                        incr = (int)val;
                        break;
                case 'l':
                        errno = 0;
                        uval = strtoul(optarg, &ep, 10);
                        if ((ep != NULL && *ep != '\0') ||
-                           (uval == ULONG_MAX && errno != 0)) {
-                               (void)fprintf(stderr,
-                                   "invalid num argument -- %s\n", optarg);
-                               exit(EXIT_FAILURE);
-                       }
+                           (uval == ULONG_MAX && errno != 0))
+                               errx(EXIT_FAILURE,
+                                   "invalid num argument -- %s", optarg);
                        nblank = (unsigned int)uval;
                        break;
                case 'n':
@@ -205,11 +198,9 @@
                                format = FORMAT_RN;
                        } else if (strcmp(optarg, "rz") == 0) {
                                format = FORMAT_RZ;
-                       } else {
-                               (void)fprintf(stderr,
-                                   "nl: illegal format -- %s\n", optarg);
-                               exit(EXIT_FAILURE);
-                       }
+                       } else
+                               errx(EXIT_FAILURE,
+                                   "illegal format -- %s", optarg);
                        break;
                case 's':
                        sep = optarg;
@@ -218,29 +209,23 @@
                        errno = 0;
                        val = strtol(optarg, &ep, 10);
                        if ((ep != NULL && *ep != '\0') ||
-                        ((val == LONG_MIN || val == LONG_MAX) && errno != 0)) {
-                               (void)fprintf(stderr,
-                                   "invalid startnum value -- %s\n", optarg);
-                               exit(EXIT_FAILURE);
-                       }
+                        ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
+                               errx(EXIT_FAILURE,
+                                   "invalid startnum value -- %s", optarg);
                        startnum = (int)val;
                        break;
                case 'w':
                        errno = 0;
                        val = strtol(optarg, &ep, 10);
                        if ((ep != NULL && *ep != '\0') ||
-                        ((val == LONG_MIN || val == LONG_MAX) && errno != 0)) {
-                               (void)fprintf(stderr,
-                                   "invalid width value -- %s\n", optarg);
-                               exit(EXIT_FAILURE);
-                       }
+                        ((val == LONG_MIN || val == LONG_MAX) && errno != 0))
+                               errx(EXIT_FAILURE,
+                                   "invalid width value -- %s", optarg);
                        width = (int)val;
-                       if (!(width > 0)) {
-                               (void)fprintf(stderr,
-                                   "nl: width argument must be > 0 -- %d\n",
+                       if (!(width > 0))
+                               errx(EXIT_FAILURE,
+                                   "width argument must be > 0 -- %d",
                                    width);
-                                exit(EXIT_FAILURE);
-                       }
                        break;
                case '?':
                default:
@@ -255,10 +240,8 @@
        case 0:
                break;
        case 1:
-               if (freopen(argv[0], "r", stdin) == NULL) {
-                       perror(argv[0]);
-                       exit(EXIT_FAILURE);
-               }
+               if (freopen(argv[0], "r", stdin) == NULL)
+                       err(EXIT_FAILURE, "Cannot open `%s'", argv[0]);
                break;
        default:
                usage();
@@ -270,27 +253,23 @@
                val = LINE_MAX;
        /* Allocate sufficient buffer space (including the terminating NUL). */
        buffersize = (size_t)val + 1;
-       if ((buffer = malloc(buffersize)) == NULL) {
-               perror("cannot allocate input line buffer");
-               exit(EXIT_FAILURE);
-       }
+       if ((buffer = malloc(buffersize)) == NULL)
+               err(EXIT_FAILURE, "Cannot allocate input line buffer");
 
        /* Allocate a buffer suitable for preformatting line number. */
        intbuffersize = max((int)INT_STRLEN_MAXIMUM, width) + 1; /* NUL */
-       if ((intbuffer = malloc(intbuffersize)) == NULL) {
-               perror("cannot allocate preformatting buffer");
-               exit(EXIT_FAILURE);
-       }
+       if ((intbuffer = malloc(intbuffersize)) == NULL)
+               err(EXIT_FAILURE, "cannot allocate preformatting buffer");
 
        /* Do the work. */
        filter();
 
-       exit(EXIT_SUCCESS);
+       return EXIT_SUCCESS;
        /* NOTREACHED */
 }
 
 static void
-filter()
+filter(void)
 {
        int line;               /* logical line number */
        int section;            /* logical page section */
@@ -349,8 +328,8 @@
                }
 
                if (donumber) {
-                       /* Note: sprintf() is safe here. */
-                       consumed = sprintf(intbuffer, format, width, line);
+                       consumed = snprintf(intbuffer, intbuffersize, format,
+                           width, line);
                        (void)printf("%s",
                            intbuffer + max(0, consumed - width));
                        line += incr;
@@ -359,18 +338,14 @@
                }
                (void)printf("%s%s", sep, buffer);
 
-               if (ferror(stdout)) {
-                       perror("output error");
-                       exit(EXIT_FAILURE);
-               }
+               if (ferror(stdout))
+                       err(EXIT_FAILURE, "output error");
 nextline:
                ;
        }
 
-       if (ferror(stdin)) {
-               perror("input error");
-               exit(EXIT_FAILURE);
-       }
+       if (ferror(stdin))
+               err(EXIT_FAILURE, "input error");
 }
 
 /*
@@ -378,9 +353,7 @@
  */
 
 static void
-parse_numbering(argstr, section)
-       const char *argstr;
-       int section;
+parse_numbering(const char *argstr, int section)
 {
        int error;
        char errorbuf[NL_TEXTMAX];
@@ -408,27 +381,25 @@
                        (void)regerror(error,
                            &numbering_properties[section].expr,
                            errorbuf, sizeof (errorbuf));
-                       (void)fprintf(stderr,
-                           "nl: %s expr: %s -- %s\n",
+                       errx(EXIT_FAILURE,
+                           "%s expr: %s -- %s",
                            numbering_properties[section].name, errorbuf,
                            &argstr[1]);
-                       exit(EXIT_FAILURE);
                }
                break;
        default:
-               (void)fprintf(stderr,
-                   "nl: illegal %s line numbering type -- %s\n",
+               errx(EXIT_FAILURE,
+                   "illegal %s line numbering type -- %s",
                    numbering_properties[section].name, argstr);
                exit(EXIT_FAILURE);
        }
 }
 
 static void
-usage()
+usage(void)
 {
-
-       (void)fprintf(stderr, "usage: nl [-p] [-b type] [-d delim] [-f type] \
-[-h type] [-i incr] [-l num]\n\t[-n format] [-s sep] [-v startnum] [-w width] \
-[file]\n");
+       (void)fprintf(stderr, "Usage: %s [-p] [-b type] [-d delim] [-f type] "
+           "[-h type] [-i incr] [-l num]\n\t[-n format] [-s sep] "
+           "[-v startnum] [-w width] [file]\n", getprogname());
        exit(EXIT_FAILURE);



Home | Main Index | Thread Index | Old Index