/* $NetBSD: dirname.c,v 1.14 2018/09/27 00:45:34 kre Exp $ */12/*-3* Copyright (c) 1997, 2002 The NetBSD Foundation, Inc.4* All rights reserved.5*6* This code is derived from software contributed to The NetBSD Foundation7* by Klaus Klein and Jason R. Thorpe.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS19* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED20* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS22* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR23* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF24* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS25* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN26* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE28* POSSIBILITY OF SUCH DAMAGE.29*/3031#ifdef HAVE_CONFIG_H32# include <config.h>33#endif34#ifndef HAVE_DIRNAME3536#include <sys/cdefs.h>37#if defined(LIBC_SCCS) && !defined(lint)38__RCSID("$NetBSD: dirname.c,v 1.14 2018/09/27 00:45:34 kre Exp $");39#endif /* !LIBC_SCCS && !lint */4041#include <sys/param.h>42#ifdef HAVE_LIBGEN_H43#include <libgen.h>44#endif45#ifdef HAVE_LIMITS_H46#include <limits.h>47#endif48#ifdef HAVE_STRING_H49#include <string.h>50#endif51#ifndef PATH_MAX52# define PATH_MAX 102453#endif54#ifndef MIN55# define MIN(a, b) ((a < b) ? a : b)56#endif575859static size_t60xdirname_r(const char *path, char *buf, size_t buflen)61{62const char *endp;63size_t len;6465/*66* If `path' is a null pointer or points to an empty string,67* return a pointer to the string ".".68*/69if (path == NULL || *path == '\0') {70path = ".";71len = 1;72goto out;73}7475/* Strip trailing slashes, if any. */76endp = path + strlen(path) - 1;77while (endp != path && *endp == '/')78endp--;7980/* Find the start of the dir */81while (endp > path && *endp != '/')82endp--;8384if (endp == path) {85path = *endp == '/' ? "/" : ".";86len = 1;87goto out;88}8990do91endp--;92while (endp > path && *endp == '/');9394len = endp - path + 1;95out:96if (buf != NULL && buflen != 0) {97buflen = MIN(len, buflen - 1);98if (buf != path)99memcpy(buf, path, buflen);100buf[buflen] = '\0';101}102return len;103}104105char *106dirname(char *path)107{108static char result[PATH_MAX];109(void)xdirname_r(path, result, sizeof(result));110return result;111}112113#ifdef MAIN114#include <stdlib.h>115#include <stdio.h>116117int118main(int argc, char *argv[])119{120printf("%s\n", dirname(argv[1]));121exit(0);122}123#endif124#endif125126127