aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/cleanname.c
blob: 8fa1846661a95051088962ea3d4d0c8a0281bdea (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
39
40
41
42
43
44
#include <u.h>
#include <libc.h>

void
main(int argc, char **argv)
{
	char *dir;
	char *name;
	int i;

	dir = nil;
	ARGBEGIN{
	case 'd':
		if((dir=ARGF()) == nil)
			goto Usage;
		break;
	default:
		goto Usage;
	}ARGEND;

	if(argc < 1) {
	Usage:
		fprint(2, "usage: cleanname [-d pwd] name...\n");
		exits("usage");
	}

	for(i=0; i<argc; i++) {
		if(dir == nil || argv[i][0] == '/') {
			cleanname(argv[i]);
			print("%s\n", argv[i]);
		} else {
			name = malloc(strlen(argv[i])+1+strlen(dir)+1);
			if(name == nil) {
				fprint(2, "cleanname: out of memory\n");
				exits("out of memory");
			}
			sprint(name, "%s/%s", dir, argv[i]);
			cleanname(name);
			print("%s\n", name);
			free(name);
		}
	}
	exits(0);
}