Added the write_message() function (from reqs.c)

This commit is contained in:
Robert James Kaes 2001-12-24 00:01:32 +00:00
parent 475e526a48
commit b10221fa07
2 changed files with 55 additions and 2 deletions

View File

@ -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
@ -321,6 +321,58 @@ safe_read(int fd, char *buffer, size_t count)
return len;
}
/*
* 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

View File

@ -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