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 3456bcbf00SBruce Evans #include <sys/cdefs.h> 35135b57f9SDavid E. O'Brien __FBSDID("$FreeBSD$"); 361250db81SDag-Erling Smørgrav 371250db81SDag-Erling Smørgrav #include <errno.h> 381250db81SDag-Erling Smørgrav #include <libgen.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 { 461250db81SDag-Erling Smørgrav static char bname[MAXPATHLEN]; 471250db81SDag-Erling Smørgrav register const char *endp, *startp; 481250db81SDag-Erling Smørgrav 491250db81SDag-Erling Smørgrav /* Empty or NULL string gets treated as "." */ 501250db81SDag-Erling Smørgrav if (path == NULL || *path == '\0') { 511250db81SDag-Erling Smørgrav (void)strcpy(bname, "."); 521250db81SDag-Erling Smørgrav return(bname); 531250db81SDag-Erling Smørgrav } 541250db81SDag-Erling Smørgrav 551250db81SDag-Erling Smørgrav /* Strip trailing slashes */ 561250db81SDag-Erling Smørgrav endp = path + strlen(path) - 1; 571250db81SDag-Erling Smørgrav while (endp > path && *endp == '/') 581250db81SDag-Erling Smørgrav endp--; 591250db81SDag-Erling Smørgrav 601250db81SDag-Erling Smørgrav /* All slashes becomes "/" */ 611250db81SDag-Erling Smørgrav if (endp == path && *endp == '/') { 621250db81SDag-Erling Smørgrav (void)strcpy(bname, "/"); 631250db81SDag-Erling Smørgrav return(bname); 641250db81SDag-Erling Smørgrav } 651250db81SDag-Erling Smørgrav 661250db81SDag-Erling Smørgrav /* Find the start of the base */ 671250db81SDag-Erling Smørgrav startp = endp; 681250db81SDag-Erling Smørgrav while (startp > path && *(startp - 1) != '/') 691250db81SDag-Erling Smørgrav startp--; 701250db81SDag-Erling Smørgrav 71ff5f0dbcSDima Dorfman if (endp - startp + 2 > sizeof(bname)) { 721250db81SDag-Erling Smørgrav errno = ENAMETOOLONG; 731250db81SDag-Erling Smørgrav return(NULL); 741250db81SDag-Erling Smørgrav } 751250db81SDag-Erling Smørgrav (void)strncpy(bname, startp, endp - startp + 1); 761250db81SDag-Erling Smørgrav bname[endp - startp + 1] = '\0'; 771250db81SDag-Erling Smørgrav return(bname); 781250db81SDag-Erling Smørgrav } 79