diff options
author | Alan Coopersmith <alan.coopersmith@oracle.com> | 2011-01-12 23:09:29 -0800 |
---|---|---|
committer | Alan Coopersmith <alan.coopersmith@oracle.com> | 2011-01-12 23:09:29 -0800 |
commit | ac07da253415ccb8b6322feedf7969967357050e (patch) | |
tree | 20bdac2612a5ca5ee04d6e51669e71b4c60d09f1 /src/Alloc.c | |
parent | 84f73c49db8071c06f27609ce7cc7a32a17351a9 (diff) |
Add XtAsprintf() as a new exported API
Like asprintf() but using XtMalloc() to tie into the Xt memory allocation
and error handling subsystems.
Bumps libXt version to 1.0.99.1 so that modules can set their pkg-config
dependency to libXt >= 1.0.99.1 to require XtAsprintf().
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
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( |