blob: 81298730b4452f58befb4cbd4ffcc326661a1814 [file] [log] [blame]
Matt Kraaiceeff732001-06-21 19:41:37 +00001/* vi: set sw=4 ts=4: */
2/*
Manuel Novoa III cad53642003-03-19 09:13:01 +00003 * dirname implementation for busybox (for libc's missing one)
Matt Kraaiceeff732001-06-21 19:41:37 +00004 *
Manuel Novoa III cad53642003-03-19 09:13:01 +00005 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
Matt Kraaiceeff732001-06-21 19:41:37 +00006 *
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 cad53642003-03-19 09:13:01 +000020 *
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 Kraaiceeff732001-06-21 19:41:37 +000026 */
27
Manuel Novoa III a2949aa2001-06-29 18:59:32 +000028#include <string.h>
Matt Kraaiceeff732001-06-21 19:41:37 +000029#include "libbb.h"
30
Manuel Novoa III cad53642003-03-19 09:13:01 +000031#if __GNU_LIBRARY__ < 5
Matt Kraai80f6d552001-08-24 20:35:45 +000032
Manuel Novoa III cad53642003-03-19 09:13:01 +000033extern
Matt Kraai80f6d552001-08-24 20:35:45 +000034char *dirname(char *path)
Matt Kraaiceeff732001-06-21 19:41:37 +000035{
Manuel Novoa III cad53642003-03-19 09:13:01 +000036 static const char null_or_empty_or_noslash[] = ".";
37 register char *s;
38 register char *last;
39 char *first;
Matt Kraaiceeff732001-06-21 19:41:37 +000040
Manuel Novoa III cad53642003-03-19 09:13:01 +000041 last = s = path;
Matt Kraaiceeff732001-06-21 19:41:37 +000042
Manuel Novoa III cad53642003-03-19 09:13:01 +000043 if (s != NULL) {
Matt Kraaiceeff732001-06-21 19:41:37 +000044
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 LOOP:
46 while (*s && (*s != '/')) ++s;
47 first = s;
48 while (*s == '/') ++s;
49 if (*s) {
50 last = first;
51 goto LOOP;
52 }
Matt Kraaiceeff732001-06-21 19:41:37 +000053
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 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 Kraaiceeff732001-06-21 19:41:37 +000067}
Matt Kraai80f6d552001-08-24 20:35:45 +000068
69#endif