9base

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

netmkaddr.c (1194B)


      1 #include <u.h>
      2 #include <libc.h>
      3 #include <ctype.h>
      4 
      5 /*
      6  *  make an address, add the defaults
      7  */
      8 char *
      9 netmkaddr(char *linear, char *defnet, char *defsrv)
     10 {
     11 	static char addr[256];
     12 	char *cp;
     13 
     14 	/*
     15 	 *  dump network name
     16 	 */
     17 	cp = strchr(linear, '!');
     18 	if(cp == 0){
     19 		if(defnet == 0)
     20 			defnet = "net";
     21 		/* allow unix sockets to omit unix! prefix */
     22 		if(access(linear, 0) >= 0){
     23 			snprint(addr, sizeof(addr), "unix!%s", linear);
     24 			return addr;
     25 		}
     26 		/* allow host:service in deference to Unix convention */
     27 		if((cp = strchr(linear, ':')) != nil){
     28 			snprint(addr, sizeof(addr), "%s!%.*s!%s", 
     29 				defnet, utfnlen(linear, cp-linear),
     30 				linear, cp+1);
     31 			return addr;
     32 		}
     33 		if(defsrv)
     34 			snprint(addr, sizeof(addr), "%s!%s!%s",
     35 				defnet, linear, defsrv);
     36 		else
     37 			snprint(addr, sizeof(addr), "%s!%s", defnet, linear);
     38 		return addr;
     39 	}
     40 
     41 	/*
     42 	 *  if there is already a service, use it
     43 	 */
     44 	cp = strchr(cp+1, '!');
     45 	if(cp)
     46 		return linear;
     47 
     48 	/*
     49 	 * if the network is unix, no service
     50 	 */
     51 	if(strncmp(linear, "unix!", 5) == 0)
     52 		return linear;
     53 
     54 	/*
     55 	 *  add default service
     56 	 */
     57 	if(defsrv == 0)
     58 		return linear;
     59 
     60 	snprint(addr, sizeof(addr), "%s!%s", linear, defsrv);
     61 	return addr;
     62 }