summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/sock.c54
-rw-r--r--src/sock.h3
2 files changed, 55 insertions, 2 deletions
diff --git a/src/sock.c b/src/sock.c
index 3fa0833..181ac3d 100644
--- a/src/sock.c
+++ b/src/sock.c
@@ -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
diff --git a/src/sock.h b/src/sock.h
index 5b09f63..67a8200 100644
--- a/src/sock.h
+++ b/src/sock.h
@@ -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