xref: /freebsd/lib/libc/gen/basename.c (revision 2bc46a64529175d805900dab5db72ab615e1eab2)
11250db81SDag-Erling Smørgrav /*
21250db81SDag-Erling Smørgrav  * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
31250db81SDag-Erling Smørgrav  * All rights reserved.
41250db81SDag-Erling Smørgrav  *
51250db81SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
61250db81SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
71250db81SDag-Erling Smørgrav  * are met:
81250db81SDag-Erling Smørgrav  * 1. Redistributions of source code must retain the above copyright
91250db81SDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer.
101250db81SDag-Erling Smørgrav  * 2. Redistributions in binary form must reproduce the above copyright
111250db81SDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer in the
121250db81SDag-Erling Smørgrav  *    documentation and/or other materials provided with the distribution.
131250db81SDag-Erling Smørgrav  * 3. The name of the author may not be used to endorse or promote products
141250db81SDag-Erling Smørgrav  *    derived from this software without specific prior written permission.
151250db81SDag-Erling Smørgrav  *
161250db81SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
171250db81SDag-Erling Smørgrav  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
181250db81SDag-Erling Smørgrav  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
191250db81SDag-Erling Smørgrav  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
201250db81SDag-Erling Smørgrav  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
211250db81SDag-Erling Smørgrav  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
221250db81SDag-Erling Smørgrav  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
231250db81SDag-Erling Smørgrav  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
241250db81SDag-Erling Smørgrav  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
251250db81SDag-Erling Smørgrav  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
261250db81SDag-Erling Smørgrav  */
271250db81SDag-Erling Smørgrav 
28135b57f9SDavid E. O'Brien #if 0
2956bcbf00SBruce Evans #ifndef lint
301250db81SDag-Erling Smørgrav static char rcsid[] = "$OpenBSD: basename.c,v 1.4 1999/05/30 17:10:30 espie Exp $";
311250db81SDag-Erling Smørgrav #endif /* not lint */
3256bcbf00SBruce Evans #endif
3356bcbf00SBruce Evans #include <sys/cdefs.h>
34135b57f9SDavid E. O'Brien __FBSDID("$FreeBSD$");
351250db81SDag-Erling Smørgrav 
361250db81SDag-Erling Smørgrav #include <errno.h>
371250db81SDag-Erling Smørgrav #include <libgen.h>
382bc46a64SMarcel Moolenaar #include <stdlib.h>
391250db81SDag-Erling Smørgrav #include <string.h>
401250db81SDag-Erling Smørgrav #include <sys/param.h>
411250db81SDag-Erling Smørgrav 
421250db81SDag-Erling Smørgrav char *
431250db81SDag-Erling Smørgrav basename(path)
441250db81SDag-Erling Smørgrav 	const char *path;
451250db81SDag-Erling Smørgrav {
465fb691beSRob Braun 	static char *bname = NULL;
4722626efaSDavid E. O'Brien 	const char *endp, *startp;
481250db81SDag-Erling Smørgrav 
495fb691beSRob Braun  	if (bname == NULL) {
505fb691beSRob Braun 		bname = (char *)malloc(MAXPATHLEN);
515fb691beSRob Braun 		if (bname == NULL)
525fb691beSRob Braun 			return(NULL);
535fb691beSRob Braun 	}
545fb691beSRob Braun 
551250db81SDag-Erling Smørgrav 	/* Empty or NULL string gets treated as "." */
561250db81SDag-Erling Smørgrav 	if (path == NULL || *path == '\0') {
571250db81SDag-Erling Smørgrav 		(void)strcpy(bname, ".");
581250db81SDag-Erling Smørgrav 		return(bname);
591250db81SDag-Erling Smørgrav 	}
601250db81SDag-Erling Smørgrav 
611250db81SDag-Erling Smørgrav 	/* Strip trailing slashes */
621250db81SDag-Erling Smørgrav 	endp = path + strlen(path) - 1;
631250db81SDag-Erling Smørgrav 	while (endp > path && *endp == '/')
641250db81SDag-Erling Smørgrav 		endp--;
651250db81SDag-Erling Smørgrav 
661250db81SDag-Erling Smørgrav 	/* All slashes becomes "/" */
671250db81SDag-Erling Smørgrav 	if (endp == path && *endp == '/') {
681250db81SDag-Erling Smørgrav 		(void)strcpy(bname, "/");
691250db81SDag-Erling Smørgrav 		return(bname);
701250db81SDag-Erling Smørgrav 	}
711250db81SDag-Erling Smørgrav 
721250db81SDag-Erling Smørgrav 	/* Find the start of the base */
731250db81SDag-Erling Smørgrav 	startp = endp;
741250db81SDag-Erling Smørgrav 	while (startp > path && *(startp - 1) != '/')
751250db81SDag-Erling Smørgrav 		startp--;
761250db81SDag-Erling Smørgrav 
775fb691beSRob Braun 	if (endp - startp + 2 > MAXPATHLEN) {
781250db81SDag-Erling Smørgrav 		errno = ENAMETOOLONG;
791250db81SDag-Erling Smørgrav 		return(NULL);
801250db81SDag-Erling Smørgrav 	}
811250db81SDag-Erling Smørgrav 	(void)strncpy(bname, startp, endp - startp + 1);
821250db81SDag-Erling Smørgrav 	bname[endp - startp + 1] = '\0';
831250db81SDag-Erling Smørgrav 	return(bname);
841250db81SDag-Erling Smørgrav }
85