char *format_hash_as_string(const unsigned char *hash, int size);
int create_cachedirtag(const char *dir);
char *format(const char *format, ...) ATTR_FORMAT(printf, 1, 2);
+void reformat(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);
void *x_malloc(size_t size);
void *x_calloc(size_t nmemb, size_t size);
void *x_realloc(void *ptr, size_t size);
-void x_asprintf2(char **ptr, const char *format, ...)
- ATTR_FORMAT(printf, 2, 3);
void traverse(const char *dir, void (*fn)(const char *, struct stat *));
char *basename(const char *path);
char *dirname(const char *path);
/*
- * This is like x_asprintf() but frees *ptr if *ptr != NULL.
+ * Construct a string according to the format and store it in *ptr. The
+ * original *ptr is then freed.
*/
void
-x_asprintf2(char **ptr, const char *format, ...)
+reformat(char **ptr, const char *format, ...)
{
char *saved = *ptr;
va_list ap;
*ptr = NULL;
va_start(ap, format);
if (vasprintf(ptr, format, ap) == -1) {
- fatal("Out of memory in x_asprintf2");
+ fatal("Out of memory in reformat");
}
va_end(ap);
- if (!ptr) fatal("Out of memory in x_asprintf2");
+ if (!ptr) fatal("Out of memory in reformat");
if (saved) {
free(saved);
}
common_prefix_len = common_dir_prefix_length(from, to);
for (p = from + common_prefix_len; *p; p++) {
if (*p == '/') {
- x_asprintf2(&result, "../%s", result);
+ reformat(&result, "../%s", result);
}
}
if (strlen(to) > common_prefix_len) {
while (*p == '/') {
p++;
}
- x_asprintf2(&result, "%s%s", result, p);
+ reformat(&result, "%s%s", result, p);
}
i = strlen(result) - 1;
while (i >= 0 && result[i] == '/') {
if (q == p) {
/* Special case: don't consider a single $ the start of a variable. */
- x_asprintf2(result, "%s$", *result);
+ reformat(result, "%s$", *result);
return true;
}
free(name);
return false;
}
- x_asprintf2(result, "%s%s", *result, value);
+ reformat(result, "%s%s", *result, value);
if (!curly) {
--q;
}
q = str;
for (q = str; *q; ++q) {
if (*q == '$') {
- x_asprintf2(&result, "%s%.*s", result, (int)(q - p), p);
+ reformat(&result, "%s%.*s", result, (int)(q - p), p);
if (!expand_variable(&q, &result, errmsg)) {
free(result);
return NULL;
p = q + 1;
}
}
- x_asprintf2(&result, "%s%.*s", result, (int)(q - p), p);
+ reformat(&result, "%s%.*s", result, (int)(q - p), p);
return result;
}