- Sprinkle const
- Fix mmap call [MAP_PRIVATE], return [MAP_FAILED]
- Don't clobber errno.
- Use getprogname/setprogname
This commit is contained in:
christos 2005-04-03 17:21:29 +00:00
parent 216c7eb47d
commit 3348f64feb
2 changed files with 39 additions and 40 deletions

View File

@ -1,5 +1,6 @@
# $NetBSD: Makefile,v 1.2 2005/01/11 18:52:11 skrll Exp $ # $NetBSD: Makefile,v 1.3 2005/04/03 17:21:29 christos Exp $
WARNS=3
.if ${MACHINE} == "i386" .if ${MACHINE} == "i386"
PROG = iwictl PROG = iwictl
.endif .endif

View File

@ -1,4 +1,4 @@
/* $NetBSD: iwictl.c,v 1.2 2005/01/11 18:52:11 skrll Exp $ */ /* $NetBSD: iwictl.c,v 1.3 2005/04/03 17:21:29 christos Exp $ */
/*- /*-
* Copyright (c) 2004, 2005 * Copyright (c) 2004, 2005
@ -28,7 +28,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__RCSID("$NetBSD: iwictl.c,v 1.2 2005/01/11 18:52:11 skrll Exp $"); __RCSID("$NetBSD: iwictl.c,v 1.3 2005/04/03 17:21:29 christos Exp $");
#include <sys/types.h> #include <sys/types.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
@ -66,24 +66,23 @@ struct header {
u_int32_t mode; u_int32_t mode;
} __attribute__((__packed__)); } __attribute__((__packed__));
extern char *optarg; static void usage(void) __attribute__((__noreturn__));
extern int optind; static int do_req(const char *, unsigned long, void *);
static void mmap_file(const char *, void **, size_t *);
static void usage(void); static void load_firmware(const char *, const char *, const char *);
static int do_req(char *, unsigned long, void *); static void kill_firmware(const char *);
static void mmap_file(char *, void **, size_t *); static void get_radio_state(const char *);
static void load_firmware(char *, char *, char *); static void get_statistics(const char *);
static void kill_firmware(char *);
static void get_radio_state(char *);
static void get_statistics(char *);
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
int ch; int ch;
char *iface = NULL, *mode = "bss", *path = NULL; char *iface = NULL, *path = NULL;
const char *mode = "bss";
int noflag = 1, kflag = 0, rflag = 0; int noflag = 1, kflag = 0, rflag = 0;
setprogname(argv[0]);
if (argc > 1 && argv[1][0] != '-') { if (argc > 1 && argv[1][0] != '-') {
iface = argv[1]; iface = argv[1];
optind++; optind++;
@ -143,39 +142,38 @@ main(int argc, char **argv)
static void static void
usage(void) usage(void)
{ {
extern char *__progname; (void)fprintf(stderr, "Usage: %s iface\n"
(void)fprintf(stderr, "usage: %s iface\n"
"\t%s iface -d path [-m bss|ibss]\n" "\t%s iface -d path [-m bss|ibss]\n"
"\t%s iface -k\n" "\t%s iface -k\n"
"\t%s iface -r\n", __progname, __progname, __progname, "\t%s iface -r\n", getprogname(), getprogname(), getprogname(),
__progname); getprogname());
exit(EX_USAGE); exit(EX_USAGE);
} }
static int static int
do_req(char *iface, unsigned long req, void *data) do_req(const char *iface, unsigned long req, void *data)
{ {
int s; int s;
struct ifreq ifr; struct ifreq ifr;
int error; int error, serrno;
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
err(EX_OSERR, "Can't create socket"); err(EX_OSERR, "Can't create socket");
(void)memset(&ifr, 0, sizeof ifr); (void)memset(&ifr, 0, sizeof(ifr));
(void)strncpy(ifr.ifr_name, iface, sizeof ifr.ifr_name); (void)strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
ifr.ifr_data = data; ifr.ifr_data = data;
error = ioctl(s, req, &ifr); error = ioctl(s, req, &ifr);
serrno = errno;
(void)close(s); (void)close(s);
errno = serrno;
return error; return error;
} }
static void static void
mmap_file(char *filename, void **addr, size_t *len) mmap_file(const char *filename, void **addr, size_t *len)
{ {
int fd; int fd;
struct stat st; struct stat st;
@ -186,31 +184,32 @@ mmap_file(char *filename, void **addr, size_t *len)
if (fstat(fd, &st) == -1) if (fstat(fd, &st) == -1)
err(EX_OSERR, "Unable to stat %s", filename); err(EX_OSERR, "Unable to stat %s", filename);
*len = st.st_size; *len = (size_t)st.st_size;
if ((*addr = mmap(NULL, st.st_size, PROT_READ, 0, fd, 0)) == NULL) *addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
if (*addr == MAP_FAILED)
err(EX_OSERR, "Can't map %s into memory", filename); err(EX_OSERR, "Can't map %s into memory", filename);
*(char **)addr += sizeof (struct header); *(char **)addr += sizeof(struct header);
*len -= sizeof (struct header); *len -= sizeof(struct header);
(void)close(fd); (void)close(fd);
} }
static void static void
load_firmware(char *iface, char *path, char *mode) load_firmware(const char *iface, const char *path, const char *mode)
{ {
char filename[FILENAME_MAX]; char filename[FILENAME_MAX];
struct firmware fw; struct firmware fw;
(void)snprintf(filename, sizeof filename, "%s/iwi-boot.fw", path); (void)snprintf(filename, sizeof(filename), "%s/iwi-boot.fw", path);
mmap_file(filename, &fw.boot, &fw.boot_size); mmap_file(filename, &fw.boot, &fw.boot_size);
(void)snprintf(filename, sizeof filename, "%s/iwi-ucode-%s.fw", path, (void)snprintf(filename, sizeof(filename), "%s/iwi-ucode-%s.fw", path,
mode); mode);
mmap_file(filename, &fw.ucode, &fw.ucode_size); mmap_file(filename, &fw.ucode, &fw.ucode_size);
(void)snprintf(filename, sizeof filename, "%s/iwi-%s.fw", path, mode); (void)snprintf(filename, sizeof(filename), "%s/iwi-%s.fw", path, mode);
mmap_file(filename, &fw.main, &fw.main_size); mmap_file(filename, &fw.main, &fw.main_size);
if (do_req(iface, SIOCSLOADFW, &fw) == -1) if (do_req(iface, SIOCSLOADFW, &fw) == -1)
@ -218,14 +217,14 @@ load_firmware(char *iface, char *path, char *mode)
} }
static void static void
kill_firmware(char *iface) kill_firmware(const char *iface)
{ {
if (do_req(iface, SIOCSKILLFW, NULL) == -1) if (do_req(iface, SIOCSKILLFW, NULL) == -1)
err(EX_OSERR, "Can't kill firmware"); err(EX_OSERR, "Can't kill firmware");
} }
static void static void
get_radio_state(char *iface) get_radio_state(const char *iface)
{ {
int radio; int radio;
@ -298,15 +297,14 @@ static const struct statistic tbl[] = {
}; };
static void static void
get_statistics(char *iface) get_statistics(const char *iface)
{ {
static u_int32_t stats[256]; static u_int32_t stats[256];
const struct statistic *stat; const struct statistic *st;
if (do_req(iface, SIOCGTABLE0, stats) == -1) if (do_req(iface, SIOCGTABLE0, stats) == -1)
err(EX_OSERR, "Can't read statistics"); err(EX_OSERR, "Can't read statistics");
for (stat = tbl; stat->index != 0; stat++) for (st = tbl; st->index != 0; st++)
(void)printf("%-60s[%u]\n", stat->desc, stats[stat->index]); (void)printf("%-60s[%u]\n", st->desc, stats[st->index]);
} }