xref: /freebsd/lib/libc/gen/basename.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1*34168b28SEd Schouten /*-
2*34168b28SEd Schouten  * Copyright (c) 2015-2016 Nuxi, https://nuxi.nl/
31250db81SDag-Erling Smørgrav  *
4*34168b28SEd Schouten  * Redistribution and use in source and binary forms, with or without
5*34168b28SEd Schouten  * modification, are permitted provided that the following conditions
6*34168b28SEd Schouten  * are met:
7*34168b28SEd Schouten  * 1. Redistributions of source code must retain the above copyright
8*34168b28SEd Schouten  *    notice, this list of conditions and the following disclaimer.
9*34168b28SEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
10*34168b28SEd Schouten  *    notice, this list of conditions and the following disclaimer in the
11*34168b28SEd Schouten  *    documentation and/or other materials provided with the distribution.
121250db81SDag-Erling Smørgrav  *
13*34168b28SEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14*34168b28SEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*34168b28SEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*34168b28SEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17*34168b28SEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*34168b28SEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*34168b28SEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20*34168b28SEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21*34168b28SEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22*34168b28SEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23*34168b28SEd Schouten  * SUCH DAMAGE.
241250db81SDag-Erling Smørgrav  */
251250db81SDag-Erling Smørgrav 
261250db81SDag-Erling Smørgrav #include <libgen.h>
271250db81SDag-Erling Smørgrav #include <string.h>
281250db81SDag-Erling Smørgrav 
291250db81SDag-Erling Smørgrav char *
30*34168b28SEd Schouten (basename)(char *path)
311250db81SDag-Erling Smørgrav {
32*34168b28SEd Schouten 	char *ptr;
331250db81SDag-Erling Smørgrav 
34*34168b28SEd Schouten 	/*
35*34168b28SEd Schouten 	 * If path is a null pointer or points to an empty string,
36*34168b28SEd Schouten 	 * basename() shall return a pointer to the string ".".
37*34168b28SEd Schouten 	 */
38*34168b28SEd Schouten 	if (path == NULL || *path == '\0')
39*34168b28SEd Schouten 		return (__DECONST(char *, "."));
401250db81SDag-Erling Smørgrav 
41*34168b28SEd Schouten 	/* Find end of last pathname component and null terminate it. */
42*34168b28SEd Schouten 	ptr = path + strlen(path);
43*34168b28SEd Schouten 	while (ptr > path + 1 && *(ptr - 1) == '/')
44*34168b28SEd Schouten 		--ptr;
45*34168b28SEd Schouten 	*ptr-- = '\0';
461250db81SDag-Erling Smørgrav 
47*34168b28SEd Schouten 	/* Find beginning of last pathname component. */
48*34168b28SEd Schouten 	while (ptr > path && *(ptr - 1) != '/')
49*34168b28SEd Schouten 		--ptr;
50*34168b28SEd Schouten 	return (ptr);
51a502a84dSRobert Watson }
52