1*0a6a1f1dSLionel Sambuc /* $NetBSD: basename.c,v 1.11 2014/07/16 10:52:26 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras * Copyright (c) 1997, 2002 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * This code is derived from software contributed to The NetBSD Foundation
82fe8fb19SBen Gras * by Klaus Klein and Jason R. Thorpe.
92fe8fb19SBen Gras *
102fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras * are met:
132fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
202fe8fb19SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
212fe8fb19SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
222fe8fb19SBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
232fe8fb19SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
242fe8fb19SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
252fe8fb19SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
262fe8fb19SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
272fe8fb19SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
282fe8fb19SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
292fe8fb19SBen Gras * POSSIBILITY OF SUCH DAMAGE.
302fe8fb19SBen Gras */
312fe8fb19SBen Gras
322fe8fb19SBen Gras #include <sys/cdefs.h>
332fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
34*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: basename.c,v 1.11 2014/07/16 10:52:26 christos Exp $");
352fe8fb19SBen Gras #endif /* !LIBC_SCCS && !lint */
362fe8fb19SBen Gras
372fe8fb19SBen Gras #include "namespace.h"
38*0a6a1f1dSLionel Sambuc #include <sys/param.h>
392fe8fb19SBen Gras #include <libgen.h>
40*0a6a1f1dSLionel Sambuc #include <string.h>
412fe8fb19SBen Gras #include <limits.h>
422fe8fb19SBen Gras #include <string.h>
432fe8fb19SBen Gras
442fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(basename,_basename)452fe8fb19SBen Gras __weak_alias(basename,_basename)
462fe8fb19SBen Gras #endif
472fe8fb19SBen Gras
48*0a6a1f1dSLionel Sambuc static size_t
49*0a6a1f1dSLionel Sambuc xbasename_r(const char *path, char *buf, size_t buflen)
502fe8fb19SBen Gras {
51*0a6a1f1dSLionel Sambuc const char *startp, *endp;
522fe8fb19SBen Gras size_t len;
532fe8fb19SBen Gras
542fe8fb19SBen Gras /*
552fe8fb19SBen Gras * If `path' is a null pointer or points to an empty string,
562fe8fb19SBen Gras * return a pointer to the string ".".
572fe8fb19SBen Gras */
58*0a6a1f1dSLionel Sambuc if (path == NULL || *path == '\0') {
59*0a6a1f1dSLionel Sambuc startp = ".";
60*0a6a1f1dSLionel Sambuc len = 1;
61*0a6a1f1dSLionel Sambuc goto out;
622fe8fb19SBen Gras }
632fe8fb19SBen Gras
642fe8fb19SBen Gras /* Strip trailing slashes, if any. */
65*0a6a1f1dSLionel Sambuc endp = path + strlen(path) - 1;
66*0a6a1f1dSLionel Sambuc while (endp != path && *endp == '/')
67*0a6a1f1dSLionel Sambuc endp--;
68*0a6a1f1dSLionel Sambuc
69*0a6a1f1dSLionel Sambuc /* Only slashes -> "/" */
70*0a6a1f1dSLionel Sambuc if (endp == path && *endp == '/') {
71*0a6a1f1dSLionel Sambuc startp = "/";
72*0a6a1f1dSLionel Sambuc len = 1;
73*0a6a1f1dSLionel Sambuc goto out;
74*0a6a1f1dSLionel Sambuc }
752fe8fb19SBen Gras
762fe8fb19SBen Gras /* Now find the beginning of this (final) component. */
77*0a6a1f1dSLionel Sambuc for (startp = endp; startp > path && *(startp - 1) != '/'; startp--)
78*0a6a1f1dSLionel Sambuc continue;
792fe8fb19SBen Gras
802fe8fb19SBen Gras /* ...and copy the result into the result buffer. */
81*0a6a1f1dSLionel Sambuc len = (endp - startp) + 1 /* last char */;
82*0a6a1f1dSLionel Sambuc out:
83*0a6a1f1dSLionel Sambuc if (buf != NULL && buflen != 0) {
84*0a6a1f1dSLionel Sambuc buflen = MIN(len, buflen - 1);
85*0a6a1f1dSLionel Sambuc memcpy(buf, startp, buflen);
86*0a6a1f1dSLionel Sambuc buf[buflen] = '\0';
872fe8fb19SBen Gras }
88*0a6a1f1dSLionel Sambuc return len;
89*0a6a1f1dSLionel Sambuc }
90*0a6a1f1dSLionel Sambuc
91*0a6a1f1dSLionel Sambuc #if !HAVE_BASENAME
92*0a6a1f1dSLionel Sambuc
93*0a6a1f1dSLionel Sambuc char *
basename(char * path)94*0a6a1f1dSLionel Sambuc basename(char *path) {
95*0a6a1f1dSLionel Sambuc static char result[PATH_MAX];
96*0a6a1f1dSLionel Sambuc
97*0a6a1f1dSLionel Sambuc (void)xbasename_r(path, result, sizeof(result));
98*0a6a1f1dSLionel Sambuc return result;
99*0a6a1f1dSLionel Sambuc }
100*0a6a1f1dSLionel Sambuc
1012fe8fb19SBen Gras #endif
102