1
/* $Id: basename.c 2683 2007-08-08 23:33:01Z charles $ */
2
/* $OpenBSD: basename.c,v 1.14 2005/08/08 08:05:33 espie Exp $ */
5
* Copyright (c) 1997, 2004 Todd C. Miller <Todd.Miller@courtesan.com>
7
* Permission to use, copy, modify, and distribute this software for any
8
* purpose with or without fee is hereby granted, provided that the above
9
* copyright notice and this permission notice appear in all copies.
11
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24
#include <unistd.h> /* for MAXPATHLEN */
25
#include <sys/param.h>
28
basename(const char *path)
30
static char bname[MAXPATHLEN];
32
const char *endp, *startp;
34
/* Empty or NULL string gets treated as "." */
35
if (path == NULL || *path == '\0') {
41
/* Strip any trailing slashes */
42
endp = path + strlen(path) - 1;
43
while (endp > path && *endp == '/')
46
/* All slashes becomes "/" */
47
if (endp == path && *endp == '/') {
53
/* Find the start of the base */
55
while (startp > path && *(startp - 1) != '/')
58
len = endp - startp + 1;
59
if (len >= sizeof(bname)) {
63
memcpy(bname, startp, len);
68
#endif /* HAVE_BASENAME */