xref: /freebsd/contrib/bmake/dirname.c (revision 1fb62fb074788ca4713551be09d6569966a3abee)
1 /*	$NetBSD: dirname.c,v 1.13 2014/07/16 10:52:26 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Klaus Klein and Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifdef HAVE_CONFIG_H
33 # include <config.h>
34 #endif
35 #ifndef HAVE_DIRNAME
36 
37 #include <sys/cdefs.h>
38 #include <sys/param.h>
39 #ifdef HAVE_LIBGEN_H
40 #include <libgen.h>
41 #endif
42 #ifdef HAVE_LIMITS_H
43 #include <limits.h>
44 #endif
45 #ifdef HAVE_STRING_H
46 #include <string.h>
47 #endif
48 #ifndef PATH_MAX
49 # define PATH_MAX 1024
50 #endif
51 #ifndef MIN
52 # define MIN(a, b) ((a < b) ? a : b)
53 #endif
54 
55 
56 static size_t
57 xdirname_r(const char *path, char *buf, size_t buflen)
58 {
59 	const char *endp;
60 	size_t len;
61 
62 	/*
63 	 * If `path' is a null pointer or points to an empty string,
64 	 * return a pointer to the string ".".
65 	 */
66 	if (path == NULL || *path == '\0') {
67 		path = ".";
68 		len = 1;
69 		goto out;
70 	}
71 
72 	/* Strip trailing slashes, if any. */
73 	endp = path + strlen(path) - 1;
74 	while (endp != path && *endp == '/')
75 		endp--;
76 
77 	/* Find the start of the dir */
78 	while (endp > path && *endp != '/')
79 		endp--;
80 
81 	if (endp == path) {
82 		path = *endp == '/' ? "/" : ".";
83 		len = 1;
84 		goto out;
85 	}
86 
87 	do
88 		endp--;
89 	while (endp > path && *endp == '/');
90 
91 	len = endp - path + 1;
92 out:
93 	if (buf != NULL && buflen != 0) {
94 		buflen = MIN(len, buflen - 1);
95 		memcpy(buf, path, buflen);
96 		buf[buflen] = '\0';
97 	}
98 	return len;
99 }
100 
101 char *
102 dirname(char *path)
103 {
104 	static char result[PATH_MAX];
105 	(void)xdirname_r(path, result, sizeof(result));
106 	return result;
107 }
108 
109 #ifdef MAIN
110 #include <stdlib.h>
111 #include <stdio.h>
112 
113 int
114 main(int argc, char *argv[])
115 {
116 	printf("%s\n", dirname(argv[1]));
117 	exit(0);
118 }
119 #endif
120 #endif
121