Source-Changes-HG archive

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

[src/trunk]: src/lib/libc/gen guard single-dot from modification by the appli...



details:   https://anonhg.NetBSD.org/src/rev/57782b98535c
branches:  trunk
changeset: 749269:57782b98535c
user:      tnozaki <tnozaki%NetBSD.org@localhost>
date:      Tue Nov 24 13:34:20 2009 +0000

description:
guard single-dot from modification by the application
(SUSv3 spec don't forbid this brutal operation).
this idea taken from OpenBSD's version of basename(3) and dirname(3).

diffstat:

 lib/libc/gen/basename.c |  13 ++++++++-----
 lib/libc/gen/dirname.c  |  14 +++++++++-----
 2 files changed, 17 insertions(+), 10 deletions(-)

diffs (89 lines):

diff -r a7d953ef7f9d -r 57782b98535c lib/libc/gen/basename.c
--- a/lib/libc/gen/basename.c   Tue Nov 24 13:12:01 2009 +0000
+++ b/lib/libc/gen/basename.c   Tue Nov 24 13:34:20 2009 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: basename.c,v 1.8 2008/05/10 22:39:40 christos Exp $    */
+/*     $NetBSD: basename.c,v 1.9 2009/11/24 13:34:20 tnozaki Exp $     */
 
 /*-
  * Copyright (c) 1997, 2002 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: basename.c,v 1.8 2008/05/10 22:39:40 christos Exp $");
+__RCSID("$NetBSD: basename.c,v 1.9 2009/11/24 13:34:20 tnozaki Exp $");
 #endif /* !LIBC_SCCS && !lint */
 
 #include "namespace.h"
@@ -47,7 +47,6 @@
 char *
 basename(char *path)
 {
-       static char singledot[] = ".";
        static char result[PATH_MAX];
        const char *p, *lastp;
        size_t len;
@@ -56,8 +55,12 @@
         * If `path' is a null pointer or points to an empty string,
         * return a pointer to the string ".".
         */
-       if ((path == NULL) || (*path == '\0'))
-               return (singledot);
+       if ((path == NULL) || (*path == '\0')) {
+               result[0] = '.';
+               result[1] = '\0';
+
+               return (result);
+       }
 
        /* Strip trailing slashes, if any. */
        lastp = path + strlen(path) - 1;
diff -r a7d953ef7f9d -r 57782b98535c lib/libc/gen/dirname.c
--- a/lib/libc/gen/dirname.c    Tue Nov 24 13:12:01 2009 +0000
+++ b/lib/libc/gen/dirname.c    Tue Nov 24 13:34:20 2009 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dirname.c,v 1.10 2008/05/10 22:39:40 christos Exp $    */
+/*     $NetBSD: dirname.c,v 1.11 2009/11/24 13:34:20 tnozaki Exp $     */
 
 /*-
  * Copyright (c) 1997, 2002 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: dirname.c,v 1.10 2008/05/10 22:39:40 christos Exp $");
+__RCSID("$NetBSD: dirname.c,v 1.11 2009/11/24 13:34:20 tnozaki Exp $");
 #endif /* !LIBC_SCCS && !lint */
 
 #include "namespace.h"
@@ -47,7 +47,6 @@
 char *
 dirname(char *path)
 {
-       static char singledot[] = ".";
        static char result[PATH_MAX];
        const char *lastp;
        size_t len;
@@ -57,7 +56,8 @@
         * return a pointer to the string ".".
         */
        if ((path == NULL) || (*path == '\0'))
-               return (singledot);
+               goto singledot;
+
 
        /* Strip trailing slashes, if any. */
        lastp = path + strlen(path) - 1;
@@ -84,6 +84,10 @@
        } while (--lastp >= path);
 
        /* No /'s found, return a pointer to the string ".". */
-       return (singledot);
+singledot:
+       result[0] = '.';
+       result[1] = '\0';
+
+       return (result);
 }
 #endif



Home | Main Index | Thread Index | Old Index