diff options
Diffstat (limited to 'src/Alloc.c')
-rw-r--r-- | src/Alloc.c | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/src/Alloc.c b/src/Alloc.c index 94cb90a..67413ab 100644 --- a/src/Alloc.c +++ b/src/Alloc.c @@ -1,5 +1,5 @@ /*********************************************************** -Copyright (c) 1993, Oracle and/or its affiliates. All rights reserved. +Copyright (c) 1993, 2011, Oracle and/or its affiliates. All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), @@ -82,6 +82,8 @@ in this Software without prior written authorization from The Open Group. #undef _XBCOPYFUNC #include <stdlib.h> +#include <stdio.h> +#include <stdarg.h> #define Xmalloc(size) malloc((size)) #define Xrealloc(ptr, size) realloc((ptr), (size)) @@ -121,6 +123,43 @@ void _XtHeapInit( heap->bytes_remaining = 0; } +/* Version of asprintf() using XtMalloc + * Not currently available in XTTRACEMEMORY version, since that would + * require varargs macros everywhere, which are only standard in C99 & later. + */ +Cardinal XtAsprintf( + String *new_string, + _Xconst char * _X_RESTRICT_KYWD format, + ...) +{ + char buf[256]; + Cardinal len; + va_list ap; + + va_start(ap, format); + len = vsnprintf(buf, sizeof(buf), format, ap); + va_end(ap); + + if (len < 0) + _XtAllocError("vsnprintf"); + + *new_string = XtMalloc(len + 1); /* snprintf doesn't count trailing '\0' */ + if (len < sizeof(buf)) + { + strncpy(*new_string, buf, len); + new_string[len] = '\0'; + } + else + { + va_start(ap, format); + if (vsnprintf(*new_string, len + 1, format, ap) < 0) + _XtAllocError("vsnprintf"); + va_end(ap); + } + return len; +} + + #ifndef XTTRACEMEMORY char *XtMalloc( |