Index: head/lib/libc/gen/dirname.3 =================================================================== --- head/lib/libc/gen/dirname.3 (revision 305951) +++ head/lib/libc/gen/dirname.3 (revision 305952) @@ -1,90 +1,85 @@ .\" $OpenBSD: dirname.3,v 1.17 2007/05/31 19:19:28 jmc Exp $ .\" .\" Copyright (c) 1997 Todd C. Miller .\" .\" Permission to use, copy, modify, and distribute this software for any .\" purpose with or without fee is hereby granted, provided that the above .\" copyright notice and this permission notice appear in all copies. .\" .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" .\" $FreeBSD$ .\" -.Dd August 12, 2016 +.Dd September 5, 2016 .Dt DIRNAME 3 .Os .Sh NAME .Nm dirname .Nd extract the directory part of a pathname .Sh SYNOPSIS .In libgen.h .Ft char * .Fn dirname "char *path" .Sh DESCRIPTION The .Fn dirname function is the converse of .Xr basename 3 ; it returns a pointer to the parent directory of the pathname pointed to by .Fa path . Any trailing .Sq \&/ characters are not counted as part of the directory name. .Sh RETURN VALUES If .Fa path is a null pointer, the empty string, or contains no .Sq \&/ characters, .Fn dirname returns a pointer to the string .Qq \&. , signifying the current directory. Otherwise, it returns a pointer to the parent directory of .Fa path . .Sh IMPLEMENTATION NOTES This implementation of .Fn dirname uses the buffer provided by the caller to store the resulting parent directory. Other vendor implementations may return a pointer to internal storage space instead. The advantage of the former approach is that it ensures thread-safety, while also placing no upper limit on the supported length of the pathname. -.Pp -The algorithm used by this implementation also discards redundant -slashes and -.Qq \&. -pathname components from the pathname string. .Sh SEE ALSO .Xr basename 1 , .Xr dirname 1 , .Xr basename 3 .Sh STANDARDS The .Fn dirname function conforms to .St -xpg4.2 . .Sh HISTORY The .Fn dirname function first appeared in .Ox 2.2 and .Fx 4.2 . .Pp In .Fx 12.0 , this function was reimplemented to store its result in the provided input buffer. .Sh AUTHORS .An Nuxi, the Netherlands Index: head/lib/libc/gen/dirname.c =================================================================== --- head/lib/libc/gen/dirname.c (revision 305951) +++ head/lib/libc/gen/dirname.c (revision 305952) @@ -1,90 +1,73 @@ /*- * Copyright (c) 2015-2016 Nuxi, https://nuxi.nl/ * * 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 __FBSDID("$FreeBSD$"); #include -#include #include char * (dirname)(char *path) { - const char *in, *prev, *begin, *end; - char *out; - size_t prevlen; - bool skipslash; + char *end; /* * If path is a null pointer or points to an empty string, * dirname() shall return a pointer to the string ".". */ if (path == NULL || *path == '\0') - return ((char *)"."); + return (__DECONST(char *, ".")); - /* Retain at least one leading slash character. */ - in = out = *path == '/' ? path + 1 : path; + /* Find end of last pathname component. */ + end = path + strlen(path); + while (end > path + 1 && *(end - 1) == '/') + --end; - skipslash = true; - prev = "."; - prevlen = 1; - for (;;) { - /* Extract the next pathname component. */ - while (*in == '/') - ++in; - begin = in; - while (*in != '/' && *in != '\0') - ++in; - end = in; - if (begin == end) - break; + /* Strip off the last pathname component. */ + while (end > path && *(end - 1) != '/') + --end; - /* - * Copy over the previous pathname component, except if - * it's dot. There is no point in retaining those. - */ - if (prevlen != 1 || *prev != '.') { - if (!skipslash) - *out++ = '/'; - skipslash = false; - memmove(out, prev, prevlen); - out += prevlen; - } - - /* Preserve the pathname component for the next iteration. */ - prev = begin; - prevlen = end - begin; - } - /* * If path does not contain a '/', then dirname() shall return a * pointer to the string ".". */ - if (out == path) - *out++ = '.'; - *out = '\0'; + if (end == path) { + path[0] = '.'; + path[1] = '\0'; + return (path); + } + + /* + * Remove trailing slashes from the resulting directory name. Ensure + * that at least one character remains. + */ + while (end > path + 1 && *(end - 1) == '/') + --end; + + /* Null terminate directory name and return it. */ + *end = '\0'; return (path); }