From: Stephen D. <sd...@us...> - 2007-06-25 23:19:03
|
Update of /cvsroot/naviserver/naviserver/nsd In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv12808/nsd Modified Files: dstring.c Makefile Removed Files: dsprintf.c Log Message: * include/ns.h: * nsd/Makefile: * nsd/dsprintf.c (removed): * nsd/dstring.c: Implement Ns_DStringVPrintf() using OS vsnprintf() to pick up modern format specifiers and bug fixes (dsprintf.c was 7 year old NetBSD code). This is similar to the old AOLserver 3.x implementation except that instead of printing into a large stack buffer and ignoring overflows, taking the hit of always copying the result into the dstring, we manipulate the dstring buffer and print directly into it. (There are only 2 instances in the current test suite where we overflow static space and need to grow.) NB: Win32 is handled, but untested. I don't think we care about Irix any more, which seems to have been a concern of the old code. Index: dstring.c =================================================================== RCS file: /cvsroot/naviserver/naviserver/nsd/dstring.c,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** dstring.c 19 Feb 2006 14:23:40 -0000 1.4 --- dstring.c 25 Jun 2007 23:18:19 -0000 1.5 *************** *** 162,165 **** --- 162,257 ---- /* *---------------------------------------------------------------------- + * + * Ns_DStringVPrintf -- + * + * Append a sequence of values using a format string. + * + * Results: + * Pointer to the current string value. + * + * Side effects: + * None. + * + *---------------------------------------------------------------------- + */ + + char * + Ns_DStringVPrintf(Ns_DString *dsPtr, CONST char *fmt, va_list ap) + { + char *buf; + int origLength, newLength, bufLength, result; + + origLength = dsPtr->length; + + /* + * Extend the dstring, trying first to firt everything in the + * static space (unless it is unreasonably small), or if + * we already have an allocated buffer just bump it up by 1k. + */ + + if (dsPtr->spaceAvl < TCL_INTEGER_SPACE) { + newLength = dsPtr->length + 1024; + } else { + newLength = dsPtr->spaceAvl -1; /* leave space for dstring NIL */ + } + Ns_DStringSetLength(dsPtr, newLength); + + /* + * Now that any dstring buffer relocation has taken place it's + * safe to point into the middle of it at the end of the + * existing data. + */ + + buf = dsPtr->string + origLength; + bufLength = newLength - origLength; + + #ifdef __WIN32 + result = _vsnprintf_s(buf, bufLength, fmt, ap); + #else + result = vsnprintf(buf, bufLength, fmt, ap); + #endif + + /* + * Check for overflow and retry. For win32 just double the buffer size + * and iterate, otherwise we should get this correct first time. + */ + + #ifdef __WIN32 + while (result == -1 && errno == ERANGE) { + newLength = dsPtr->spaceAvl * 2; + #else + if (result >= bufLength) { + newLength = dsPtr->spaceAvl + (result - bufLength); + #endif + + Ns_DStringSetLength(dsPtr, newLength); + + buf = dsPtr->string + origLength; + bufLength = newLength - origLength; + + #ifdef __WIN32 + result = _vsnprintf_s(buf, bufLength, fmt, ap); + #else + result = vsnprintf(buf, bufLength, fmt, ap); + #endif + } + + /* + * Set the dstring buffer to the actual length. + * NB: Eat any errors. + */ + + if (result > 0) { + Ns_DStringSetLength(dsPtr, origLength + result); + } else { + Ns_DStringSetLength(dsPtr, origLength); + } + + return Ns_DStringValue(dsPtr); + } + + + /* + *---------------------------------------------------------------------- * Ns_DStringAppendArgv -- * Index: Makefile =================================================================== RCS file: /cvsroot/naviserver/naviserver/nsd/Makefile,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** Makefile 14 May 2007 23:45:08 -0000 1.16 --- Makefile 25 Jun 2007 23:18:20 -0000 1.17 *************** *** 40,44 **** LOBJS = adpcmds.o adpeval.o adpparse.o adprequest.o auth.o binder.o \ cache.o callbacks.o cls.o compress.o config.o conn.o connio.o cookies.o \ ! crypt.o dns.o driver.o dsprintf.o dstring.o encoding.o event.o exec.o \ fastpath.o fd.o filter.o form.o httptime.o index.o info.o \ init.o limits.o lisp.o listen.o log.o mimetypes.o modload.o nsconf.o \ --- 40,44 ---- LOBJS = adpcmds.o adpeval.o adpparse.o adprequest.o auth.o binder.o \ cache.o callbacks.o cls.o compress.o config.o conn.o connio.o cookies.o \ ! crypt.o dns.o driver.o dstring.o encoding.o event.o exec.o \ fastpath.o fd.o filter.o form.o httptime.o index.o info.o \ init.o limits.o lisp.o listen.o log.o mimetypes.o modload.o nsconf.o \ --- dsprintf.c DELETED --- |