about summary refs log tree commit diff
path: root/src/misc/dirname.c
blob: 8f70dbb1feb1773fd5ed6325c374b749d2ab5e58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string.h>
#include <libgen.h>

char *dirname(char *s)
{
	size_t i;
	if (!s || !*s || !strchr(s, '/')) return ".";
	i = strlen(s)-1;
	for (; i&&s[i]=='/'; i--);
	for (; i&&s[i-1]!='/'; i--);
	for (; i&&s[i-1]=='/'; i--);
	if (!i && *s=='/') i++;
	s[i] = 0;
	return s;
}