Add basename() and dirname(), from XPG4.2.

This commit is contained in:
kleink 1997-11-02 16:48:25 +00:00
parent aac78bc58f
commit 8bfc0cef1e
6 changed files with 332 additions and 6 deletions

View File

@ -1,13 +1,13 @@
# $NetBSD: Makefile.inc,v 1.81 1997/10/21 00:56:42 fvdl Exp $
# $NetBSD: Makefile.inc,v 1.82 1997/11/02 16:48:27 kleink Exp $
# @(#)Makefile.inc 8.3 (Berkeley) 4/16/94
# gen sources
.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/gen ${.CURDIR}/gen
SRCS+= _errno.c alarm.c assert.c clock.c closedir.c confstr.c ctermid.c \
ctype_.c daemon.c devname.c disklabel.c err.c errx.c errlist.c \
errno.c execl.c execle.c execlp.c execv.c execvp.c fnmatch.c fstab.c \
ftok.c __fts13.c fts.c getbsize.c getcap.c \
SRCS+= _errno.c alarm.c assert.c basename.c clock.c closedir.c confstr.c \
ctermid.c ctype_.c daemon.c devname.c dirname.c disklabel.c err.c \
errx.c errlist.c errno.c execl.c execle.c execlp.c execv.c execvp.c \
fnmatch.c fstab.c ftok.c __fts13.c fts.c getbsize.c getcap.c \
getcwd.c getdomainname.c getgrent.c getgrouplist.c gethostname.c \
getloadavg.c getlogin.c getmntinfo.c getnetgrent.c getpagesize.c \
getpass.c getpwent.c getsubopt.c getttyent.c getusershell.c __glob13.c \

83
lib/libc/gen/basename.3 Normal file
View File

@ -0,0 +1,83 @@
.\" $NetBSD: basename.3,v 1.1 1997/11/02 16:48:27 kleink Exp $
.\"
.\" Copyright (c) 1997 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Klaus Klein.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the NetBSD
.\" Foundation, Inc. and its contributors.
.\" 4. Neither the name of The NetBSD Foundation nor the names of its
.\" contributors may be used to endorse or promote products derived
.\" from this software without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd Oct 31, 1997
.Dt BASENAME 3
.Os NetBSD
.Sh NAME
.Nm basename
.Nd return the last component of a pathname
.Sh SYNOPSIS
.Fd #include <libgen.h>
.Ft char *
.Fn basename "char *path"
.Sh DESCRIPTION
The
.Fn basename
function takes the pathname pointed to by
.Ar path
and returns a pointer to the final component of the pathname,
deleting and trailing '/' characters.
.Pp
If
.Ar path
consists entirely of '/' characters,
.Fn basename
returns a pointer to the string "/".
.Pp
If
.Ar path
is a null pointer or points to an empty string,
.Fn basename
returns a pointer to the string ".".
.Pp
.Sh RETURN VALUES
The
.Fn basename
function returns a pointer to the final component of
.Ar path .
.Sh SEE ALSO
.Xr dirname 3
.Sh STANDARDS
.Bl -bullet -compact
.It
.St -xpg4.2
.El
.Sh BUGS
The
.Fn basename
function may modify the string pointed to by
.Ar path .

77
lib/libc/gen/basename.c Normal file
View File

@ -0,0 +1,77 @@
/* $NetBSD: basename.c,v 1.1 1997/11/02 16:48:28 kleink Exp $ */
/*-
* Copyright (c) 1997 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Klaus Klein.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: basename.c,v 1.1 1997/11/02 16:48:28 kleink Exp $");
#endif /* !LIBC_SCCS && !lint */
#include "namespace.h"
#include <libgen.h>
#include <string.h>
#ifdef __weak_alias
__weak_alias(basename,_basename);
#endif
char *
basename(path)
char *path;
{
static char singledot[] = ".";
char *p;
/*
* 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);
/* Strip trailing slashes, if any. */
p = path + strlen(path) - 1;
while (*p == '/' && p != path)
*p-- = '\0';
/* Return pointer to the final pathname component. */
if (((p = strrchr(path, '/')) == NULL) || (*(p + 1) == '\0'))
return (path);
else
return (p + 1);
}

