Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3 | * dirname implementation for busybox (for libc's missing one) |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 4 | * |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 5 | * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org> |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
| 23 | /* Note: The previous busybox implementation did not handle NULL path |
| 24 | * and also moved a pointer before path, which is not portable in C. |
| 25 | * So I replaced it with my uClibc version. |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 26 | */ |
| 27 | |
Manuel Novoa III | a2949aa | 2001-06-29 18:59:32 +0000 | [diff] [blame] | 28 | #include <string.h> |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 29 | #include "libbb.h" |
| 30 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 31 | #if __GNU_LIBRARY__ < 5 |
Matt Kraai | 80f6d55 | 2001-08-24 20:35:45 +0000 | [diff] [blame] | 32 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 33 | extern |
Matt Kraai | 80f6d55 | 2001-08-24 20:35:45 +0000 | [diff] [blame] | 34 | char *dirname(char *path) |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 35 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 36 | static const char null_or_empty_or_noslash[] = "."; |
| 37 | register char *s; |
| 38 | register char *last; |
| 39 | char *first; |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 40 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 41 | last = s = path; |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 42 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 43 | if (s != NULL) { |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 44 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 45 | LOOP: |
| 46 | while (*s && (*s != '/')) ++s; |
| 47 | first = s; |
| 48 | while (*s == '/') ++s; |
| 49 | if (*s) { |
| 50 | last = first; |
| 51 | goto LOOP; |
| 52 | } |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 53 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 54 | if (last == path) { |
| 55 | if (*last != '/') { |
| 56 | goto DOT; |
| 57 | } |
| 58 | if ((*++last == '/') && (last[1] == 0)) { |
| 59 | ++last; |
| 60 | } |
| 61 | } |
| 62 | *last = 0; |
| 63 | return path; |
| 64 | } |
| 65 | DOT: |
| 66 | return (char *) null_or_empty_or_noslash; |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 67 | } |
Matt Kraai | 80f6d55 | 2001-08-24 20:35:45 +0000 | [diff] [blame] | 68 | |
| 69 | #endif |