xref: /freebsd/lib/libc/gen/basename.c (revision 34168b28e99b0bb328c7bb49cd91cb942181056f)
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 
2656bcbf00SBruce Evans #include <sys/cdefs.h>
27135b57f9SDavid E. O'Brien __FBSDID("$FreeBSD$");
281250db81SDag-Erling Smørgrav 
291250db81SDag-Erling Smørgrav #include <libgen.h>
301250db81SDag-Erling Smørgrav #include <string.h>
311250db81SDag-Erling Smørgrav 
321250db81SDag-Erling Smørgrav char *
33*34168b28SEd Schouten (basename)(char *path)
341250db81SDag-Erling Smørgrav {
35*34168b28SEd Schouten 	char *ptr;
361250db81SDag-Erling Smørgrav 
37*34168b28SEd Schouten 	/*
38*34168b28SEd Schouten 	 * If path is a null pointer or points to an empty string,
39*34168b28SEd Schouten 	 * basename() shall return a pointer to the string ".".
40*34168b28SEd Schouten 	 */
41*34168b28SEd Schouten 	if (path == NULL || *path == '\0')
42*34168b28SEd Schouten 		return (__DECONST(char *, "."));
431250db81SDag-Erling Smørgrav 
44*34168b28SEd Schouten 	/* Find end of last pathname component and null terminate it. */
45*34168b28SEd Schouten 	ptr = path + strlen(path);
46*34168b28SEd Schouten 	while (ptr > path + 1 && *(ptr - 1) == '/')
47*34168b28SEd Schouten 		--ptr;
48*34168b28SEd Schouten 	*ptr-- = '\0';
491250db81SDag-Erling Smørgrav 
50*34168b28SEd Schouten 	/* Find beginning of last pathname component. */
51*34168b28SEd Schouten 	while (ptr > path && *(ptr - 1) != '/')
52*34168b28SEd Schouten 		--ptr;
53*34168b28SEd Schouten 	return (ptr);
54a502a84dSRobert Watson }
55