xref: /freebsd/lib/libc/gen/dirname_compat.c (revision 1f474190)
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 <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
21 
22 #include <errno.h>
23 #include <libgen.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/param.h>
27 
28 char * __freebsd11_dirname(char *path);
29 
30 char *
31 __freebsd11_dirname(char *path)
32 {
33 	static char *dname = NULL;
34 	size_t len;
35 	const char *endp;
36 
37 	if (dname == NULL) {
38 		dname = (char *)malloc(MAXPATHLEN);
39 		if (dname == NULL)
40 			return(NULL);
41 	}
42 
43 	/* Empty or NULL string gets treated as "." */
44 	if (path == NULL || *path == '\0') {
45 		dname[0] = '.';
46 		dname[1] = '\0';
47 		return (dname);
48 	}
49 
50 	/* Strip any trailing slashes */
51 	endp = path + strlen(path) - 1;
52 	while (endp > path && *endp == '/')
53 		endp--;
54 
55 	/* Find the start of the dir */
56 	while (endp > path && *endp != '/')
57 		endp--;
58 
59 	/* Either the dir is "/" or there are no slashes */
60 	if (endp == path) {
61 		dname[0] = *endp == '/' ? '/' : '.';
62 		dname[1] = '\0';
63 		return (dname);
64 	} else {
65 		/* Move forward past the separating slashes */
66 		do {
67 			endp--;
68 		} while (endp > path && *endp == '/');
69 	}
70 
71 	len = endp - path + 1;
72 	if (len >= MAXPATHLEN) {
73 		errno = ENAMETOOLONG;
74 		return (NULL);
75 	}
76 	memcpy(dname, path, len);
77 	dname[len] = '\0';
78 	return (dname);
79 }
80 
81 __sym_compat(dirname, __freebsd11_dirname, FBSD_1.0);
82