Damien Miller | 3db2e4d | 2003-11-24 13:33:34 +1100 | [diff] [blame] | 1 | /* OPENBSD ORIGINAL: lib/libc/gen/basename.c */ |
| 2 | |
Ben Lindstrom | af4a6c3 | 2003-08-25 01:10:51 +0000 | [diff] [blame] | 3 | /* $OpenBSD: basename.c,v 1.11 2003/06/17 21:56:23 millert Exp $ */ |
Damien Miller | 8d8168a | 2003-02-24 12:55:55 +1100 | [diff] [blame] | 4 | |
| 5 | /* |
| 6 | * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> |
Damien Miller | 8d8168a | 2003-02-24 12:55:55 +1100 | [diff] [blame] | 7 | * |
Ben Lindstrom | af4a6c3 | 2003-08-25 01:10:51 +0000 | [diff] [blame] | 8 | * Permission to use, copy, modify, and distribute this software for any |
| 9 | * purpose with or without fee is hereby granted, provided that the above |
| 10 | * copyright notice and this permission notice appear in all copies. |
Damien Miller | 8d8168a | 2003-02-24 12:55:55 +1100 | [diff] [blame] | 11 | * |
Ben Lindstrom | af4a6c3 | 2003-08-25 01:10:51 +0000 | [diff] [blame] | 12 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 13 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 14 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 15 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 16 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Damien Miller | 8d8168a | 2003-02-24 12:55:55 +1100 | [diff] [blame] | 19 | */ |
Damien Miller | 8d8168a | 2003-02-24 12:55:55 +1100 | [diff] [blame] | 20 | |
Ben Lindstrom | af4a6c3 | 2003-08-25 01:10:51 +0000 | [diff] [blame] | 21 | #include "includes.h" |
| 22 | #ifndef HAVE_BASENAME |
Damien Miller | 8d8168a | 2003-02-24 12:55:55 +1100 | [diff] [blame] | 23 | |
| 24 | #ifndef lint |
Ben Lindstrom | af4a6c3 | 2003-08-25 01:10:51 +0000 | [diff] [blame] | 25 | static char rcsid[] = "$OpenBSD: basename.c,v 1.11 2003/06/17 21:56:23 millert Exp $"; |
Damien Miller | 8d8168a | 2003-02-24 12:55:55 +1100 | [diff] [blame] | 26 | #endif /* not lint */ |
| 27 | |
| 28 | char * |
| 29 | basename(const char *path) |
| 30 | { |
| 31 | static char bname[MAXPATHLEN]; |
| 32 | register const char *endp, *startp; |
| 33 | |
| 34 | /* Empty or NULL string gets treated as "." */ |
| 35 | if (path == NULL || *path == '\0') { |
| 36 | (void)strlcpy(bname, ".", sizeof bname); |
| 37 | return(bname); |
| 38 | } |
| 39 | |
| 40 | /* Strip trailing slashes */ |
| 41 | endp = path + strlen(path) - 1; |
| 42 | while (endp > path && *endp == '/') |
| 43 | endp--; |
| 44 | |
| 45 | /* All slashes become "/" */ |
| 46 | if (endp == path && *endp == '/') { |
| 47 | (void)strlcpy(bname, "/", sizeof bname); |
| 48 | return(bname); |
| 49 | } |
| 50 | |
| 51 | /* Find the start of the base */ |
| 52 | startp = endp; |
| 53 | while (startp > path && *(startp - 1) != '/') |
| 54 | startp--; |
| 55 | |
| 56 | if (endp - startp + 2 > sizeof(bname)) { |
| 57 | errno = ENAMETOOLONG; |
| 58 | return(NULL); |
| 59 | } |
| 60 | strlcpy(bname, startp, endp - startp + 2); |
| 61 | return(bname); |
| 62 | } |
| 63 | |
| 64 | #endif /* !defined(HAVE_BASENAME) */ |