9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

cleanname.c (715B)


      1 #include <u.h>
      2 #include <libc.h>
      3 
      4 void
      5 main(int argc, char **argv)
      6 {
      7 	char *dir;
      8 	char *name;
      9 	int i;
     10 
     11 	dir = nil;
     12 	ARGBEGIN{
     13 	case 'd':
     14 		if((dir=ARGF()) == nil)
     15 			goto Usage;
     16 		break;
     17 	default:
     18 		goto Usage;
     19 	}ARGEND;
     20 
     21 	if(argc < 1) {
     22 	Usage:
     23 		fprint(2, "usage: cleanname [-d pwd] name...\n");
     24 		exits("usage");
     25 	}
     26 
     27 	for(i=0; i<argc; i++) {
     28 		if(dir == nil || argv[i][0] == '/') {
     29 			cleanname(argv[i]);
     30 			print("%s\n", argv[i]);
     31 		} else {
     32 			name = malloc(strlen(argv[i])+1+strlen(dir)+1);
     33 			if(name == nil) {
     34 				fprint(2, "cleanname: out of memory\n");
     35 				exits("out of memory");
     36 			}
     37 			sprint(name, "%s/%s", dir, argv[i]);
     38 			cleanname(name);
     39 			print("%s\n", name);
     40 			free(name);
     41 		}
     42 	}
     43 	exits(0);
     44 }