1223216Sdelphij/* $OpenBSD: basename.c,v 1.14 2005/08/08 08:05:33 espie Exp $ */ 2223216Sdelphij 365294Sdes/* 4223216Sdelphij * Copyright (c) 1997, 2004 Todd C. Miller <Todd.Miller@courtesan.com> 565294Sdes * 6223216Sdelphij * Permission to use, copy, modify, and distribute this software for any 7223216Sdelphij * purpose with or without fee is hereby granted, provided that the above 8223216Sdelphij * copyright notice and this permission notice appear in all copies. 965294Sdes * 10223216Sdelphij * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11223216Sdelphij * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12223216Sdelphij * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13223216Sdelphij * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14223216Sdelphij * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15223216Sdelphij * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16223216Sdelphij * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1765294Sdes */ 1865294Sdes 1990016Sbde#include <sys/cdefs.h> 2089999Sobrien__FBSDID("$FreeBSD$"); 2165294Sdes 2265294Sdes#include <errno.h> 2365294Sdes#include <libgen.h> 24108419Smarcel#include <stdlib.h> 2565294Sdes#include <string.h> 2665294Sdes#include <sys/param.h> 2765294Sdes 2865294Sdeschar * 29223216Sdelphijbasename_r(const char *path, char *bname) 3065294Sdes{ 3190041Sobrien const char *endp, *startp; 32223216Sdelphij size_t len; 3365294Sdes 3465294Sdes /* Empty or NULL string gets treated as "." */ 3565294Sdes if (path == NULL || *path == '\0') { 36223216Sdelphij bname[0] = '.'; 37223216Sdelphij bname[1] = '\0'; 38223216Sdelphij return (bname); 3965294Sdes } 4065294Sdes 41223216Sdelphij /* Strip any trailing slashes */ 4265294Sdes endp = path + strlen(path) - 1; 4365294Sdes while (endp > path && *endp == '/') 4465294Sdes endp--; 4565294Sdes 4665294Sdes /* All slashes becomes "/" */ 4765294Sdes if (endp == path && *endp == '/') { 48223216Sdelphij bname[0] = '/'; 49223216Sdelphij bname[1] = '\0'; 50223216Sdelphij return (bname); 5165294Sdes } 5265294Sdes 5365294Sdes /* Find the start of the base */ 5465294Sdes startp = endp; 5565294Sdes while (startp > path && *(startp - 1) != '/') 5665294Sdes startp--; 5765294Sdes 58223216Sdelphij len = endp - startp + 1; 59223216Sdelphij if (len >= MAXPATHLEN) { 6065294Sdes errno = ENAMETOOLONG; 61223216Sdelphij return (NULL); 6265294Sdes } 63223216Sdelphij memcpy(bname, startp, len); 64223216Sdelphij bname[len] = '\0'; 65223216Sdelphij return (bname); 6665294Sdes} 67197804Srwatson 68197804Srwatsonchar * 69223216Sdelphijbasename(const char *path) 70197804Srwatson{ 71197804Srwatson static char *bname = NULL; 72197804Srwatson 73197804Srwatson if (bname == NULL) { 74197804Srwatson bname = (char *)malloc(MAXPATHLEN); 75197804Srwatson if (bname == NULL) 76197804Srwatson return (NULL); 77197804Srwatson } 78197804Srwatson return (basename_r(path, bname)); 79197804Srwatson} 80