Module Name: src
Committed By: tnozaki
Date: Tue Nov 24 13:34:20 UTC 2009
Modified Files:
src/lib/libc/gen: basename.c dirname.c
Log Message:
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).
To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/lib/libc/gen/basename.c
cvs rdiff -u -r1.10 -r1.11 src/lib/libc/gen/dirname.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/lib/libc/gen/basename.c
diff -u src/lib/libc/gen/basename.c:1.8 src/lib/libc/gen/basename.c:1.9
--- src/lib/libc/gen/basename.c:1.8 Sat May 10 22:39:40 2008
+++ src/lib/libc/gen/basename.c Tue Nov 24 13:34:20 2009
@@ -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;
Index: src/lib/libc/gen/dirname.c
diff -u src/lib/libc/gen/dirname.c:1.10 src/lib/libc/gen/dirname.c:1.11
--- src/lib/libc/gen/dirname.c:1.10 Sat May 10 22:39:40 2008
+++ src/lib/libc/gen/dirname.c Tue Nov 24 13:34:20 2009
@@ -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