summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@sun.com>2009-07-17 20:37:51 -0700
committerAlan Coopersmith <alan.coopersmith@sun.com>2009-07-17 21:18:06 -0700
commit56a14b8a1f4d3aa883485b794c818581b8f07cd8 (patch)
tree2ce09753683336d48858a91851647aa4274a4aa5 /util.c
parenta74131c41c63cbe5861b9550dfa7a921ec601c87 (diff)
sprintf -> snprintf/asprintf conversions
Signed-off-by: Alan Coopersmith <alan.coopersmith@sun.com>
Diffstat (limited to 'util.c')
-rw-r--r--util.c39
1 files changed, 37 insertions, 2 deletions
diff --git a/util.c b/util.c
index 72d6688..fdbb1a2 100644
--- a/util.c
+++ b/util.c
@@ -54,6 +54,41 @@ from The Open Group.
#endif
#endif
+#ifndef HAVE_ASPRINTF
+# include <stdarg.h>
+/* Allocating sprintf found in many newer libc's
+ * Since xdm is single threaded, assumes arguments don't change
+ * between initial length calculation and copy to result buffer.
+ */
+int
+Asprintf(char ** ret, const char *restrict format, ...)
+{
+ va_list ap;
+ int len;
+ char buf[256];
+
+ va_start(ap, format);
+ len = vsnprintf(buf, sizeof(buf), format, ap);
+ if (len >= 0) {
+ *ret = malloc(len + 1);
+ if (*ret) {
+ if (len < sizeof(buf)) {
+ memcpy(*ret, buf, len + 1);
+ } else {
+ vsnprintf(*ret, len + 1, format, ap);
+ }
+ } else {
+ len = -1;
+ }
+ } else {
+ *ret = NULL;
+ }
+ va_end(ap);
+
+ return len;
+}
+#endif /* !HAVE_ASPRINTF */
+
void
printEnv (char **e)
{
@@ -66,12 +101,12 @@ makeEnv (char *name, char *value)
{
char *result;
- result = malloc ((unsigned) (strlen (name) + strlen (value) + 2));
+ asprintf(&result, "%s=%s", name, value);
+
if (!result) {
LogOutOfMem ("makeEnv");
return NULL;
}
- sprintf (result, "%s=%s", name, value);
return result;
}