libc: provide netmkaddrbuf() function avoiding the static buffer

We need a reentrant version for netmkaddr() that
can be used in multithreaded programs.
This commit is contained in:
cinap_lenrek 2025-01-02 20:09:50 +00:00
parent 5ea80693ba
commit e981a26000
3 changed files with 55 additions and 39 deletions

View file

@ -543,6 +543,7 @@ extern void setnetmtpt(char*, int, char*);
extern int hangup(int); extern int hangup(int);
extern int listen(char*, char*); extern int listen(char*, char*);
extern char* netmkaddr(char*, char*, char*); extern char* netmkaddr(char*, char*, char*);
extern char* netmkaddrbuf(char*, char*, char*, char*, int);
extern int reject(int, char*, char*); extern int reject(int, char*, char*);
/* /*

View file

@ -1,6 +1,6 @@
.TH DIAL 2 .TH DIAL 2
.SH NAME .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 .SH SYNOPSIS
.B #include <u.h> .B #include <u.h>
.br .br
@ -25,7 +25,12 @@ int accept(int ctl, char *dir)
int reject(int ctl, char *dir, char *cause) int reject(int ctl, char *dir, char *cause)
.PP .PP
.B .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 .PP
.B .B
void setnetmtpt(char *to, int tolen, char *from) void setnetmtpt(char *to, int tolen, char *from)
@ -171,10 +176,19 @@ returns a file descriptor for the data file opened
.BR ORDWR . .BR ORDWR .
.PP .PP
.I Netmkaddr .I Netmkaddr
makes an address suitable for dialing or announcing. and
It takes an address along with a default network and service to use .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. 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 .PP
.I Getnetconninfo .I Getnetconninfo
returns a structure containing information about a returns a structure containing information about a

View file

@ -6,47 +6,48 @@
* make an address, add the defaults * make an address, add the defaults
*/ */
char * 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; char *cp;
cp = strchr(addr, '!');
if(cp == nil){
/* /*
* dump network name * dump network name
*/ */
cp = strchr(linear, '!'); if(defnet == nil){
if(cp == 0){ if(defsrv != nil)
if(defnet==0){ snprint(buf, len, "net!%s!%s", addr, defsrv);
if(defsrv)
snprint(addr, sizeof(addr), "net!%s!%s",
linear, defsrv);
else else
snprint(addr, sizeof(addr), "net!%s", linear); snprint(buf, len, "net!%s", addr);
} }
else { else {
if(defsrv) if(defsrv != nil)
snprint(addr, sizeof(addr), "%s!%s!%s", defnet, snprint(buf, len, "%s!%s!%s", defnet, addr, defsrv);
linear, defsrv);
else else
snprint(addr, sizeof(addr), "%s!%s", defnet, snprint(buf, len, "%s!%s", defnet, addr);
linear);
} }
return addr; } else {
}
/*
* if there is already a service, use it
*/
cp = strchr(cp+1, '!'); cp = strchr(cp+1, '!');
if(cp) if(cp != nil || defsrv == nil){
return linear; /*
* if there is already a service or no defsrv given
*/
snprint(buf, len, "%s", addr);
} else {
/* /*
* add default service * add default service
*/ */
if(defsrv == 0) snprint(buf, len,"%s!%s", addr, defsrv);
return linear; }
snprint(addr, sizeof(addr), "%s!%s", linear, defsrv); }
return buf;
return addr; }
char *
netmkaddr(char *addr, char *defnet, char *defsrv)
{
static char buf[256];
return netmkaddrbuf(addr, defnet, defsrv, buf, sizeof(buf));
} }