summaryrefslogtreecommitdiff
path: root/libc/inet/getservice.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2004-03-18 11:12:34 +0000
committerEric Andersen <andersen@codepoet.org>2004-03-18 11:12:34 +0000
commite32376aaed01ed44981386e348ac35b58da23495 (patch)
tree3626e95d9c5c905b74085288d6e83ff7bd8dfa78 /libc/inet/getservice.c
parent38cd8e780fc6375117e8bc7db2eca17764779bba (diff)
Reduce memory used by static buffers and allocate that memory dynamicly
instead. Based on an initial patch from Tobias Anderberg, but reworked. I asked Tobias to look into doing something more like what is done in busybox, but that proved to be a pain. One possible concern is that these buffers will probably show up as memory leaks i.e. with valgrind. Perhaps we should add in an atexit call to free this memory right after we allocate it?
Diffstat (limited to 'libc/inet/getservice.c')
-rw-r--r--libc/inet/getservice.c24
1 files changed, 20 insertions, 4 deletions
diff --git a/libc/inet/getservice.c b/libc/inet/getservice.c
index fb22ff95a..606def8e4 100644
--- a/libc/inet/getservice.c
+++ b/libc/inet/getservice.c
@@ -81,12 +81,22 @@ static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
#define MAXALIASES 35
+#define SBUFSIZE (BUFSIZ + 1 + (sizeof(char *) * MAXALIASES))
static FILE *servf = NULL;
static struct servent serv;
-static char buf[BUFSIZ+1 + sizeof(char *)*MAXALIASES];
+static char *buf = NULL;
static int serv_stayopen;
+static void __initbuf(void)
+{
+ if (!buf) {
+ buf = malloc(SBUFSIZE);
+ if (!buf)
+ abort();
+ }
+}
+
void setservent(int f)
{
LOCK;
@@ -112,7 +122,9 @@ void endservent(void)
struct servent * getservent(void)
{
struct servent *result;
- getservent_r(&serv, buf, sizeof(buf), &result);
+
+ __initbuf();
+ getservent_r(&serv, buf, SBUFSIZE, &result);
return result;
}
@@ -120,7 +132,9 @@ struct servent * getservent(void)
struct servent *getservbyname(const char *name, const char *proto)
{
struct servent *result;
- getservbyname_r(name, proto, &serv, buf, sizeof(buf), &result);
+
+ __initbuf();
+ getservbyname_r(name, proto, &serv, buf, SBUFSIZE, &result);
return result;
}
@@ -128,7 +142,9 @@ struct servent *getservbyname(const char *name, const char *proto)
struct servent * getservbyport(int port, const char *proto)
{
struct servent *result;
- getservbyport_r(port, proto, &serv, buf, sizeof(buf), &result);
+
+ __initbuf();
+ getservbyport_r(port, proto, &serv, buf, SBUFSIZE, &result);
return result;
}