From 887a30551ef623e840577f53bd34cbf839bb6d55 Mon Sep 17 00:00:00 2001 From: kre Date: Thu, 23 Feb 2017 14:09:11 +0000 Subject: [PATCH] Generate usage error for bad usage, before attempting any other operations. This means that "mixerctl" (no args) will generate a usage msg, even when /dev/mixer cannot be opened (or any other device given via -d or $MIXERDEVICE) While here, get rid of "goto usage" replacing the usage: with a static inline void __dead function... The compiler ought to optimise the calls into essentially the same code as existed with the goto version, but this is much cleaner. Also, mixerctl falls back on /dev/mixer0 if /dev/mixer cannot be opened. (that is old code - probably from when /dev/mixer was first added) It used to do that when called as mixerctl -d /dev/mixer or with "MIXERDEVICE=/dev/mixer mixerctl...". No longer. Now the fallback (which is probably obsolete now anyway) only happens when the user doesn't specify any mixer device (by either method) and the default of /dev/mixer is used. In other cases, only the device specified is tried. --- usr.bin/mixerctl/mixerctl.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/usr.bin/mixerctl/mixerctl.c b/usr.bin/mixerctl/mixerctl.c index 2c655b3e9b3a..76d7c20c4bab 100644 --- a/usr.bin/mixerctl/mixerctl.c +++ b/usr.bin/mixerctl/mixerctl.c @@ -1,4 +1,4 @@ -/* $NetBSD: mixerctl.c,v 1.26 2012/10/28 02:01:15 isaki Exp $ */ +/* $NetBSD: mixerctl.c,v 1.27 2017/02/23 14:09:11 kre Exp $ */ /* * Copyright (c) 1997 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include #ifndef lint -__RCSID("$NetBSD: mixerctl.c,v 1.26 2012/10/28 02:01:15 isaki Exp $"); +__RCSID("$NetBSD: mixerctl.c,v 1.27 2017/02/23 14:09:11 kre Exp $"); #endif #include @@ -61,6 +61,8 @@ struct field { mixer_ctrl_t *values; mixer_devinfo_t *infos; +static const char mixer_path[] = _PATH_MIXER; + static char * catstr(char *p, char *q) { @@ -316,6 +318,15 @@ prarg(int fd, char *arg, const char *sep) prfield(p, sep, vflag), fprintf(out, "\n"); } +static inline void __dead +usage(void) +{ + fprintf(out, "%s [-d file] [-v] [-n] name ...\n", prog); + fprintf(out, "%s [-d file] [-v] [-n] -w name=value ...\n",prog); + fprintf(out, "%s [-d file] [-v] [-n] -a\n", prog); + exit(0); +} + int main(int argc, char **argv) { @@ -328,7 +339,7 @@ main(int argc, char **argv) file = getenv("MIXERDEVICE"); if (file == NULL) - file = _PATH_MIXER; + file = mixer_path; prog = *argv; @@ -352,19 +363,18 @@ main(int argc, char **argv) break; case '?': default: - usage: - fprintf(out, "%s [-d file] [-v] [-n] name ...\n", prog); - fprintf(out, "%s [-d file] [-v] [-n] -w name=value ...\n",prog); - fprintf(out, "%s [-d file] [-v] [-n] -a\n", prog); - exit(0); + usage(); } } argc -= optind; argv += optind; + if (aflag ? (argc != 0 || wflag) : argc == 0) + usage(); + fd = open(file, O_RDWR); - /* Try with mixer0. */ - if (fd < 0 && strcmp(file, _PATH_MIXER) == 0) { + /* Try with mixer0 but only if using the default device. */ + if (fd < 0 && file == mixer_path) { file = _PATH_MIXER0; fd = open(file, O_RDWR); } @@ -442,6 +452,6 @@ main(int argc, char **argv) argv++; } } else - goto usage; + usage(); exit(0); }