diff options
author | Robert James Kaes <rjkaes@users.sourceforge.net> | 2001-12-24 00:01:32 +0000 |
---|---|---|
committer | Robert James Kaes <rjkaes@users.sourceforge.net> | 2001-12-24 00:01:32 +0000 |
commit | b10221fa0717a0718aaee75e1d1964d069326062 (patch) | |
tree | 0bdf681fee41bf453b2c08ac2bd4519b61aac121 | |
parent | 475e526a487c81a74f9eb6915de53db8d89261fa (diff) | |
download | tinyproxy-b10221fa0717a0718aaee75e1d1964d069326062.tar.gz tinyproxy-b10221fa0717a0718aaee75e1d1964d069326062.zip |
Added the write_message() function (from reqs.c)
-rw-r--r-- | src/sock.c | 54 | ||||
-rw-r--r-- | src/sock.h | 3 |
2 files changed, 55 insertions, 2 deletions
@@ -1,4 +1,4 @@ -/* $Id: sock.c,v 1.22 2001-12-19 20:41:28 rjkaes Exp $ +/* $Id: sock.c,v 1.23 2001-12-24 00:01:32 rjkaes Exp $ * * Sockets are created and destroyed here. When a new connection comes in from * a client, we need to copy the socket and the create a second socket to the @@ -322,6 +322,58 @@ safe_read(int fd, char *buffer, size_t count) } /* + * Send a "message" to the file descriptor provided. This handles the + * differences between the various implementations of vsnprintf. This code + * was basically stolen from the snprintf() man page of Debian Linux + * (although I did fix a memory leak. :) + */ +int +write_message(int fd, const char *fmt, ...) +{ + ssize_t n; + size_t size = (1024 * 8); /* start with 8 KB and go from there */ + char *buf, *tmpbuf; + va_list ap; + + if ((buf = safemalloc(size)) == NULL) + return -1; + + while (1) { + va_start(ap, fmt); + n = vsnprintf(buf, size, fmt, ap); + va_end(ap); + + /* If that worked, break out so we can send the buffer */ + if (n > -1 && n < size) + break; + + /* Else, try again with more space */ + if (n > -1) + /* precisely what is needed (glibc2.1) */ + size = n + 1; + else + /* twice the old size (glibc2.0) */ + size *= 2; + + if ((tmpbuf = saferealloc(buf, size)) == NULL) { + safefree(buf); + return -1; + } else + buf = tmpbuf; + } + + if (safe_write(fd, buf, n) < 0) { + DEBUG2("Error in write_message(): %d", fd); + + safefree(buf); + return -1; + } + + safefree(buf); + return 0; +} + +/* * Read in a "line" from the socket. It might take a few loops through * the read sequence. The full string is allocate off the heap and stored * at the whole_buffer pointer. The caller needs to free the memory when @@ -1,4 +1,4 @@ -/* $Id: sock.h,v 1.8 2001-12-15 20:04:04 rjkaes Exp $ +/* $Id: sock.h,v 1.9 2001-12-24 00:01:32 rjkaes Exp $ * * See 'sock.c' for a detailed description. * @@ -38,6 +38,7 @@ extern char *getpeer_string(int fd, char *string); extern ssize_t safe_write(int fd, const char *buffer, size_t count); extern ssize_t safe_read(int fd, char *buffer, size_t count); +extern int write_message(int fd, const char *fmt, ...); extern ssize_t readline(int fd, char **whole_buffer); #endif |