Source-Changes-HG archive

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

[src/trunk]: src/bin/mt Allow short command names as long as they are not amb...



details:   https://anonhg.NetBSD.org/src/rev/a4a0bf94db02
branches:  trunk
changeset: 573619:a4a0bf94db02
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Feb 03 15:15:48 2005 +0000

description:
Allow short command names as long as they are not ambiguous.

diffstat:

 bin/mt/mt.c |  26 +++++++++++++++++---------
 1 files changed, 17 insertions(+), 9 deletions(-)

diffs (68 lines):

diff -r d045079dcfd6 -r a4a0bf94db02 bin/mt/mt.c
--- a/bin/mt/mt.c       Thu Feb 03 15:14:39 2005 +0000
+++ b/bin/mt/mt.c       Thu Feb 03 15:15:48 2005 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: mt.c,v 1.38 2005/02/03 00:03:02 christos Exp $ */
+/* $NetBSD: mt.c,v 1.39 2005/02/03 15:15:48 christos Exp $ */
 
 /*
  * Copyright (c) 1980, 1993
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = "@(#)mt.c       8.2 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: mt.c,v 1.38 2005/02/03 00:03:02 christos Exp $");
+__RCSID("$NetBSD: mt.c,v 1.39 2005/02/03 15:15:48 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -48,6 +48,7 @@
  *   magnetic tape manipulation program
  */
 #include <sys/types.h>
+#include <sys/param.h>
 #include <sys/ioctl.h>
 #include <sys/mtio.h>
 #include <sys/stat.h>
@@ -111,13 +112,14 @@
 int
 main(int argc, char *argv[])
 {
-       const struct commands *comp = (const struct commands *) NULL;
+       const struct commands *cp, *comp;
        struct mtget mt_status;
        struct mtop mt_com;
        int ch, mtfd, flags;
        char *p;
        const char *tape;
        int count;
+       size_t len;
 
        setprogname(argv[0]);
        if ((tape = getenv("TAPE")) == NULL)
@@ -139,13 +141,19 @@
        if (argc < 1 || argc > 2)
                usage();
 
-       p = *argv++;
-       for (comp = com;; comp++) {
-               if (comp->c_name == NULL)
-                       errx(1, "%s: unknown command", p);
-               if (strncmp(p, comp->c_name, comp->c_namelen) == 0)
-                       break;
+       len = strlen(p = *argv++);
+       for (comp = NULL, cp = com; cp->c_name != NULL; cp++) {
+               size_t clen = MIN(len, cp->c_namelen);
+               if (strncmp(p, cp->c_name, clen) == 0) {
+                       if (comp != NULL)
+                               errx(1, "%s: Ambiguous command `%s' or `%s'?",
+                                   p, cp->c_name, comp->c_name);
+                       else
+                               comp = cp;
+               }
        }
+       if (comp == NULL)
+               errx(1, "%s: unknown command", p);
 
        if (*argv) {
                count = strtol(*argv, &p, 10);



Home | Main Index | Thread Index | Old Index