Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 4 Aug 1999 09:03:58 -0400 (EDT)
From:      James Howard <howardjp@wam.umd.edu>
To:        FreeBSD-gnats-submit@freebsd.org
Subject:   bin/12962: basename(3) and dirname(3) part 2
Message-ID:  <199908041303.JAA10126@rac3.wam.umd.edu>

next in thread | raw e-mail | index | archive | help

>Number:         12962
>Category:       bin
>Synopsis:       basename(3) and dirname(3) part 2
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Wed Aug  4 06:10:01 PDT 1999
>Closed-Date:
>Last-Modified:
>Originator:     James Howard
>Release:        FreeBSD 3.2-STABLE i386
>Organization:
University of Maryland
>Environment:

FreeBSD byzantine.student.umd.edu 3.2-STABLE FreeBSD 3.2-STABLE #4: Thu Jun 17 18:40:41 GMT 1999     howardjp@byzantine.student.umd.edu:/usr/src/sys/compile/BYZANTINE  i386

>Description:

See PR bin/12960

Here are basename.[3c] and dirname.[3c].  They are in a shar archive.
These should be dropped into /usr/src/lib/libc/gen along with the 
correct changes to the Makefiles.  I stole them from OpenBSD.  There
are more details in PR bin/12960.

Once again, I had some problems with diff and large directories.

>How-To-Repeat:

	<Code/input/activities to reproduce the problem (multiple lines)>

>Fix:
	
# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	basename.3
#	basename.c
#	dirname.3
#	dirname.c
#
echo x - basename.3
sed 's/^X//' >basename.3 << 'END-of-basename.3'
X.\"
X.\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
X.\" All rights reserved.
X.\"
X.\" Redistribution and use in source and binary forms, with or without
X.\" modification, are permitted provided that the following conditions
X.\" are met:
X.\" 1. Redistributions of source code must retain the above copyright
X.\"    notice, this list of conditions and the following disclaimer.
X.\" 2. Redistributions in binary form must reproduce the above copyright
X.\"    notice, this list of conditions and the following disclaimer in the
X.\"    documentation and/or other materials provided with the distribution.
X.\" 3. The name of the author may not be used to endorse or promote products
X.\"    derived from this software without specific prior written permission.
X.\"
X.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
X.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
X.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
X.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
X.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
X.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
X.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
X.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
X.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
X.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
X.\"
X.\" $OpenBSD: basename.3,v 1.11 1999/06/04 01:30:10 aaron Exp $
X.\"
X.Dd August 17, 1997
X.Dt BASENAME 3
X.Os
X.Sh NAME
X.Nm basename
X.Nd extract the base portion of a pathname
X.Sh SYNOPSIS
X.Fd #include <libgen.h>
X.Ft char *
X.Fn basename "const char *path"
X.Sh DESCRIPTION
XThe
X.Fn basename
Xfunction
Xreturns the last component from the pathname pointed to by
X.Ar path ,
Xdeleting any trailing
X.Sq \&/
Xcharacters.  If
X.Ar path
Xconsists entirely of
X.Sq \&/
Xcharacters, a pointer to the string
X.Qq \&/
Xis returned.  If
X.Ar path
Xis a null pointer or the empty string, a pointer to the string
X.Qq \&.
Xis returned.
X.Sh RETURN VALUES
XOn successful completion,
X.Fn basename
Xreturns a pointer to the last component of
X.Ar path .
X.Pp
XIf
X.Fn basename
Xfails, a null pointer is returned and the global variable
X.Va errno
Xis set to indicate the error.
X.Sh ERRORS
XThe following error codes may be set in
X.Va errno :
X.Bl -tag -width Er
X.It Bq Er ENAMETOOLONG
XThe path component to be returned was larger than
X.Dv MAXPATHLEN .
X.El
X.Sh WARNINGS
X.Fn basename
Xreturns a pointer to internal static storage space that will be overwritten
Xby subsequent calls.
X.Sh SEE ALSO
X.Xr basename 1 ,
X.Xr dirname 1 ,
X.Xr dirname 3
X.Sh STANDARDS
XThe
X.Fn basename
Xfunction conforms to
X.St -xpg4.2 .
X.Sh HISTORY
XThe
X.Fn basename
Xfunction first appeared in
X.Ox 2.2 .
X.Sh AUTHOR
XTodd C. Miller <Todd.Miller@courtesan.com>
END-of-basename.3
echo x - basename.c
sed 's/^X//' >basename.c << 'END-of-basename.c'
X/*	$OpenBSD: basename.c,v 1.4 1999/05/30 17:10:30 espie Exp $	*/
X
X/*
X * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
X * All rights reserved.
X *
X * Redistribution and use in source and binary forms, with or without
X * modification, are permitted provided that the following conditions
X * are met:
X * 1. Redistributions of source code must retain the above copyright
X *    notice, this list of conditions and the following disclaimer.
X * 2. Redistributions in binary form must reproduce the above copyright
X *    notice, this list of conditions and the following disclaimer in the
X *    documentation and/or other materials provided with the distribution.
X * 3. The name of the author may not be used to endorse or promote products
X *    derived from this software without specific prior written permission.
X *
X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
X * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
X * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
X * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
X * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
X * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
X * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
X * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
X * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
X * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
X */
X
X#ifndef lint
Xstatic char rcsid[] = "$OpenBSD: basename.c,v 1.4 1999/05/30 17:10:30 espie Exp $";
X#endif /* not lint */
X
X#include <errno.h>
X#include <libgen.h>
X#include <string.h>
X#include <sys/param.h>
X
Xchar *
Xbasename(path)
X	const char *path;
X{
X	static char bname[MAXPATHLEN];
X	register const char *endp, *startp;
X
X	/* Empty or NULL string gets treated as "." */
X	if (path == NULL || *path == '\0') {
X		(void)strcpy(bname, ".");
X		return(bname);
X	}
X
X	/* Strip trailing slashes */
X	endp = path + strlen(path) - 1;
X	while (endp > path && *endp == '/')
X		endp--;
X
X	/* All slashes becomes "/" */
X	if (endp == path && *endp == '/') {
X		(void)strcpy(bname, "/");
X		return(bname);
X	}
X
X	/* Find the start of the base */
X	startp = endp;
X	while (startp > path && *(startp - 1) != '/')
X		startp--;
X
X	if (endp - startp + 1 > sizeof(bname)) {
X		errno = ENAMETOOLONG;
X		return(NULL);
X	}
X	(void)strncpy(bname, startp, endp - startp + 1);
X	bname[endp - startp + 1] = '\0';
X	return(bname);
X}
END-of-basename.c
echo x - dirname.3
sed 's/^X//' >dirname.3 << 'END-of-dirname.3'
X.\"
X.\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
X.\" All rights reserved.
X.\"
X.\" Redistribution and use in source and binary forms, with or without
X.\" modification, are permitted provided that the following conditions
X.\" are met:
X.\" 1. Redistributions of source code must retain the above copyright
X.\"    notice, this list of conditions and the following disclaimer.
X.\" 2. Redistributions in binary form must reproduce the above copyright
X.\"    notice, this list of conditions and the following disclaimer in the
X.\"    documentation and/or other materials provided with the distribution.
X.\" 3. The name of the author may not be used to endorse or promote products
X.\"    derived from this software without specific prior written permission.
X.\"
X.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
X.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
X.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
X.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
X.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
X.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
X.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
X.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
X.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
X.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
X.\"
X.\" $OpenBSD: dirname.3,v 1.8 1999/07/17 09:32:30 downsj Exp $
X.\"
X.Dd August 17, 1997
X.Dt DIRNAME 3
X.Os
X.Sh NAME
X.Nm dirname
X.Nd extract the directory portition of a pathname
X.Sh SYNOPSIS
X.Fd #include <libgen.h>
X.Ft char *
X.Fn dirname "const char *path"
X.Sh DESCRIPTION
XThe
X.Fn dirname
Xfunction
Xis the converse of
X.Xr basename 3 ;
Xit returns a pointer to the parent directory of the pathname pointed to by
X.Ar path .
XAny trailing
X.Sq \&/
Xcharacters are not counted as part of the directory
Xname.  If
X.Ar path
Xis a null pointer, the empty string, or contains no
X.Sq \&/
Xcharacters,
X.Fn dirname
Xreturns a pointer to the string
X.Qq \&. ,
Xsignifying the current directory.
X.Sh RETURN VALUES
XOn successful completion,
X.Fn dirname
Xreturns a pointer to the parent directory of
X.Ar path .
X.Pp
XIf
X.Fn dirname
Xfails, a null pointer is returned and the global variable
X.Va errno
Xis set to indicate the error.
X.Sh ERRORS
XThe following error codes may be set in
X.Va errno :
X.Bl -tag -width Er
X.It Bq Er ENAMETOOLONG
XThe path component to be returned was larger than
X.Dv MAXPATHLEN .
X.El
X.Sh WARNINGS
X.Fn dirname
Xreturns a pointer to internal static storage space that will be overwritten
Xby subsequent calls (each function has its own separate storage).
X.Pp
XOther vendor implementations of
X.Fn dirname
Xmay modify the contents of the string passed to
X.Fn dirname ;
Xthis should be taken into account when writing code which calls this function
Xif portability is desired.
X.Sh SEE ALSO
X.Xr basename 1 ,
X.Xr dirname 1 ,
X.Xr basename 3
X.Sh STANDARDS
XThe
X.Fn dirname
Xfunction conforms to
X.St -xpg4.2 .
X.Sh HISTORY
XThe
X.Fn dirname
Xfunction first appeared in
X.Ox 2.2 .
X.Sh AUTHOR
XTodd C. Miller <Todd.Miller@courtesan.com>
END-of-dirname.3
echo x - dirname.c
sed 's/^X//' >dirname.c << 'END-of-dirname.c'
X/*	$OpenBSD: dirname.c,v 1.4 1999/05/30 17:10:30 espie Exp $	*/
X
X/*
X * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
X * All rights reserved.
X *
X * Redistribution and use in source and binary forms, with or without
X * modification, are permitted provided that the following conditions
X * are met:
X * 1. Redistributions of source code must retain the above copyright
X *    notice, this list of conditions and the following disclaimer.
X * 2. Redistributions in binary form must reproduce the above copyright
X *    notice, this list of conditions and the following disclaimer in the
X *    documentation and/or other materials provided with the distribution.
X * 3. The name of the author may not be used to endorse or promote products
X *    derived from this software without specific prior written permission.
X *
X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
X * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
X * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
X * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
X * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
X * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
X * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
X * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
X * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
X * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
X */
X
X#ifndef lint
Xstatic char rcsid[] = "$OpenBSD: dirname.c,v 1.4 1999/05/30 17:10:30 espie Exp $";
X#endif /* not lint */
X
X#include <errno.h>
X#include <libgen.h>
X#include <string.h>
X#include <sys/param.h>
X
Xchar *
Xdirname(path)
X	const char *path;
X{
X	static char bname[MAXPATHLEN];
X	register const char *endp;
X
X	/* Empty or NULL string gets treated as "." */
X	if (path == NULL || *path == '\0') {
X		(void)strcpy(bname, ".");
X		return(bname);
X	}
X
X	/* Strip trailing slashes */
X	endp = path + strlen(path) - 1;
X	while (endp > path && *endp == '/')
X		endp--;
X
X	/* Find the start of the dir */
X	while (endp > path && *endp != '/')
X		endp--;
X
X	/* Either the dir is "/" or there are no slashes */
X	if (endp == path) {
X		(void)strcpy(bname, *endp == '/' ? "/" : ".");
X		return(bname);
X	} else {
X		do {
X			endp--;
X		} while (endp > path && *endp == '/');
X	}
X
X	if (endp - path + 1 > sizeof(bname)) {
X		errno = ENAMETOOLONG;
X		return(NULL);
X	}
X	(void)strncpy(bname, path, endp - path + 1);
X	bname[endp - path + 1] = '\0';
X	return(bname);
X}
END-of-dirname.c
exit



>Release-Note:
>Audit-Trail:
>Unformatted:


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199908041303.JAA10126>