Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/basename Clean up by using basename(3) from libc.



details:   https://anonhg.NetBSD.org/src/rev/52686d2696fc
branches:  trunk
changeset: 580004:52686d2696fc
user:      peter <peter%NetBSD.org@localhost>
date:      Mon Apr 04 16:47:54 2005 +0000

description:
Clean up by using basename(3) from libc.

diffstat:

 usr.bin/basename/basename.c |  68 +++++++++-----------------------------------
 1 files changed, 15 insertions(+), 53 deletions(-)

diffs (118 lines):

diff -r 9aa6fcf009cf -r 52686d2696fc usr.bin/basename/basename.c
--- a/usr.bin/basename/basename.c       Mon Apr 04 16:46:55 2005 +0000
+++ b/usr.bin/basename/basename.c       Mon Apr 04 16:47:54 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: basename.c,v 1.12 2005/02/17 16:57:27 xtraeme Exp $    */
+/*     $NetBSD: basename.c,v 1.13 2005/04/04 16:47:54 peter Exp $      */
 
 /*-
  * Copyright (c) 1991, 1993, 1994
@@ -39,16 +39,18 @@
 #if 0
 static char sccsid[] = "@(#)basename.c 8.4 (Berkeley) 5/4/95";
 #endif
-__RCSID("$NetBSD: basename.c,v 1.12 2005/02/17 16:57:27 xtraeme Exp $");
+__RCSID("$NetBSD: basename.c,v 1.13 2005/04/04 16:47:54 peter Exp $");
 #endif /* not lint */
 
+#include <err.h>
+#include <libgen.h>
+#include <locale.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <locale.h>
 #include <unistd.h>
 
-void usage(void);
+static void usage(void);
 
 int
 main(int argc, char **argv)
@@ -59,7 +61,7 @@
        setlocale(LC_ALL, "");
 
        while ((ch = getopt(argc, argv, "")) != -1)
-               switch(ch) {
+               switch (ch) {
                case '?':
                default:
                        usage();
@@ -70,53 +72,13 @@
        if (argc != 1 && argc != 2)
                usage();
 
-       /*
-        * (1) If string is // it is implementation defined whether steps (2)
-        *     through (5) are skipped or processed.
-        *
-        * (2) If string consists entirely of slash characters, string shall
-        *     be set to a single slash character.  In this case, skip steps
-        *     (3) through (5).
-        */
-       for (p = *argv;; ++p) {
-               if (!*p) {
-                       if (p > *argv)
-                               (void)printf("/\n");
-                       else
-                               (void)printf("\n");
-                       exit(0);
-               }
-               if (*p != '/')
-                       break;
+       if (**argv == '\0') {
+               (void)printf("\n");
+               exit(0);
        }
-
-       /*
-        * (3) If there are any trailing slash characters in string, they
-        *     shall be removed.
-        */
-       for (; *p; ++p)
-               continue;
-       while (*--p == '/')
-               continue;
-       *++p = '\0';
-
-       /*
-        * (4) If there are any slash characters remaining in string, the
-        *     prefix of string up to an including the last slash character
-        *     in string shall be removed.
-        */
-       while (--p >= *argv)
-               if (*p == '/')
-                       break;
-       ++p;
-
-       /*
-        * (5) If the suffix operand is present, is not identical to the
-        *     characters remaining in string, and is identical to a suffix
-        *     of the characters remaining in string, the suffix suffix
-        *     shall be removed from string.
-        */
-       if (*++argv) {
+       if ((p = basename(*argv)) == NULL)
+               err(1, "%s", *argv);
+       if (*++argv != '\0') {
                int suffixlen, stringlen, off;
 
                suffixlen = strlen(*argv);
@@ -124,7 +86,7 @@
 
                if (suffixlen < stringlen) {
                        off = stringlen - suffixlen;
-                       if (!strcmp(p + off, *argv))
+                       if (strcmp(p + off, *argv) == 0)
                                p[off] = '\0';
                }
        }
@@ -132,7 +94,7 @@
        exit(0);
 }
 
-void
+static void
 usage(void)
 {
 



Home | Main Index | Thread Index | Old Index