diff --git a/sys/include/libc.h b/sys/include/libc.h index c3b3b881f..d9ac69ac8 100644 --- a/sys/include/libc.h +++ b/sys/include/libc.h @@ -543,6 +543,7 @@ extern void setnetmtpt(char*, int, char*); extern int hangup(int); extern int listen(char*, char*); extern char* netmkaddr(char*, char*, char*); +extern char* netmkaddrbuf(char*, char*, char*, char*, int); extern int reject(int, char*, char*); /* diff --git a/sys/man/2/dial b/sys/man/2/dial index c5e90e216..2d3587aff 100644 --- a/sys/man/2/dial +++ b/sys/man/2/dial @@ -1,6 +1,6 @@ .TH DIAL 2 .SH NAME -dial, hangup, announce, listen, accept, reject, netmkaddr, setnetmtpt, getnetconninfo, freenetconninfo \- make and break network connections +dial, hangup, announce, listen, accept, reject, netmkaddr, netmkaddrbuf, setnetmtpt, getnetconninfo, freenetconninfo \- make and break network connections .SH SYNOPSIS .B #include .br @@ -25,7 +25,12 @@ int accept(int ctl, char *dir) int reject(int ctl, char *dir, char *cause) .PP .B -char* netmkaddr(char *addr, char *defnet, char *defservice) +char* netmkaddr(char *addr, char *defnet, char *defsrv) +.PP +.B +char* netmkaddrbuf(char *addr, char *defnet, char *defsrv, +.br + char *buf, int len) .PP .B void setnetmtpt(char *to, int tolen, char *from) @@ -171,10 +176,19 @@ returns a file descriptor for the data file opened .BR ORDWR . .PP .I Netmkaddr -makes an address suitable for dialing or announcing. -It takes an address along with a default network and service to use +and +.I netmkaddrbuf +make an address suitable for dialing or announcing. +They take an address along with a default network and service to use if they are not specified in the address. -It returns a pointer to static data holding the actual address to use. +.I Netmkaddrbuf +stores the resulting dialstring in +.I buf +up to a maximum length of +.I len +and returns a pointer to it. +.I Netmkaddr +returns a pointer to a static buffer. .PP .I Getnetconninfo returns a structure containing information about a diff --git a/sys/src/libc/port/netmkaddr.c b/sys/src/libc/port/netmkaddr.c index fd53f4684..6b2c7716d 100644 --- a/sys/src/libc/port/netmkaddr.c +++ b/sys/src/libc/port/netmkaddr.c @@ -6,47 +6,48 @@ * make an address, add the defaults */ char * -netmkaddr(char *linear, char *defnet, char *defsrv) +netmkaddrbuf(char *addr, char *defnet, char *defsrv, char *buf, int len) { - static char addr[256]; char *cp; - /* - * dump network name - */ - cp = strchr(linear, '!'); - if(cp == 0){ - if(defnet==0){ - if(defsrv) - snprint(addr, sizeof(addr), "net!%s!%s", - linear, defsrv); + cp = strchr(addr, '!'); + if(cp == nil){ + /* + * dump network name + */ + if(defnet == nil){ + if(defsrv != nil) + snprint(buf, len, "net!%s!%s", addr, defsrv); else - snprint(addr, sizeof(addr), "net!%s", linear); + snprint(buf, len, "net!%s", addr); } else { - if(defsrv) - snprint(addr, sizeof(addr), "%s!%s!%s", defnet, - linear, defsrv); + if(defsrv != nil) + snprint(buf, len, "%s!%s!%s", defnet, addr, defsrv); else - snprint(addr, sizeof(addr), "%s!%s", defnet, - linear); + snprint(buf, len, "%s!%s", defnet, addr); + } + } else { + cp = strchr(cp+1, '!'); + if(cp != nil || defsrv == nil){ + /* + * if there is already a service or no defsrv given + */ + snprint(buf, len, "%s", addr); + } else { + /* + * add default service + */ + snprint(buf, len,"%s!%s", addr, defsrv); } - return addr; } - - /* - * if there is already a service, use it - */ - cp = strchr(cp+1, '!'); - if(cp) - return linear; - - /* - * add default service - */ - if(defsrv == 0) - return linear; - snprint(addr, sizeof(addr), "%s!%s", linear, defsrv); - - return addr; + return buf; +} + +char * +netmkaddr(char *addr, char *defnet, char *defsrv) +{ + static char buf[256]; + + return netmkaddrbuf(addr, defnet, defsrv, buf, sizeof(buf)); }