Source-Changes-HG archive

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

[src/trunk]: src/usr.sbin/ipwctl - KNF



details:   https://anonhg.NetBSD.org/src/rev/e88dcec5ca0d
branches:  trunk
changeset: 579977:e88dcec5ca0d
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Apr 03 17:27:15 2005 +0000

description:
- KNF
- Fix mmap call arguments and return value.
- Sprinkle const

diffstat:

 usr.sbin/ipwctl/Makefile |   3 +-
 usr.sbin/ipwctl/ipwctl.c |  70 ++++++++++++++++++++++-------------------------
 2 files changed, 35 insertions(+), 38 deletions(-)

diffs (188 lines):

diff -r fb5299a7f093 -r e88dcec5ca0d usr.sbin/ipwctl/Makefile
--- a/usr.sbin/ipwctl/Makefile  Sun Apr 03 17:21:29 2005 +0000
+++ b/usr.sbin/ipwctl/Makefile  Sun Apr 03 17:27:15 2005 +0000
@@ -1,5 +1,6 @@
-#      $NetBSD: Makefile,v 1.4 2004/08/23 13:51:05 lukem Exp $
+#      $NetBSD: Makefile,v 1.5 2005/04/03 17:27:15 christos Exp $
 
+WARNS=3
 .if ${MACHINE} == "i386"
 PROG=  ipwctl
 .endif
diff -r fb5299a7f093 -r e88dcec5ca0d usr.sbin/ipwctl/ipwctl.c
--- a/usr.sbin/ipwctl/ipwctl.c  Sun Apr 03 17:21:29 2005 +0000
+++ b/usr.sbin/ipwctl/ipwctl.c  Sun Apr 03 17:27:15 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ipwctl.c,v 1.4 2004/08/27 00:05:37 lukem Exp $ */
+/*     $NetBSD: ipwctl.c,v 1.5 2005/04/03 17:27:15 christos Exp $      */
 /*     Id: ipwctl.c,v 1.1.2.1 2004/08/19 16:24:50 damien Exp   */
 
 /*-
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: ipwctl.c,v 1.4 2004/08/27 00:05:37 lukem Exp $");
+__RCSID("$NetBSD: ipwctl.c,v 1.5 2005/04/03 17:27:15 christos Exp $");
 
 #include <sys/types.h>
 #include <sys/ioctl.h>
@@ -53,25 +53,20 @@
 #define SIOCGRADIO     _IOWR('i', 139, struct ifreq)
 #define SIOCGTABLE1    _IOWR('i', 140, struct ifreq)
 
-extern char *optarg;
-extern int optind;
-extern int optopt;
-extern int opterr;
-extern int optreset;
-
-static void usage(void);
-static int do_req(char *, unsigned long, void *);
-static void load_firmware(char *, char *);
-static void kill_firmware(char *);
-static void get_radio_state(char *);
-static void get_statistics(char *);
+static void usage(void) __attribute__((__noreturn__));
+static int do_req(const char *, unsigned long, void *);
+static void load_firmware(const char *, const char *);
+static void kill_firmware(const char *);
+static void get_radio_state(const char *);
+static void get_statistics(const char *);
 
 int
 main(int argc, char **argv)
 {
        int ch;
-       char *iface;
+       const char *iface;
 
+       setprogname(argv[0]);
        opterr = 0;
        ch = getopt(argc, argv, "i:");
        if (ch == 'i') {
@@ -115,23 +110,21 @@
 static void
 usage(void)
 {
-       extern char *__progname;
-
-       (void)fprintf(stderr, "usage:  %s -i iface\n"
+       (void)fprintf(stderr, "Usage:  %s -i iface\n"
            "\t%s -i iface -f firmware\n"
            "\t%s -i iface -k\n"
-           "\t%s -i iface -r\n", __progname, __progname, __progname, 
-           __progname);
+           "\t%s -i iface -r\n", getprogname(), getprogname(), getprogname(), 
+           getprogname());
 
        exit(EX_USAGE);
 }
 
 static int
-do_req(char *iface, unsigned long req, void *data)
+do_req(const char *iface, unsigned long req, void *data)
 {
        int s;
        struct ifreq ifr;
-       int error;
+       int error, serrno;
 
        if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
                err(EX_OSERR, "Can't create socket");
@@ -140,18 +133,19 @@
        strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
        ifr.ifr_data = data;
        error = ioctl(s, req, &ifr);
-
+       serrno = errno;
        (void)close(s);
-
+       errno = serrno;
        return error;
 }
 
 static void
-load_firmware(char *iface, char *firmware)
+load_firmware(const char *iface, const char *firmware)
 {
        int fd;
        struct stat st;
        void *map;
+       size_t len;
 
        if ((fd = open(firmware, O_RDONLY)) == -1)
                err(EX_OSERR, "%s", firmware);
@@ -159,7 +153,9 @@
        if (fstat(fd, &st) == -1)
                err(EX_OSERR, "Unable to stat %s", firmware);
 
-       if ((map = mmap(NULL, st.st_size, PROT_READ, 0, fd, 0)) == NULL)
+       len = (size_t)st.st_size;
+       map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
+       if (map == MAP_FAILED)
                err(EX_OSERR, "Can't map %s into memory", firmware);
 
        if (do_req(iface, SIOCSLOADFW, map) == -1)
@@ -170,14 +166,14 @@
 }
 
 static void
-kill_firmware(char *iface)
+kill_firmware(const char *iface)
 {
        if (do_req(iface, SIOCSKILLFW, NULL) == -1)
                err(EX_OSERR, "Can't kill firmware");
 }
 
 static void
-get_radio_state(char *iface)
+get_radio_state(const char *iface)
 {
        int radio;
 
@@ -381,10 +377,10 @@
 };
 
 static void
-get_statistics(char *iface)
+get_statistics(const char *iface)
 {
        static unsigned long stats[256]; /* XXX */
-       const struct statistic *stat;
+       const struct statistic *st;
 
        if (do_req(iface, SIOCGTABLE1, stats) == -1) {
                if (errno == ENOTTY)
@@ -394,24 +390,24 @@
                        err(EX_OSERR, "Can't retrieve statistics");
        }
 
-       for (stat = tbl; stat->index != 0; stat++) {
-               (void)printf("%-60s[", stat->desc);
-               switch (stat->unit) {
+       for (st = tbl; st->index != 0; st++) {
+               (void)printf("%-60s[", st->desc);
+               switch (st->unit) {
                case INT:
-                       (void)printf("%lu", stats[stat->index]);
+                       (void)printf("%lu", stats[st->index]);
                        break;
                
                case BOOL:
-                       (void)printf(stats[stat->index] ? "true" : "false");
+                       (void)printf(stats[st->index] ? "true" : "false");
                        break;
 
                case PERCENTAGE:
-                       (void)printf("%lu%%", stats[stat->index]);
+                       (void)printf("%lu%%", stats[st->index]);
                        break;
 
                case HEX:
                default:
-                       (void)printf("0x%08lX", stats[stat->index]);
+                       (void)printf("0x%08lX", stats[st->index]);
                }
                (void)printf("]\n");
        }



Home | Main Index | Thread Index | Old Index