]>
Commit | Line | Data |
---|---|---|
d74671e4 | 1 | /* OPENBSD ORIGINAL: lib/libc/gen/dirname.c */ |
2 | ||
510a42ce | 3 | /* $OpenBSD: dirname.c,v 1.10 2003/06/17 21:56:23 millert Exp $ */ |
b4d02860 | 4 | |
5 | /* | |
6 | * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> | |
b4d02860 | 7 | * |
510a42ce | 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. | |
b4d02860 | 11 | * |
510a42ce | 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. | |
b4d02860 | 19 | */ |
20 | ||
21 | #include "includes.h" | |
22 | #ifndef HAVE_DIRNAME | |
23 | ||
510a42ce | 24 | #ifndef lint |
25 | static char rcsid[] = "$OpenBSD: dirname.c,v 1.10 2003/06/17 21:56:23 millert Exp $"; | |
26 | #endif /* not lint */ | |
b4d02860 | 27 | |
28 | #include <errno.h> | |
29 | #include <string.h> | |
30 | #include <sys/param.h> | |
31 | ||
32 | char * | |
510a42ce | 33 | dirname(const char *path) |
b4d02860 | 34 | { |
35 | static char bname[MAXPATHLEN]; | |
36 | register const char *endp; | |
37 | ||
38 | /* Empty or NULL string gets treated as "." */ | |
39 | if (path == NULL || *path == '\0') { | |
ac8802eb | 40 | (void)strlcpy(bname, ".", sizeof bname); |
b4d02860 | 41 | return(bname); |
42 | } | |
43 | ||
44 | /* Strip trailing slashes */ | |
45 | endp = path + strlen(path) - 1; | |
46 | while (endp > path && *endp == '/') | |
47 | endp--; | |
48 | ||
49 | /* Find the start of the dir */ | |
50 | while (endp > path && *endp != '/') | |
51 | endp--; | |
52 | ||
53 | /* Either the dir is "/" or there are no slashes */ | |
54 | if (endp == path) { | |
ac8802eb | 55 | (void)strlcpy(bname, *endp == '/' ? "/" : ".", sizeof bname); |
b4d02860 | 56 | return(bname); |
57 | } else { | |
58 | do { | |
59 | endp--; | |
60 | } while (endp > path && *endp == '/'); | |
61 | } | |
62 | ||
1656cbed | 63 | if (endp - path + 2 > sizeof(bname)) { |
b4d02860 | 64 | errno = ENAMETOOLONG; |
65 | return(NULL); | |
66 | } | |
f11065cb | 67 | strlcpy(bname, path, endp - path + 2); |
b4d02860 | 68 | return(bname); |
69 | } | |
70 | #endif |