]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add a function that formats a string in a handier way than x_asprintf
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 17 Jul 2010 20:51:29 +0000 (22:51 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 17 Jul 2010 20:51:29 +0000 (22:51 +0200)
ccache.h
util.c

index dfc4f7e219326233a2d41f29006b2afd704e5304..cc7119ca45887838b4a836d387e35116212ce171 100644 (file)
--- a/ccache.h
+++ b/ccache.h
@@ -86,6 +86,7 @@ const char *tmp_string(void);
 char *format_hash_as_string(const unsigned char *hash, unsigned size);
 int create_hash_dir(char **dir, const char *hash, const char *cache_dir);
 int create_cachedirtag(const char *dir);
+char *format(const char *format, ...) ATTR_FORMAT(printf, 1, 2);
 void x_asprintf(char **ptr, const char *format, ...) ATTR_FORMAT(printf, 2, 3);
 char *x_strdup(const char *s);
 char *x_strndup(const char *s, size_t n);
diff --git a/util.c b/util.c
index 13e84ccb353041cd00430be626db06f6949ed9c7..4b3d6cad061b4c846f518fb29048c33939dcf609 100644 (file)
--- a/util.c
+++ b/util.c
@@ -471,6 +471,23 @@ error:
        return 1;
 }
 
+/* Construct a string according to a format. Caller frees. */
+char*
+format(const char *format, ...)
+{
+       va_list ap;
+       char *ptr = NULL;
+
+       va_start(ap, format);
+       if (vasprintf(&ptr, format, ap) == -1) {
+               fatal("Out of memory in format");
+       }
+       va_end(ap);
+
+       if (!*ptr) fatal("Internal error in format");
+       return ptr;
+}
+
 /*
   this is like asprintf() but dies if the malloc fails
   note that we use vsnprintf in a rather poor way to make this more portable