xref: /dflybsd-src/lib/libc/gen/basename.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>
27a15492ffSJeroen Ruigrok/asmodai #include <stdlib.h>
28984263bcSMatthew Dillon #include <string.h>
29984263bcSMatthew Dillon #include <sys/param.h>
30984263bcSMatthew Dillon 
31984263bcSMatthew Dillon char *
basename(char * path)32*207ba670SMatthew Dillon basename(char *path)
33984263bcSMatthew Dillon {
34*207ba670SMatthew Dillon 	char *ptr;
35984263bcSMatthew Dillon 
36*207ba670SMatthew Dillon 	if (path == NULL || *path == 0)
37*207ba670SMatthew Dillon 		return (__DECONST(char *, "."));
38a15492ffSJeroen Ruigrok/asmodai 
39*207ba670SMatthew Dillon 	ptr = path + strlen(path);
40*207ba670SMatthew Dillon 	while (ptr > path + 1 && ptr[-1] == '/')
41*207ba670SMatthew Dillon 		--ptr;
42*207ba670SMatthew Dillon 	*ptr-- = 0;
43984263bcSMatthew Dillon 
44*207ba670SMatthew Dillon 	while (ptr > path && ptr[-1] != '/')
45*207ba670SMatthew Dillon 		--ptr;
46*207ba670SMatthew Dillon 	return ptr;
47984263bcSMatthew Dillon }
48