1/*	$OpenBSD: dirname.c,v 1.17 2020/10/20 19:30:14 naddy Exp $	*/
2
3/*
4 * Copyright (c) 1997, 2004 Todd C. Miller <millert@openbsd.org>
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 <limits.h>
22#include <string.h>
23
24/* A slightly modified copy of this file exists in libexec/ld.so */
25
26char *
27dirname(char *path)
28{
29	static char dname[PATH_MAX];
30	size_t len;
31	const char *endp;
32
33	/* Empty or NULL string gets treated as "." */
34	if (path == NULL || *path == '\0') {
35		dname[0] = '.';
36		dname[1] = '\0';
37		return (dname);
38	}
39
40	/* Strip any trailing slashes */
41	endp = path + strlen(path) - 1;
42	while (endp > path && *endp == '/')
43		endp--;
44
45	/* Find the start of the dir */
46	while (endp > path && *endp != '/')
47		endp--;
48
49	/* Either the dir is "/" or there are no slashes */
50	if (endp == path) {
51		dname[0] = *endp == '/' ? '/' : '.';
52		dname[1] = '\0';
53		return (dname);
54	} else {
55		/* Move forward past the separating slashes */
56		do {
57			endp--;
58		} while (endp > path && *endp == '/');
59	}
60
61	len = endp - path + 1;
62	if (len >= sizeof(dname)) {
63		errno = ENAMETOOLONG;
64		return (NULL);
65	}
66	memcpy(dname, path, len);
67	dname[len] = '\0';
68	return (dname);
69}
70