Index: /trunk/archive/psdb/psDB.c
===================================================================
--- /trunk/archive/psdb/psDB.c	(revision 3204)
+++ /trunk/archive/psdb/psDB.c	(revision 3205)
@@ -8,6 +8,6 @@
  *  @author Joshua Hoblitt
  *
- *  @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-02-12 02:40:21 $
+ *  @version $Revision: 1.18 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-02-13 11:21:18 $
  *
  *  Copyright 2005 Joshua Hoblitt, University of Hawaii
@@ -15,4 +15,5 @@
 
 #include <stdio.h>
+#include <stdarg.h>
 #include <string.h>
 #include <math.h>
@@ -50,6 +51,6 @@
 // general utility functions
 static char    *psDBIntToString(psU64 n);
-static size_t   psStringAppend(char **dest, const char *source);
-static size_t   psStringPrepend(char **dest, const char *source);
+static ssize_t  psStringAppend(char **dest, const char *format, ...);
+static ssize_t psStringPrepend(char **dest, const char *format, ...);
 
 // database utility functions
@@ -1237,38 +1238,82 @@
 }
 
-static size_t psStringAppend(char **dest, const char *source)
-{
-    size_t          newSize;
-
-    // dest + source + \0
-    newSize = strlen(*dest) + strlen(source) + 1;
-
-    *dest = psRealloc(*dest, newSize);
-
-    strncat(*dest, source, newSize);
-
-    return newSize;
-}
-
-static size_t psStringPrepend(char **dest, const char *source)
-{
-    size_t          newSize;
-    char            *oldDest;
-
-    // dest + source + \0
-    newSize = strlen(*dest) + strlen(source) + 1;
-
-    // backup dest
+static ssize_t psStringAppend(char **dest, const char *format, ...)
+{
+    va_list         args;
+    size_t          length;             // complete string length (sans \0)
+    size_t          oldLength;          // original string length (sans \0)
+    ssize_t         tailLength;         // length of string to append
+
+    // size of existing string
+    oldLength = strlen(*dest);
+
+    // find the size of the string to append
+    va_start(args, format);
+    // C99 guarentees vsnprintf() to work as expected with size = 0
+    tailLength = vsnprintf(*dest, 0, format, args);
+    va_end(args);
+
+    // if the new tail is zero length, return the length of the old string.  if
+    // it's a format error, return the error code.
+    if (tailLength < 1) {
+        return tailLength == 0 ? oldLength : tailLength;
+    }
+
+    // new string length (sans \0)
+    length = oldLength + tailLength;
+
+    // realloc string to string + tail + \0
+    *dest = psRealloc(*dest, length + 1);
+
+    // append tail + \0
+    va_start(args, format);
+    vsnprintf(*dest + oldLength, tailLength + 1, format, args);
+    va_end(args);
+
+    return length;
+}
+
+static ssize_t psStringPrepend(char **dest, const char *format, ...)
+{
+    va_list         args;
+    size_t          length;             // complete string length (sans \0)
+    ssize_t         headLength;         // length of string to prepend
+    char            *oldDest;           // copy of original string
+
+    // size of existing string
+    length = strlen(*dest);
+
+    // find the size of the string to prepend
+    va_start(args, format);
+    // C99 guarentees vsnprintf() to work as expected with size = 0
+    headLength = vsnprintf(*dest, 0, format, args);
+    va_end(args);
+
+    // if the new head is zero length, return the length of the old string.  if
+    // it's a format error, return the error code.
+    if (headLength < 1) {
+        return headLength == 0 ? length : headLength;
+    }
+
+    // backup original string
     oldDest = psStringCopy(*dest);
-    *dest = psRealloc(*dest, newSize);
-
-    // copy source to the beginning of the string then append the original
-    // string
-    strncpy(*dest, source, newSize);
-    strncat(*dest, oldDest, newSize);
+
+    // new string length (sans \0)
+    length += headLength;
+
+    // realloc string to head + string + \0
+    *dest = psRealloc(*dest, length + 1);
+
+    // copy the new head to the beginning of string
+    va_start(args, format);
+    vsnprintf(*dest, length + 1, format, args);
+    va_end(args);
+
+    // append the original string
+    strncat(*dest, oldDest, length + 1);
 
     psFree(oldDest);
 
-    return newSize;
+    return length;
 }
 
