[PATCH:libXt 1/3 v2] Add XtAsprintf() as a new exported API

Alan Coopersmith alan.coopersmith at oracle.com
Wed Jan 12 23:59:57 PST 2011


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 at oracle.com>
---

Changes since v1 (patch 2/4 in the Dec 13 series):
 - bump libXt version in configure.ac
 - add man pages
 - update error checks similar to those put in xrdb version during review

 COPYING                 |    2 +-
 configure.ac            |    2 +-
 include/X11/Intrinsic.h |    9 +++++++++
 man/Makefile.am         |    3 ++-
 man/XtAsprintf.man      |    1 +
 man/XtMalloc.man        |   18 ++++++++++++++++++
 src/Alloc.c             |   41 ++++++++++++++++++++++++++++++++++++++++-
 7 files changed, 72 insertions(+), 4 deletions(-)
 create mode 100644 man/XtAsprintf.man

diff --git a/COPYING b/COPYING
index 5507aa4..9e121b1 100644
--- a/COPYING
+++ b/COPYING
@@ -19,7 +19,7 @@ DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 PERFORMANCE OF THIS SOFTWARE.
 
-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"),
diff --git a/configure.ac b/configure.ac
index ed295e0..f429f12 100644
--- a/configure.ac
+++ b/configure.ac
@@ -23,7 +23,7 @@ dnl Process this file with autoconf to create configure.
 
 # Initialize Autoconf
 AC_PREREQ([2.60])
-AC_INIT([libXt], [1.0.9],
+AC_INIT([libXt], [1.0.99.1],
         [https://bugs.freedesktop.org/enter_bug.cgi?product=xorg], [libXt])
 AC_CONFIG_MACRO_DIR([m4])
 AC_CONFIG_SRCDIR([Makefile.am])
diff --git a/include/X11/Intrinsic.h b/include/X11/Intrinsic.h
index 5111537..d09acad 100644
--- a/include/X11/Intrinsic.h
+++ b/include/X11/Intrinsic.h
@@ -1855,6 +1855,15 @@ extern void XtFree(
     char*		/* ptr */
 );
 
+#ifndef _X_RESTRICT_KYWD
+# define _X_RESTRICT_KYWD
+#endif
+extern Cardinal XtAsprintf(
+    String *new_string,
+    _Xconst char * _X_RESTRICT_KYWD format,
+    ...
+) _X_ATTRIBUTE_PRINTF(2,3);
+
 #ifdef XTTRACEMEMORY
 
 extern char *_XtMalloc( /* implementation-private */
diff --git a/man/Makefile.am b/man/Makefile.am
index 005c2d5..305598d 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -330,7 +330,8 @@ XtMalloc_shadows =				\
 	XtRealloc				\
 	XtFree					\
 	XtNew					\
-	XtNewString
+	XtNewString				\
+	XtAsprintf
 
 XtManageChildren_shadows =			\
 	XtManageChild				\
diff --git a/man/XtAsprintf.man b/man/XtAsprintf.man
new file mode 100644
index 0000000..50eaf76
--- /dev/null
+++ b/man/XtAsprintf.man
@@ -0,0 +1 @@
+.so man__libmansuffix__/XtMalloc.__libmansuffix__
diff --git a/man/XtMalloc.man b/man/XtMalloc.man
index 396218b..70183ec 100644
--- a/man/XtMalloc.man
+++ b/man/XtMalloc.man
@@ -139,6 +139,8 @@ void XtFree(char *\fIptr\fP);
 \fItype\fP *XtNew(\fItype\fP); 
 .HP
 String XtNewString(String \fIstring\fP); 
+.HP
+Cardinal XtAsprintf(String *\fInew_string\fP, const char *\fIformat\fP, ...);
 .SH ARGUMENTS
 .IP \fInum\fP 1i
 Specifies the number of bytes or array elements.
@@ -152,6 +154,10 @@ desired.
 Specifies a previously declared string.
 .IP \fItype\fP 1i
 Specifies a previously declared data type.
+.IP \fInew_string\fP 1i
+Specifies a pointer to write a newly allocated string to.
+.IP \fIformat\fP 1i
+Specifies a formatting string as defined by sprintf(3c)
 .SH DESCRIPTION
 The
 .ZN XtMalloc
@@ -226,6 +232,18 @@ with the following arguments specified:
 .ta .5i
 (strcpy(XtMalloc((unsigned) strlen(str) + 1), str))
 .De
+.LP
+The
+.ZN XtAsprintf
+function allocates space for a string large enough to hold the string
+specified by the sprintf(3c) format pattern when used with the remaining
+arguments, and fills it with the formatted results.
+The address of the allocated string is placed into the pointer passed as ret.
+The length of the string (not including the terminating null byte) is returned.
+If there is insufficient memory to allocate the new block,
+.ZN XtAsprintf
+calls
+.ZN XtErrorMsg .
 .SH "SEE ALSO"
 .br
 \fI\*(xT\fP
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(
-- 
1.7.3.2



More information about the xorg-devel mailing list