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