diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 1997-08-17 21:31:24 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 1997-08-17 21:31:24 +0000 |
commit | 6dc0bfe0de2bfa6895acbae09d74eecb0175a1f9 (patch) | |
tree | 62e84f2c805392808e7086092a4193db1887f1ff /lib | |
parent | 10e5cadf726aa1983d5356a264d2c5724c1e517d (diff) |
basename(3) and dirname(3) as specified by XPG4 and XPG4.2
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/gen/Makefile.inc | 8 | ||||
-rw-r--r-- | lib/libc/gen/basename.3 | 125 | ||||
-rw-r--r-- | lib/libc/gen/basename.c | 78 | ||||
-rw-r--r-- | lib/libc/gen/dirname.c | 81 | ||||
-rw-r--r-- | lib/libc/shlib_version | 2 |
5 files changed, 289 insertions, 5 deletions
diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc index 7b6e4a5120c..1d7a9745697 100644 --- a/lib/libc/gen/Makefile.inc +++ b/lib/libc/gen/Makefile.inc @@ -1,10 +1,10 @@ -# $OpenBSD: Makefile.inc,v 1.11 1997/07/23 21:09:04 kstailey Exp $ +# $OpenBSD: Makefile.inc,v 1.12 1997/08/17 21:31:19 millert Exp $ # gen sources .PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/gen ${.CURDIR}/gen -SRCS+= alarm.c assert.c clock.c closedir.c confstr.c ctermid.c \ - ctype_.c daemon.c devname.c disklabel.c elf_hash.c err.c \ +SRCS+= alarm.c assert.c basename.c clock.c closedir.c confstr.c ctermid.c \ + ctype_.c daemon.c devname.c dirname.c disklabel.c elf_hash.c err.c \ errx.c errlist.c errno.c exec.c fnmatch.c fstab.c ftok.c fts.c \ getbsize.c getcap.c getcwd.c getdomainname.c getgrent.c \ getgrouplist.c gethostname.c getloadavg.c getlogin.c getmntinfo.c \ @@ -42,7 +42,7 @@ errlst.o errlst.po: .endif # _NOTDEF_XXX_ -MAN+= alarm.3 clock.3 confstr.3 ctermid.3 ctype.3 daemon.3 \ +MAN+= alarm.3 basename.3 clock.3 confstr.3 ctermid.3 ctype.3 daemon.3 \ devname.3 directory.3 err.3 exec.3 fnmatch.3 frexp.3 ftok.3 fts.3 \ getbsize.3 getcap.3 getcwd.3 getdomainname.3 getdiskbyname.3 \ getfsent.3 getgrent.3 getgrouplist.3 gethostname.3 getloadavg.3 \ diff --git a/lib/libc/gen/basename.3 b/lib/libc/gen/basename.3 new file mode 100644 index 00000000000..baaee23c42f --- /dev/null +++ b/lib/libc/gen/basename.3 @@ -0,0 +1,125 @@ +.\" +.\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> +.\" All rights reserved. +.\" +.\" 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 Todd C. Miller. +.\" 4. The name of the author may not be used to endorse or promote products +.\" derived from this software without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED ``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 AUTHOR 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. +.\" +.\" $OpenBSD: basename.3,v 1.1 1997/08/17 21:31:20 millert Exp $ +.\" +.Dd August 17, 1997 +.Dt BASENAME 3 +.Os +.Sh NAME +.Nm basename , +.Nm dirname +.Nd extract the base or directory portition of a pathname +.Sh SYNOPSIS +.Fd #include <libgen.h> +.Ft char * +.Fn basename "char *path" +.Ft char * +.Fn dirname "char *path" +.Sh DESCRIPTION +The +.Fn basename +function +returns the last componenent from the pathname pointed to by +.Ar path , +deleting any trailing '/' characters. If +.Ar path +consists entirely of '/' characters, a pointer to the string "/" +is returned. If +.Ar path +is a NULL pointer or the empty string, a pointer to the string "." +is returned. +.Pp +The +.Fn dirname +function +is the converse of +.Fn basename ; +it returns a pointer to the parent directory of the pathname pointed to by +.Ar path . +Any trailing '/' characters are not counted as part of the directory +name. If +.Ar path +is a NULL pointer, the empty string, or contains no '/' characters, +.Fn dirname +returns a pointer to the string ".", signifying the current directory. +.Sh RETURN VALUES +On successful completion, +.Fn basename +returns a pointer to the last component of +.Ar path. +.Pp +On successful completion, +.Fn dirname +returns a pointer to the parent directory of +.Ar path. +.Pp +If +.Fn basename +or +.Fn dirname +fail, a NULL pointer is returned and the global variable +.Va errno +is set to indicate the error. +.Sh ERRORS +The following error codes may be set in +.Va errno : +.Bl -tag -width Er +.It Bq Er ENAMETOOLONG +the path component to be returned was larger than +.Va MANPATHLEN . +.Sh WARNINGS +Both +.Fn basename +and +.Fn dirname +return pointers to internal static storage space that will be overwritten +by subsequent calls (each function has its own separate storage). +.Sh SEE ALSO +.Xr basename 1 , +.Xr dirname 1 +.Sh STANDARDS +The +.Fn basename +and +.Fn dirname +functions conform to +.St -xpg4.2 . +.Sh HISTORY +The +.Fn basename +and +.Fn dirname +functions first appeared in +.Bx Open +2.2. +.Sh AUTHOR +.nf +Todd C. Miller <Todd.Miller@courtesan.com> +.fi diff --git a/lib/libc/gen/basename.c b/lib/libc/gen/basename.c new file mode 100644 index 00000000000..2412e3db43d --- /dev/null +++ b/lib/libc/gen/basename.c @@ -0,0 +1,78 @@ +/* $OpenBSD: basename.c,v 1.1 1997/08/17 21:31:21 millert Exp $ */ + +/* + * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> + * All rights reserved. + * + * 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 Todd C. Miller. + * 4. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``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 AUTHOR 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. + */ + +#ifndef lint +static char rcsid[] = "$OpenBSD: basename.c,v 1.1 1997/08/17 21:31:21 millert Exp $"; +#endif /* not lint */ + +#include <errno.h> +#include <libgen.h> +#include <string.h> +#include <sys/param.h> + +char * +basename(path) + char *path; +{ + static char bname[MAXPATHLEN]; + register char *endp, *startp; + + /* Empty or NULL string gets treated as "." */ + if (path == NULL || *path == '\0') { + (void)strcpy(bname, "."); + return(bname); + } + + /* Strip trailing slashes */ + endp = path + strlen(path) - 1; + while (endp > path && *endp == '/') + endp--; + + /* All slashes becomes "/" */ + if (endp == path && *endp == '/') { + (void)strcpy(bname, "/"); + return(bname); + } + + /* Find the start of the base */ + startp = endp; + while (startp > path && *(startp - 1) != '/') + startp--; + + if (endp - startp + 1 > sizeof(bname)) { + errno = ENAMETOOLONG; + return(NULL); + } + (void)strncpy(bname, startp, endp - startp + 1); + bname[endp - startp + 1] = '\0'; + return(bname); +} diff --git a/lib/libc/gen/dirname.c b/lib/libc/gen/dirname.c new file mode 100644 index 00000000000..e8128c78bf2 --- /dev/null +++ b/lib/libc/gen/dirname.c @@ -0,0 +1,81 @@ +/* $OpenBSD: dirname.c,v 1.1 1997/08/17 21:31:21 millert Exp $ */ + +/* + * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> + * All rights reserved. + * + * 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 Todd C. Miller. + * 4. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``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 AUTHOR 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. + */ + +#ifndef lint +static char rcsid[] = "$OpenBSD: dirname.c,v 1.1 1997/08/17 21:31:21 millert Exp $"; +#endif /* not lint */ + +#include <errno.h> +#include <libgen.h> +#include <string.h> +#include <sys/param.h> + +char * +dirname(path) + char *path; +{ + static char bname[MAXPATHLEN]; + register char *endp; + + /* Empty or NULL string gets treated as "." */ + if (path == NULL || *path == '\0') { + (void)strcpy(bname, "."); + return(bname); + } + + /* Strip trailing slashes */ + endp = path + strlen(path) - 1; + while (endp > path && *endp == '/') + endp--; + + /* Find the start of the dir */ + while (endp > path && *endp != '/') + endp--; + + /* Either the dir is "/" or there are no slashes */ + if (endp == path) { + (void)strcpy(bname, *endp == '/' ? "/" : "."); + return(bname); + } else { + do { + endp--; + } while (endp > path && *endp == '/'); + } + + if (endp - path + 1 > sizeof(bname)) { + errno = ENAMETOOLONG; + return(NULL); + } + (void)strncpy(bname, path, endp - path + 1); + bname[endp - path + 1] = '\0'; + return(bname); +} diff --git a/lib/libc/shlib_version b/lib/libc/shlib_version index ffdd3d2d569..730231c38d0 100644 --- a/lib/libc/shlib_version +++ b/lib/libc/shlib_version @@ -1,2 +1,2 @@ major=17 -minor=0 +minor=1 |