From 83c523cf530292844c0c8e89689a96cebd205d45 Mon Sep 17 00:00:00 2001 From: Sascha Kruse Date: Wed, 19 Dec 2012 22:34:47 +0100 Subject: [PATCH] asprintf wrapper --- dunst.c | 16 ++++++++-------- options.c | 8 ++++---- utils.c | 16 ++++++++++++++-- utils.h | 1 + 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/dunst.c b/dunst.c index 154acb5..59b5d7a 100644 --- a/dunst.c +++ b/dunst.c @@ -506,7 +506,7 @@ void add_notification_to_line_cache(notification *n, int max_width) /* print dup_count */ if (n->dup_count > 0) { - asprintf(&buf, "(%d)", n->dup_count); + sasprintf(&buf, "(%d)", n->dup_count); } else { buf = strdup(""); } @@ -514,7 +514,7 @@ void add_notification_to_line_cache(notification *n, int max_width) /* print msg */ { char *new_buf; - asprintf(&new_buf, "%s %s", buf, msg); + sasprintf(&new_buf, "%s %s", buf, msg); free(buf); buf = new_buf; } @@ -530,13 +530,13 @@ void add_notification_to_line_cache(notification *n, int max_width) char *new_buf; if (hours > 0) { - asprintf(&new_buf, "%s (%dh %dm %ds old)", buf, hours, + sasprintf(&new_buf, "%s (%dh %dm %ds old)", buf, hours, minutes, seconds); } else if (minutes > 0) { - asprintf(&new_buf, "%s (%dm %ds old)", buf, minutes, + sasprintf(&new_buf, "%s (%dm %ds old)", buf, minutes, seconds); } else { - asprintf(&new_buf, "%s (%ds old)", buf, seconds); + sasprintf(&new_buf, "%s (%ds old)", buf, seconds); } free(buf); @@ -694,7 +694,7 @@ void fill_line_cache(int width) if (indicate_hidden && queue_cnt > 0) { if (geometry.h != 1) { char *tmp; - asprintf(&tmp, "(%d more)", queue_cnt); + sasprintf(&tmp, "(%d more)", queue_cnt); ColorSet *last_colors = line_cache.lines[line_cache.count-1].colors; r_line_cache_append(&line_cache, tmp, last_colors, false); @@ -702,7 +702,7 @@ void fill_line_cache(int width) } else { char *old = line_cache.lines[0].str; char *new; - asprintf(&new, "%s (%d more)", old, queue_cnt); + sasprintf(&new, "%s (%d more)", old, queue_cnt); free(old); line_cache.lines[0].str = new; } @@ -1045,7 +1045,7 @@ int init_notification(notification * n, int id) } char *tmp; - asprintf(&tmp, "%s %s", n->summary, n->body); + sasprintf(&tmp, "%s %s", n->summary, n->body); n->urls = extract_urls(tmp); diff --git a/options.c b/options.c index ab90b57..2bc5418 100644 --- a/options.c +++ b/options.c @@ -438,18 +438,18 @@ void cmdline_usage_append(char *key, char *type, char *description) { char *key_type; if (type && strlen(type) > 0) - asprintf(&key_type, "%s (%s)", key, type); + sasprintf(&key_type, "%s (%s)", key, type); else - asprintf(&key_type, "%s", key); + sasprintf(&key_type, "%s", key); if (!usage_str) { - asprintf(&usage_str, "%-40s - %s\n", key_type, description); + sasprintf(&usage_str, "%-40s - %s\n", key_type, description); free(key_type); return; } char *tmp; - asprintf(&tmp, "%s%-40s - %s\n", usage_str, key_type, description); + sasprintf(&tmp, "%s%-40s - %s\n", usage_str, key_type, description); free(key_type); free(usage_str); diff --git a/utils.c b/utils.c index a915336..d759602 100644 --- a/utils.c +++ b/utils.c @@ -55,9 +55,9 @@ char *string_append(char *a, const char *b, const char *sep) char *new; if (!sep) - asprintf(&new, "%s%s", a, b); + sasprintf(&new, "%s%s", a, b); else - asprintf(&new, "%s%s%s", a, sep, b); + sasprintf(&new, "%s%s%s", a, sep, b); free(a); return new; @@ -108,4 +108,16 @@ void die(char *text, int exit_value) exit(exit_value); } +int sasprintf(char **strp, const char *fmt, ...) +{ + va_list args; + int result; + + va_start(args, fmt); + if ((result = vasprintf(strp, fmt, args)) == -1) + die("asprintf failed", EXIT_FAILURE); + va_end(args); + return result; +} + /* vim: set ts=8 sw=8 tw=0: */ diff --git a/utils.h b/utils.h index c93479d..25bd62f 100644 --- a/utils.h +++ b/utils.h @@ -17,6 +17,7 @@ void die(char *msg, int exit_value); int digit_count(int i); +int sasprintf(char **strp, const char *fmt, ...); #endif /* vim: set ts=8 sw=8 tw=0: */