From owner-svn-src-stable@FreeBSD.ORG Wed Sep 22 18:00:35 2010 Return-Path: Delivered-To: svn-src-stable@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 5A6821065696; Wed, 22 Sep 2010 18:00:35 +0000 (UTC) (envelope-from rpaulo@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 488108FC12; Wed, 22 Sep 2010 18:00:35 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id o8MI0Z9P038220; Wed, 22 Sep 2010 18:00:35 GMT (envelope-from rpaulo@svn.freebsd.org) Received: (from rpaulo@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id o8MI0Ze3038214; Wed, 22 Sep 2010 18:00:35 GMT (envelope-from rpaulo@svn.freebsd.org) Message-Id: <201009221800.o8MI0Ze3038214@svn.freebsd.org> From: Rui Paulo Date: Wed, 22 Sep 2010 18:00:35 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org X-SVN-Group: stable-8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r213002 - in stable/8: include lib/libc/gen X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 22 Sep 2010 18:00:35 -0000 Author: rpaulo Date: Wed Sep 22 18:00:34 2010 New Revision: 213002 URL: http://svn.freebsd.org/changeset/base/213002 Log: MFC r197804 (rwatson): 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. Note about semantics: while this interface is not POSIXy, there's another major platform that uses it (Android) and the semantics between the two platforms are pretty much the same. Modified: stable/8/include/libgen.h stable/8/lib/libc/gen/Makefile.inc stable/8/lib/libc/gen/Symbol.map stable/8/lib/libc/gen/basename.3 stable/8/lib/libc/gen/basename.c Directory Properties: stable/8/include/ (props changed) stable/8/lib/libc/ (props changed) stable/8/lib/libc/locale/ (props changed) stable/8/lib/libc/stdtime/ (props changed) stable/8/lib/libc/sys/ (props changed) Modified: stable/8/include/libgen.h ============================================================================== --- stable/8/include/libgen.h Wed Sep 22 16:54:22 2010 (r213001) +++ stable/8/include/libgen.h Wed Sep 22 18:00:34 2010 (r213002) @@ -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 *, ...); Modified: stable/8/lib/libc/gen/Makefile.inc ============================================================================== --- stable/8/lib/libc/gen/Makefile.inc Wed Sep 22 16:54:22 2010 (r213001) +++ stable/8/lib/libc/gen/Makefile.inc Wed Sep 22 18:00:34 2010 (r213002) @@ -77,6 +77,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 Modified: stable/8/lib/libc/gen/Symbol.map ============================================================================== --- stable/8/lib/libc/gen/Symbol.map Wed Sep 22 16:54:22 2010 (r213001) +++ stable/8/lib/libc/gen/Symbol.map Wed Sep 22 18:00:34 2010 (r213002) @@ -366,6 +366,7 @@ FBSD_1.1 { }; FBSD_1.2 { + basename_r; getpagesizes; }; Modified: stable/8/lib/libc/gen/basename.3 ============================================================================== --- stable/8/lib/libc/gen/basename.3 Wed Sep 22 16:54:22 2010 (r213001) +++ stable/8/lib/libc/gen/basename.3 Wed Sep 22 18:00:34 2010 (r213002) @@ -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 Modified: stable/8/lib/libc/gen/basename.c ============================================================================== --- stable/8/lib/libc/gen/basename.c Wed Sep 22 16:54:22 2010 (r213001) +++ stable/8/lib/libc/gen/basename.c Wed Sep 22 18:00:34 2010 (r213002) @@ -40,18 +40,12 @@ __FBSDID("$FreeBSD$"); #include 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)); +}