lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2002 Manuel Novoa III |
| 3 | * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org> |
| 4 | * |
| 5 | * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. |
| 6 | */ |
| 7 | |
| 8 | #include "_string.h" |
| 9 | #include <libgen.h> |
| 10 | |
| 11 | char *dirname(char *path) |
| 12 | { |
| 13 | static const char null_or_empty_or_noslash[] = "."; |
| 14 | register char *s; |
| 15 | register char *last; |
| 16 | char *first; |
| 17 | |
| 18 | last = s = path; |
| 19 | |
| 20 | if (s != NULL) { |
| 21 | |
| 22 | LOOP: |
| 23 | while (*s && (*s != '/')) ++s; |
| 24 | first = s; |
| 25 | while (*s == '/') ++s; |
| 26 | if (*s) { |
| 27 | last = first; |
| 28 | goto LOOP; |
| 29 | } |
| 30 | |
| 31 | if (last == path) { |
| 32 | if (*last != '/') { |
| 33 | goto DOT; |
| 34 | } |
| 35 | if ((*++last == '/') && (last[1] == 0)) { |
| 36 | ++last; |
| 37 | } |
| 38 | } |
| 39 | *last = 0; |
| 40 | return path; |
| 41 | } |
| 42 | DOT: |
| 43 | return (char *) null_or_empty_or_noslash; |
| 44 | } |