1 /* @(#)dirname.c	1.1 10/05/08 Copyright 2010 J. Schilling */
2 /*
3  *	dirname() to be used if missing in libc
4  *
5  *	Copyright (c) 2010 J. Schilling
6  */
7 /*
8  * The contents of this file are subject to the terms of the
9  * Common Development and Distribution License, Version 1.0 only
10  * (the "License").  You may not use this file except in compliance
11  * with the License.
12  *
13  * See the file CDDL.Schily.txt in this distribution for details.
14  * A copy of the CDDL is also available via the Internet at
15  * http://www.opensource.org/licenses/cddl1.txt
16  *
17  * When distributing Covered Code, include this CDDL HEADER in each
18  * file and include the License file CDDL.Schily.txt from this distribution.
19  */
20 
21 #include <schily/libgen.h>
22 #include <schily/string.h>
23 #include <schily/schily.h>
24 
25 #ifndef	HAVE_DIRNAME
26 
27 EXPORT char *
dirname(name)28 dirname(name)
29 	char	*name;
30 {
31 	char	*n;
32 
33 	if (name == 0 || *name == '\0')
34 		return (".");
35 
36 	n = name + strlen(name);
37 	while (n != name && *--n == '/')
38 		;
39 
40 	if (n == name && *n == '/')
41 		return ("/");
42 
43 	while (n != name) {
44 		if (*--n == '/') {
45 			while (*n == '/' && n != name)
46 				n--;
47 			*++n = '\0';
48 			return (name);
49 		}
50 	}
51 	return (".");
52 }
53 
54 #endif
55