9base

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

_p9translate.c (890B)


      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 plan9translate(char *old)
     18 {
     19 	char *new;
     20 	int i, olen, nlen, len;
     21 
     22 	if(replace[0].new == nil){
     23 		replace[0].new = getenv("PLAN9");
     24 		if(replace[0].new == nil)
     25 			replace[0].new = PREFIX;
     26 	}
     27 
     28 	for(i=0; i<nelem(replace); i++){
     29 		if(!replace[i].new)
     30 			continue;
     31 		olen = strlen(replace[i].old);
     32 		if(strncmp(old, replace[i].old, olen) != 0
     33 		|| (old[olen] != '\0' && old[olen] != '/'))
     34 			continue;
     35 		nlen = strlen(replace[i].new);
     36 		len = strlen(old)+nlen-olen;
     37 		new = malloc(len+1);
     38 		if(new == nil)
     39 			return "<out of memory>";
     40 		strcpy(new, replace[i].new);
     41 		strcpy(new+nlen, old+olen);
     42 		assert(strlen(new) == len);
     43 		return new;
     44 	}
     45 	return old;
     46 }