1e2f68161SEd Schouten /*- 2e2f68161SEd Schouten * Copyright (c) 2015-2016 Nuxi, https://nuxi.nl/ 31250db81SDag-Erling Smørgrav * 4e2f68161SEd Schouten * Redistribution and use in source and binary forms, with or without 5e2f68161SEd Schouten * modification, are permitted provided that the following conditions 6e2f68161SEd Schouten * are met: 7e2f68161SEd Schouten * 1. Redistributions of source code must retain the above copyright 8e2f68161SEd Schouten * notice, this list of conditions and the following disclaimer. 9e2f68161SEd Schouten * 2. Redistributions in binary form must reproduce the above copyright 10e2f68161SEd Schouten * notice, this list of conditions and the following disclaimer in the 11e2f68161SEd Schouten * documentation and/or other materials provided with the distribution. 121250db81SDag-Erling Smørgrav * 13e2f68161SEd Schouten * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14e2f68161SEd Schouten * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15e2f68161SEd Schouten * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16e2f68161SEd Schouten * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17e2f68161SEd Schouten * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18e2f68161SEd Schouten * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19e2f68161SEd Schouten * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20e2f68161SEd Schouten * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21e2f68161SEd Schouten * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22e2f68161SEd Schouten * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23e2f68161SEd Schouten * SUCH DAMAGE. 241250db81SDag-Erling Smørgrav */ 251250db81SDag-Erling Smørgrav 2656bcbf00SBruce Evans #include <sys/cdefs.h> 27135b57f9SDavid E. O'Brien __FBSDID("$FreeBSD$"); 281250db81SDag-Erling Smørgrav 291250db81SDag-Erling Smørgrav #include <libgen.h> 30e2f68161SEd Schouten #include <stdbool.h> 311250db81SDag-Erling Smørgrav #include <string.h> 321250db81SDag-Erling Smørgrav 331250db81SDag-Erling Smørgrav char * 34*cd4dcac8SEd Schouten (dirname)(char *path) 351250db81SDag-Erling Smørgrav { 36e2f68161SEd Schouten const char *in, *prev, *begin, *end; 37e2f68161SEd Schouten char *out; 38e2f68161SEd Schouten size_t prevlen; 39e2f68161SEd Schouten bool skipslash; 401250db81SDag-Erling Smørgrav 41e2f68161SEd Schouten /* 42e2f68161SEd Schouten * If path is a null pointer or points to an empty string, 43e2f68161SEd Schouten * dirname() shall return a pointer to the string ".". 44e2f68161SEd Schouten */ 45e2f68161SEd Schouten if (path == NULL || *path == '\0') 46e2f68161SEd Schouten return ((char *)"."); 47e2f68161SEd Schouten 48e2f68161SEd Schouten /* Retain at least one leading slash character. */ 49e2f68161SEd Schouten in = out = *path == '/' ? path + 1 : path; 50e2f68161SEd Schouten 51e2f68161SEd Schouten skipslash = true; 52e2f68161SEd Schouten prev = "."; 53e2f68161SEd Schouten prevlen = 1; 54e2f68161SEd Schouten for (;;) { 55e2f68161SEd Schouten /* Extract the next pathname component. */ 56e2f68161SEd Schouten while (*in == '/') 57e2f68161SEd Schouten ++in; 58e2f68161SEd Schouten begin = in; 59e2f68161SEd Schouten while (*in != '/' && *in != '\0') 60e2f68161SEd Schouten ++in; 61e2f68161SEd Schouten end = in; 62e2f68161SEd Schouten if (begin == end) 63e2f68161SEd Schouten break; 64e2f68161SEd Schouten 65e2f68161SEd Schouten /* 66e2f68161SEd Schouten * Copy over the previous pathname component, except if 67e2f68161SEd Schouten * it's dot. There is no point in retaining those. 68e2f68161SEd Schouten */ 69e2f68161SEd Schouten if (prevlen != 1 || *prev != '.') { 70e2f68161SEd Schouten if (!skipslash) 71e2f68161SEd Schouten *out++ = '/'; 72e2f68161SEd Schouten skipslash = false; 73e2f68161SEd Schouten memmove(out, prev, prevlen); 74e2f68161SEd Schouten out += prevlen; 755fb691beSRob Braun } 765fb691beSRob Braun 77e2f68161SEd Schouten /* Preserve the pathname component for the next iteration. */ 78e2f68161SEd Schouten prev = begin; 79e2f68161SEd Schouten prevlen = end - begin; 801250db81SDag-Erling Smørgrav } 811250db81SDag-Erling Smørgrav 82e2f68161SEd Schouten /* 83e2f68161SEd Schouten * If path does not contain a '/', then dirname() shall return a 84e2f68161SEd Schouten * pointer to the string ".". 85e2f68161SEd Schouten */ 86e2f68161SEd Schouten if (out == path) 87e2f68161SEd Schouten *out++ = '.'; 88e2f68161SEd Schouten *out = '\0'; 89e2f68161SEd Schouten return (path); 901250db81SDag-Erling Smørgrav } 91