snprintf.c

00001 /*
00002  * $Id: snprintf.c 5147 2007-05-08 15:36:22Z ajc $
00003  */
00027 #include "webcit.h"
00028 #include "webserver.h"
00029 
00035 static int needed(const char *fmt, va_list argp)
00036 {
00037         static FILE *sink = NULL;
00038 
00044         if (sink == NULL) {
00045                 if ((sink = fopen("/dev/null", "w")) == NULL) {
00046                         perror("/dev/null");
00047                         exit(1);
00048                 }
00049         }
00050         return vfprintf(sink, fmt, argp);
00051 }
00052 
00060 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp)
00061 {
00062         char *p;
00063         int size;
00064 
00065         if ((p = malloc(needed(fmt, argp) + 1)) == NULL) {
00066                 lprintf(1, "vsnprintf: malloc failed, aborting\n");
00067                 abort();
00068         }
00069         if ((size = vsprintf(p, fmt, argp)) >= max)
00070                 size = -1;
00071 
00072         strncpy(buf, p, max);
00073         buf[max - 1] = 0;
00074         free(p);
00075         return size;
00076 }
00077 
00085 int snprintf(char *buf, size_t max, const char *fmt,...)
00086 {
00087         va_list argp;
00088         int bytes;
00089 
00090         va_start(argp, fmt);
00091         bytes = vsnprintf(buf, max, fmt, argp);
00092         va_end(argp);
00093 
00094         return bytes;
00095 }
00096 
00097 
00098 

Generated on Wed Jun 20 23:13:10 2007 for webcit by  doxygen 1.5.2