9base

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

unsharp.c (927B)


      1 #include <u.h>
      2 #include <libc.h>
      3 
      4 /*
      5  * I don't want too many of these,
      6  * but the ones we have are just too useful. 
      7  */
      8 static struct {
      9 	char *old;
     10 	char *new;
     11 } replace[] = {
     12 	"#9", nil,	/* must be first */
     13 	"#d", "/dev/fd",
     14 };
     15 
     16 char*
     17 unsharp(char *old)
     18 {
     19 	char *new;
     20 	int i, olen, nlen, len;
     21 
     22 	if(replace[0].new == nil)
     23 		replace[0].new = get9root();
     24 
     25 	for(i=0; i<nelem(replace); i++){
     26 		if(!replace[i].new)
     27 			continue;
     28 		olen = strlen(replace[i].old);
     29 		if(strncmp(old, replace[i].old, olen) != 0
     30 		|| (old[olen] != '\0' && old[olen] != '/'))
     31 			continue;
     32 		nlen = strlen(replace[i].new);
     33 		len = strlen(old)+nlen-olen;
     34 		new = malloc(len+1);
     35 		if(new == nil)
     36 			/* Most callers won't check the return value... */
     37 			sysfatal("out of memory translating %s to %s%s", old, replace[i].new, old+olen);
     38 		strcpy(new, replace[i].new);
     39 		strcpy(new+nlen, old+olen);
     40 		assert(strlen(new) == len);
     41 		return new;
     42 	}
     43 	return old;
     44 }