xref: /dflybsd-src/lib/libc/gen/dirname.c (revision 207ba6700e31c36bb4e38d4da221a4f86e466ee9)
1984263bcSMatthew Dillon /*
2*207ba670SMatthew Dillon  * Copyright (c) 2015-2016 Nuxi, https://nuxi.nl/
3984263bcSMatthew Dillon  *
4984263bcSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
5984263bcSMatthew Dillon  * modification, are permitted provided that the following conditions
6984263bcSMatthew Dillon  * are met:
7984263bcSMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
8984263bcSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
9984263bcSMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
10984263bcSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
11984263bcSMatthew Dillon  *    documentation and/or other materials provided with the distribution.
12984263bcSMatthew Dillon  *
13*207ba670SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14*207ba670SMatthew Dillon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*207ba670SMatthew Dillon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*207ba670SMatthew Dillon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17*207ba670SMatthew Dillon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*207ba670SMatthew Dillon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*207ba670SMatthew Dillon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20*207ba670SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21*207ba670SMatthew Dillon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22*207ba670SMatthew Dillon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23*207ba670SMatthew Dillon  * SUCH DAMAGE.
24984263bcSMatthew Dillon  */
25984263bcSMatthew Dillon #include <errno.h>
26984263bcSMatthew Dillon #include <libgen.h>
277b43ee22SJeroen Ruigrok/asmodai #include <stdlib.h>
28984263bcSMatthew Dillon #include <string.h>
29984263bcSMatthew Dillon #include <sys/param.h>
30984263bcSMatthew Dillon 
31984263bcSMatthew Dillon char *
dirname(char * path)32*207ba670SMatthew Dillon dirname(char *path)
33984263bcSMatthew Dillon {
34*207ba670SMatthew Dillon 	char *end;
35984263bcSMatthew Dillon 
36*207ba670SMatthew Dillon 	/*
37*207ba670SMatthew Dillon 	 * If path is a null pointer or points to an empty string,
38*207ba670SMatthew Dillon 	 * dirname() shall return a pointer to the string ".".
39*207ba670SMatthew Dillon 	 */
40*207ba670SMatthew Dillon 	if (path == NULL || *path == '\0')
41*207ba670SMatthew Dillon 		return (__DECONST(char *, "."));
42*207ba670SMatthew Dillon 
43*207ba670SMatthew Dillon 	/* Find end of last pathname component. */
44*207ba670SMatthew Dillon 	end = path + strlen(path);
45*207ba670SMatthew Dillon 	while (end > path + 1 && end[-1] == '/')
46*207ba670SMatthew Dillon 		--end;
47*207ba670SMatthew Dillon 
48*207ba670SMatthew Dillon 	/* Strip off the last pathname component. */
49*207ba670SMatthew Dillon 	while (end > path && end[-1] != '/')
50*207ba670SMatthew Dillon 		--end;
51*207ba670SMatthew Dillon 
52*207ba670SMatthew Dillon 	/*
53*207ba670SMatthew Dillon 	 * If path does not contain a '/', then dirname() shall return a
54*207ba670SMatthew Dillon 	 * pointer to the string ".".
55*207ba670SMatthew Dillon 	 */
56*207ba670SMatthew Dillon 	if (end == path) {
57*207ba670SMatthew Dillon 		path[0] = '.';
58*207ba670SMatthew Dillon 		path[1] = '\0';
59*207ba670SMatthew Dillon 		return (path);
607b43ee22SJeroen Ruigrok/asmodai 	}
617b43ee22SJeroen Ruigrok/asmodai 
62*207ba670SMatthew Dillon 	/*
63*207ba670SMatthew Dillon 	 * Remove trailing slashes from the resulting directory name. Ensure
64*207ba670SMatthew Dillon 	 * that at least one character remains.
65*207ba670SMatthew Dillon 	 */
66*207ba670SMatthew Dillon 	while (end > path + 1 && end[-1] == '/')
67*207ba670SMatthew Dillon 		--end;
68984263bcSMatthew Dillon 
69*207ba670SMatthew Dillon 	/* Null terminate directory name and return it. */
70*207ba670SMatthew Dillon 	*end = '\0';
71*207ba670SMatthew Dillon 	return (path);
72984263bcSMatthew Dillon }
73