Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/mixerctl Generate usage error for bad usage, before ...



details:   https://anonhg.NetBSD.org/src/rev/37898bc57fc4
branches:  trunk
changeset: 351719:37898bc57fc4
user:      kre <kre%NetBSD.org@localhost>
date:      Thu Feb 23 14:09:11 2017 +0000

description:
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.

diffstat:

 usr.bin/mixerctl/mixerctl.c |  32 +++++++++++++++++++++-----------
 1 files changed, 21 insertions(+), 11 deletions(-)

diffs (86 lines):

diff -r 367aaebd8061 -r 37898bc57fc4 usr.bin/mixerctl/mixerctl.c
--- a/usr.bin/mixerctl/mixerctl.c       Thu Feb 23 14:01:37 2017 +0000
+++ b/usr.bin/mixerctl/mixerctl.c       Thu Feb 23 14:09:11 2017 +0000
@@ -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 <sys/cdefs.h>
 
 #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 <stdio.h>
@@ -61,6 +61,8 @@
 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 @@
                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 @@
 
        file = getenv("MIXERDEVICE");
        if (file == NULL)
-               file = _PATH_MIXER;
+               file = mixer_path;
 
        prog = *argv;
 
@@ -352,19 +363,18 @@
                        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 @@
                        argv++;
                }
        } else
-               goto usage;
+               usage();
        exit(0);
 }



Home | Main Index | Thread Index | Old Index