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 #define __need_NULL
9 #include <stddef.h>
10 #include <libgen.h>
11 
dirname(char * path)12 char *dirname(char *path)
13 {
14 	static const char null_or_empty_or_noslash[] = ".";
15 	register char *s;
16 	register char *last;
17 	char *first;
18 
19 	last = s = path;
20 
21 	if (s != NULL) {
22 
23 	LOOP:
24 		while (*s && (*s != '/')) ++s;
25 		first = s;
26 		while (*s == '/') ++s;
27 		if (*s) {
28 			last = first;
29 			goto LOOP;
30 		}
31 
32 		if (last == path) {
33 			if (*last != '/') {
34 				goto DOT;
35 			}
36 			if ((*++last == '/') && (last[1] == 0)) {
37 				++last;
38 			}
39 		}
40 		*last = 0;
41 		return path;
42 	}
43  DOT:
44 	return (char *) null_or_empty_or_noslash;
45 }
46