86
lib/libc/gen/dirname.3 Normal file
View File

@ -0,0 +1,86 @@
.\" $NetBSD: dirname.3,v 1.1 1997/11/02 16:48:28 kleink Exp $
.\"
.\" Copyright (c) 1997 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Klaus Klein.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the NetBSD
.\" Foundation, Inc. and its contributors.
.\" 4. Neither the name of The NetBSD Foundation nor the names of its
.\" contributors may be used to endorse or promote products derived
.\" from this software without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd Nov 2, 1997
.Dt DIRNAME 3
.Os NetBSD
.Sh NAME
.Nm dirname
.Nd report the parent directory name of a file pathname
.Sh SYNOPSIS
.Fd #include <libgen.h>
.Ft char *
.Fn dirname "char *path"
.Sh DESCRIPTION
The
.Fn dirname
function takes a pointer to a character string that contains a pathname,
.Ar path ,
and returns a pointer to a string that is a pathname of the parent directory of
.Ar path .
Trailing '/' characters in
.Ar path
are not counted as part of the path.
.Pp
If
.Ar path
does not contain a '/', then
.Fn dirname
returns a pointer to the string ".".
.Pp
If
.Ar path
is a null pointer or points to an empty string,
.Fn dirname
returns a pointer to the string ".".
.Pp
.Sh RETURN VALUES
The
.Fn dirname
function returns a pointer to a string that is the parent directory of
.Ar path .
.Sh SEE ALSO
.Xr basename 3
.Sh STANDARDS
.Bl -bullet -compact
.It
.St -xpg4.2
.El
.Sh BUGS
The
.Fn dirname
function may modify the string pointed to by
.Ar path .

78
lib/libc/gen/dirname.c Normal file
View File

@ -0,0 +1,78 @@
/* $NetBSD: dirname.c,v 1.1 1997/11/02 16:48:29 kleink Exp $ */
/*-
* Copyright (c) 1997 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Klaus Klein.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: dirname.c,v 1.1 1997/11/02 16:48:29 kleink Exp $");
#endif /* !LIBC_SCCS && !lint */
#include "namespace.h"
#include <libgen.h>
#include <string.h>
#ifdef __weak_alias
__weak_alias(dirname,_dirname);
#endif
char *
dirname(path)
char *path;
{
static char singledot[] = ".";
char *p;
/*
* If `path' is a null pointer or points to an empty string, or does
* not contain a '/', return a pointer to the string ".".
*/
if ((path == NULL) || (*path == '\0') ||
(strchr(path, '/') == NULL))
return (singledot);
/* Strip trailing slashes, if any. */
p = path + strlen(path) - 1;
while (*p == '/' && p != path)
*p-- = '\0';
/* Terminate path at the last occurence of '/'. */
if ((p = strrchr(path, '/')) != NULL)
*(p + (p == path ? 1 : 0)) = '\0';
return (path);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: namespace.h,v 1.4 1997/07/21 14:17:52 jtc Exp $ */
/* $NetBSD: namespace.h,v 1.5 1997/11/02 16:48:25 kleink Exp $ */
/*-
* Copyright (c) 1997 The NetBSD Foundation, Inc.
@ -62,6 +62,7 @@
#define authnone_create _authnone_create
#define authunix_create _authunix_create
#define authunix_create_default _authunix_create_default
#define basename _basename
#define bindresvport _bindresvport
#define bm_comp _bm_comp
#define bm_exec _bm_exec
@ -112,6 +113,7 @@
#define dbm_store _dbm_store
#define dbopen _dbopen
#define devname _devname
#define dirname _dirname
#define drand48 _drand48
#define endfsent _endfsent
#define endgrent _endgrent