Source-Changes-HG archive

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

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



details:   https://anonhg.NetBSD.org/src/rev/9aa6fcf009cf
branches:  trunk
changeset: 580003:9aa6fcf009cf
user:      peter <peter%NetBSD.org@localhost>
date:      Mon Apr 04 16:46:55 2005 +0000

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

Approved by christos.

diffstat:

 usr.bin/dirname/dirname.c |  82 +++++-----------------------------------------
 1 files changed, 10 insertions(+), 72 deletions(-)

diffs (123 lines):

diff -r 45a2e6b8fa4c -r 9aa6fcf009cf usr.bin/dirname/dirname.c
--- a/usr.bin/dirname/dirname.c Mon Apr 04 14:19:55 2005 +0000
+++ b/usr.bin/dirname/dirname.c Mon Apr 04 16:46:55 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dirname.c,v 1.9 2005/02/17 17:41:34 xtraeme Exp $      */
+/*     $NetBSD: dirname.c,v 1.10 2005/04/04 16:46:55 peter Exp $       */
 
 /*-
  * Copyright (c) 1991, 1993, 1994
@@ -39,12 +39,14 @@
 #if 0
 static char sccsid[] = "@(#)dirname.c  8.4 (Berkeley) 5/4/95";
 #endif
-__RCSID("$NetBSD: dirname.c,v 1.9 2005/02/17 17:41:34 xtraeme Exp $");
+__RCSID("$NetBSD: dirname.c,v 1.10 2005/04/04 16:46:55 peter Exp $");
 #endif /* not lint */
 
+#include <err.h>
+#include <libgen.h>
+#include <locale.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <locale.h>
 #include <unistd.h>
 
 static void usage(void);
@@ -52,13 +54,13 @@
 int
 main(int argc, char **argv)
 {
-       register char *p;
+       char *p;
        int ch;
 
        setlocale(LC_ALL, "");
 
        while ((ch = getopt(argc, argv, "")) != -1)
-               switch(ch) {
+               switch (ch) {
                case '?':
                default:
                        usage();
@@ -69,72 +71,9 @@
        if (argc != 1)
                usage();
 
-       /*
-        * (1) If string is //, skip steps (2) through (5).
-        * (2) If string consists entirely of slash characters, string
-        *     shall be set to a single slash character.  In this case,
-        *     skip steps (3) through (8).
-        */
-       for (p = *argv;; ++p) {
-               if (!*p) {
-                       if (p > *argv)
-                               (void)printf("/\n");
-                       else
-                               (void)printf(".\n");
-                       exit(0);
-               }
-               if (*p != '/')
-                       break;
-       }
-
-       /*
-        * (3) If there are any trailing slash characters in string, they
-        *     shall be removed.
-        */
-       for (; *p; ++p);
-       while (*--p == '/')
-               continue;
-       *++p = '\0';
-
-       /*
-        * (4) If there are no slash characters remaining in string,
-        *     string shall be set to a single period character.  In this
-        *     case skip steps (5) through (8).
-        *
-        * (5) If there are any trailing nonslash characters in string,
-        *     they shall be removed.
-        */
-       while (--p >= *argv)
-               if (*p == '/')
-                       break;
-       ++p;
-       if (p == *argv) {
-               (void)printf(".\n");
-               exit(0);
-       }
-
-       /*
-        * (6) If the remaining string is //, it is implementation defined
-        *     whether steps (7) and (8) are skipped or processed.
-        *
-        * This case has already been handled, as part of steps (1) and (2).
-        */
-       
-       /*
-        * (7) If there are any trailing slash characters in string, they
-        *     shall be removed.
-        */
-       while (--p >= *argv)
-               if (*p != '/')
-                       break;
-       ++p;
-
-       /*
-        * (8) If the remaining string is empty, string shall be set to
-        *     a single slash character.
-        */
-       *p = '\0';
-       (void)printf("%s\n", p == *argv ? "/" : *argv);
+       if ((p = dirname(*argv)) == NULL)
+               err(1, "%s", *argv);
+       (void)printf("%s\n", p);
        exit(0);
 }
 
@@ -145,4 +84,3 @@
        (void)fprintf(stderr, "usage: dirname path\n");
        exit(1);
 }
-



Home | Main Index | Thread Index | Old Index