diff options
author | rwatson <rwatson@FreeBSD.org> | 2009-10-06 14:05:57 +0000 |
---|---|---|
committer | rwatson <rwatson@FreeBSD.org> | 2009-10-06 14:05:57 +0000 |
commit | fd2a94971be3ae7d27b361e6a81e7dea97a15966 (patch) | |
tree | a93ad50032ecad21316acf5415e575f4f5e2fc27 | |
parent | b1ce942125b70ba14ecffee512770eea9eb85b0f (diff) | |
download | FreeBSD-src-fd2a94971be3ae7d27b361e6a81e7dea97a15966.zip FreeBSD-src-fd2a94971be3ae7d27b361e6a81e7dea97a15966.tar.gz |
Add basename_r(3) to complement basename(3). basename_r(3) which accepts
a caller-allocated buffer of at least MAXPATHLEN, rather than using a
global buffer.
MFC after: 1 month
Sponsored by: Google
-rw-r--r-- | include/libgen.h | 1 | ||||
-rw-r--r-- | lib/libc/gen/Makefile.inc | 1 | ||||
-rw-r--r-- | lib/libc/gen/Symbol.map | 1 | ||||
-rw-r--r-- | lib/libc/gen/basename.3 | 20 | ||||
-rw-r--r-- | lib/libc/gen/basename.c | 24 |
5 files changed, 34 insertions, 13 deletions
diff --git a/include/libgen.h b/include/libgen.h index 3639c71..0d4472f 100644 --- a/include/libgen.h +++ b/include/libgen.h @@ -36,6 +36,7 @@ __BEGIN_DECLS char *basename(const char *); +char *basename_r(const char *, char *); char *dirname(const char *); #if 0 char *regcmp(const char *, ...); diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc index b06f846..dd9af85 100644 --- a/lib/libc/gen/Makefile.inc +++ b/lib/libc/gen/Makefile.inc @@ -76,6 +76,7 @@ MAN+= alarm.3 arc4random.3 \ MLINKS+=arc4random.3 arc4random_addrandom.3 arc4random.3 arc4random_stir.3 \ arc4random.3 arc4random_buf.3 arc4random.3 arc4random_uniform.3 +MLINKS+=basename.3 basename_r.3 MLINKS+=ctermid.3 ctermid_r.3 MLINKS+=devname.3 devname_r.3 MLINKS+=devname.3 fdevname.3 diff --git a/lib/libc/gen/Symbol.map b/lib/libc/gen/Symbol.map index 197430a..6fb61b1 100644 --- a/lib/libc/gen/Symbol.map +++ b/lib/libc/gen/Symbol.map @@ -367,6 +367,7 @@ FBSD_1.1 { }; FBSD_1.2 { + basename_r; getpagesizes; }; diff --git a/lib/libc/gen/basename.3 b/lib/libc/gen/basename.3 index 33a0a71..4a3743a 100644 --- a/lib/libc/gen/basename.3 +++ b/lib/libc/gen/basename.3 @@ -27,7 +27,7 @@ .\" $OpenBSD: basename.3,v 1.12 2000/04/18 03:01:25 aaron Exp $ .\" $FreeBSD$ .\" -.Dd October 12, 2006 +.Dd October 6, 2009 .Dt BASENAME 3 .Os .Sh NAME @@ -37,6 +37,8 @@ .In libgen.h .Ft char * .Fn basename "const char *path" +.Ft char * +.Fn basename_r "const char *path" "char *bname" .Sh DESCRIPTION The .Fn basename @@ -58,6 +60,12 @@ If is a null pointer or the empty string, a pointer to the string .Qq \&. is returned. +.Pp +The +.Fn basename_r +variation accepts a buffer of at least +.Dv MAXPATHLEN +bytes in which to store the resulting component. .Sh IMPLEMENTATION NOTES The .Fn basename @@ -65,15 +73,17 @@ function returns a pointer to internal storage space allocated on the first call that will be overwritten by subsequent calls. +.Fn basename_r +is therefore preferred for threaded applications. .Sh RETURN VALUES On successful completion, .Fn basename -returns a pointer to the last component of +and +.Fn basename_r +return pointers to the last component of .Fa path . .Pp -If -.Fn basename -fails, a null pointer is returned and the global variable +If they fail, a null pointer is returned and the global variable .Va errno is set to indicate the error. .Sh ERRORS diff --git a/lib/libc/gen/basename.c b/lib/libc/gen/basename.c index 9552ab3..9588c28 100644 --- a/lib/libc/gen/basename.c +++ b/lib/libc/gen/basename.c @@ -40,18 +40,12 @@ __FBSDID("$FreeBSD$"); #include <sys/param.h> char * -basename(path) +basename_r(path, bname) const char *path; + char *bname; { - static char *bname = NULL; const char *endp, *startp; - if (bname == NULL) { - bname = (char *)malloc(MAXPATHLEN); - if (bname == NULL) - return(NULL); - } - /* Empty or NULL string gets treated as "." */ if (path == NULL || *path == '\0') { (void)strcpy(bname, "."); @@ -82,3 +76,17 @@ basename(path) bname[endp - startp + 1] = '\0'; return(bname); } + +char * +basename(path) + const char *path; +{ + static char *bname = NULL; + + if (bname == NULL) { + bname = (char *)malloc(MAXPATHLEN); + if (bname == NULL) + return (NULL); + } + return (basename_r(path, bname)); +} |