summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorgoodale <goodale@17b73243-c579-4c4c-a9d2-2d5706c11dac>2001-05-24 15:22:37 +0000
committergoodale <goodale@17b73243-c579-4c4c-a9d2-2d5706c11dac>2001-05-24 15:22:37 +0000
commit46dd11fbb66a9b7647a09239b99f3b914b4bad8a (patch)
tree1c56c89fc02bffe6657c4802d60145f8b9f3d954 /src
parent051b4e81e2296d3c02836aee3ec815309a037f70 (diff)
Added Util_asprintf,Util_asnprintf,Util_snprintf,Util_vsnprintf.
asprintf is like sprintf, but the first arg is a char ** and should basically be the address of a null pointer. The routine allocates enough space and passes back the new buffer pointer. asnprintf is as above, but the buffer need not be null. The thirs argument is the size of the current buffer, and if the string would be larger than that, the current buffer is freed and a new buffer big enough is allocated. snprintf and vsnprintf are pretty standard, except we don't have them on all platforms so I hunted down a file on the internet, written by Patrick Powel (papowell@astart.com), to give us a portable version of the functionality. It may not be entirely up to scratch with floating point - I've not tested it too thouroughly yet. This should enable us to make the web-server stuff a bit more secure against buffer-overruns. Tom git-svn-id: http://svn.cactuscode.org/flesh/trunk@2209 17b73243-c579-4c4c-a9d2-2d5706c11dac
Diffstat (limited to 'src')
-rw-r--r--src/include/util_String.h10
-rw-r--r--src/util/String.c155
-rw-r--r--src/util/make.code.defn1
3 files changed, 165 insertions, 1 deletions
diff --git a/src/include/util_String.h b/src/include/util_String.h
index 2dab9a7d..1496b89c 100644
--- a/src/include/util_String.h
+++ b/src/include/util_String.h
@@ -5,9 +5,11 @@
@desc
String routines
@enddesc
- @version $History$
+ @version $Header$
@@*/
+#include <stdarg.h>
+
#ifndef _UTIL_STRING_H_
#define _UTIL_STRING_H_ 1
@@ -33,6 +35,12 @@ char *Util_Strdup(const char *s);
int Util_StrCmpi(const char *string1,
const char *string2);
+int Util_vsnprintf (char *str, size_t count, const char *fmt, va_list args);
+int Util_snprintf (char *str,size_t count,const char *fmt,...);
+
+int Util_asprintf(char **buffer, const char *fmt, ...);
+int Util_asnprintf(char **buffer, size_t size, const char *fmt, ...);
+
#ifdef __cplusplus
}
#endif
diff --git a/src/util/String.c b/src/util/String.c
index a6ad8290..eab3893d 100644
--- a/src/util/String.c
+++ b/src/util/String.c
@@ -9,6 +9,7 @@
@@*/
#include <stdlib.h>
+#include <stdarg.h>
#include <string.h>
#include <ctype.h>
@@ -400,6 +401,160 @@ int Util_SplitFilename(char **dir, char **file, const char *string)
return retval;
}
+ /*@@
+ @routine Util_asprintf
+ @date Thu May 24 16:55:26 2001
+ @author Tom Goodale
+ @desc
+ Sprintf with memory allocation. On input
+ the buffer should point to a NULL area of memory.
+ @enddesc
+ @calls
+ @calledby
+ @history
+
+ @endhistory
+ @var buffer
+ @vdesc Buffer to which to print the string.
+ @vtype char **
+ @vio out
+ @vcomment
+ *buffer should be NULL on entry. The routine
+ allocates the memory, so the previous contents of
+ the pointer are lost.
+ On exit the buffer size will be return-value+1 (i.e
+ the length of the string plus the \0 ).
+ @endvar
+ @var format
+ @vdesc sprintf format string
+ @vtype const char *
+ @vio in
+ @vcomment
+ This is a standard sprintf format string.
+ @endvar
+ @var ...
+ @vdesc Rest of arguments
+ @vtype varargs
+ @vio in
+ @vcomment
+ These are the arguments necessary for the format string.
+ @endvar
+
+ @returntype int
+ @returndesc
+ The number of bytes written to the buffer.
+ @endreturndesc
+@@*/
+int Util_asprintf(char **buffer, const char *fmt, ...)
+{
+ int count;
+ va_list args;
+
+ va_start(args,fmt);
+
+ count = Util_vsnprintf(NULL, 0, fmt, args);
+
+ *buffer = (char *)malloc(count+1);
+
+ if(*buffer)
+ {
+ va_start(args,fmt);
+
+ Util_vsnprintf(*buffer,count+1,fmt,args);
+
+ va_end(args);
+ }
+ else
+ {
+ count = 0;
+ }
+
+ return count;
+}
+
+ /*@@
+ @routine Util_asprintf
+ @date Thu May 24 16:55:26 2001
+ @author Tom Goodale
+ @desc
+ Sprintf with memory allocation if necessary. On input
+ the buffer should point to an area of memory of length 'size' .
+ @enddesc
+ @calls
+ @calledby
+ @history
+
+ @endhistory
+ @var buffer
+ @vdesc Buffer to which to print the string.
+ @vtype char **
+ @vio out
+ @vcomment
+ Buffer to which to print string. If the buffer is too
+ small, the buffer is freed and a new buffer big enough to hold
+ the string and its null-termination is created.
+ @endvar
+ @var size
+ @vdesc initial size of the buffer
+ @vtype int
+ @vio in
+ @vcomment
+ This is the initial size of the buffer.
+ @endvar
+ @var format
+ @vdesc sprintf format string
+ @vtype const char *
+ @vio in
+ @vcomment
+ This is a standard sprintf format string.
+ @endvar
+ @var ...
+ @vdesc Rest of arguments
+ @vtype varargs
+ @vio in
+ @vcomment
+ These are the arguments necessary for the format string.
+ @endvar
+
+ @returntype int
+ @returndesc
+ The number of bytes written to the buffer.
+ @endreturndesc
+@@*/
+int Util_asnprintf(char **buffer, size_t size, const char *fmt, ...)
+{
+ int count;
+ va_list args;
+
+ va_start(args,fmt);
+
+ count = Util_vsnprintf(NULL, 0, fmt, args);
+
+ if(count+1 > size)
+ {
+ /* Use free followed by malloc as realloc may copy memory
+ * we are not interested in.
+ */
+ free(*buffer);
+ *buffer = (char *)malloc(count+1);
+ }
+
+ if(*buffer)
+ {
+ va_start(args,fmt);
+
+ Util_vsnprintf(*buffer,count+1,fmt,args);
+
+ va_end(args);
+ }
+ else
+ {
+ count = 0;
+ }
+
+ return count;
+}
+
/********************************************************************
********************* Local Routines *************************
diff --git a/src/util/make.code.defn b/src/util/make.code.defn
index a19d541a..3b505258 100644
--- a/src/util/make.code.defn
+++ b/src/util/make.code.defn
@@ -12,6 +12,7 @@ SKBinTree.c\
Hash.c\
Cache.c\
Malloc.c\
+snprintf.c\
String.c\
StringList.c\
Time.c\