blob: 207a96c715e25dcca1498dee2f1306a1af67160b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
main(int argc, char **argv)
{
char *f, *s;
int n;
if(argc != 2){
fprintf(stderr, "Usage: dirname string\n");
exit(1);
}
s = argv[1];
f = s + strlen(s) - 1;
while(f > s && *f == '/')
f--;
*++f = 0;
/* now f is after last char of string, trailing slashes removed */
for(; f >= s; f--)
if(*f == '/'){
f++;
break;
}
if(f < s) {
*s = '.';
s[1] = 0;
} else {
--f;
while(f > s && *f == '/')
f--;
f[1] = 0;
}
printf("%s\n", s);
return 0;
}
|