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 listen(char*, char*);
extern char* netmkaddr(char*, char*, char*);
extern char* netmkaddrbuf(char*, char*, char*, char*, int);
extern int reject(int, char*, char*);
/*

View file

@ -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 <u.h>
.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

View file

@ -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));
}