xref: /freebsd/lib/libc/gen/dirname_compat.c (revision 9286d46a794f25482880d29864a8901ef6666fae)
1 /*	$OpenBSD: dirname.c,v 1.13 2005/08/08 08:05:33 espie Exp $	*/
2 
3 /*
4  * Copyright (c) 1997, 2004 Todd C. Miller <Todd.Miller@courtesan.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <errno.h>
20 #include <libgen.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/param.h>
24 
25 char * __freebsd11_dirname(char *path);
26 
27 char *
28 __freebsd11_dirname(char *path)
29 {
30 	static char *dname = NULL;
31 	size_t len;
32 	const char *endp;
33 
34 	if (dname == NULL) {
35 		dname = (char *)malloc(MAXPATHLEN);
36 		if (dname == NULL)
37 			return(NULL);
38 	}
39 
40 	/* Empty or NULL string gets treated as "." */
41 	if (path == NULL || *path == '\0') {
42 		dname[0] = '.';
43 		dname[1] = '\0';
44 		return (dname);
45 	}
46 
47 	/* Strip any trailing slashes */
48 	endp = path + strlen(path) - 1;
49 	while (endp > path && *endp == '/')
50 		endp--;
51 
52 	/* Find the start of the dir */
53 	while (endp > path && *endp != '/')
54 		endp--;
55 
56 	/* Either the dir is "/" or there are no slashes */
57 	if (endp == path) {
58 		dname[0] = *endp == '/' ? '/' : '.';
59 		dname[1] = '\0';
60 		return (dname);
61 	} else {
62 		/* Move forward past the separating slashes */
63 		do {
64 			endp--;
65 		} while (endp > path && *endp == '/');
66 	}
67 
68 	len = endp - path + 1;
69 	if (len >= MAXPATHLEN) {
70 		errno = ENAMETOOLONG;
71 		return (NULL);
72 	}
73 	memcpy(dname, path, len);
74 	dname[len] = '\0';
75 	return (dname);
76 }
77 
78 __sym_compat(dirname, __freebsd11_dirname, FBSD_1.0);
79