summaryrefslogtreecommitdiffstats
path: root/arguments.c
diff options
context:
space:
mode:
authornicm <nicm>2015-11-27 15:06:43 +0000
committernicm <nicm>2015-11-27 15:06:43 +0000
commit6a2ca34216530c687027cf9e767d2b46c85976e6 (patch)
tree9f0cb8f7f41ea4e93102bc764f7388ae6b5b0d5c /arguments.c
parentac8678aefe157d7e40c5bcedd12333eaedf0df92 (diff)
Do not set a limit on the length of commands when printing them.
Diffstat (limited to 'arguments.c')
-rw-r--r--arguments.c82
1 files changed, 39 insertions, 43 deletions
diff --git a/arguments.c b/arguments.c
index 54753de3..0a42cc38 100644
--- a/arguments.c
+++ b/arguments.c
@@ -128,77 +128,73 @@ args_free(struct args *args)
free(args);
}
+/* Add to string. */
+static void printflike(3, 4)
+args_print_add(char **buf, size_t *len, const char *fmt, ...)
+{
+ va_list ap;
+ char *s;
+ size_t slen;
+
+ va_start(ap, fmt);
+ slen = xvasprintf(&s, fmt, ap);
+ va_end(ap);
+
+ *len += slen;
+ *buf = xrealloc(*buf, *len);
+
+ strlcat(*buf, s, *len);
+ free(s);
+}
+
/* Print a set of arguments. */
-size_t
-args_print(struct args *args, char *buf, size_t len)
+char *
+args_print(struct args *args)
{
- size_t off, used;
+ size_t len;
+ char *buf;
int i;
- const char *quotes;
struct args_entry *entry;
- /* There must be at least one byte at the start. */
- if (len == 0)
- return (0);
- off = 0;
+ len = 1;
+ buf = xcalloc(1, len);
/* Process the flags first. */
- buf[off++] = '-';
RB_FOREACH(entry, args_tree, &args->tree) {
if (entry->value != NULL)
continue;
- if (off == len - 1) {
- buf[off] = '\0';
- return (len);
- }
- buf[off++] = entry->flag;
- buf[off] = '\0';
+ if (*buf == '\0')
+ args_print_add(&buf, &len, "-");
+ args_print_add(&buf, &len, "%c", entry->flag);
}
- if (off == 1)
- buf[--off] = '\0';
/* Then the flags with arguments. */
RB_FOREACH(entry, args_tree, &args->tree) {
if (entry->value == NULL)
continue;
- if (off >= len) {
- /* snprintf will have zero terminated. */
- return (len);
- }
-
+ if (*buf != '\0')
+ args_print_add(&buf, &len, " -%c ", entry->flag);
+ else
+ args_print_add(&buf, &len, "-%c ", entry->flag);
if (strchr(entry->value, ' ') != NULL)
- quotes = "\"";
+ args_print_add(&buf, &len, "\"%s\"", entry->value);
else
- quotes = "";
- used = xsnprintf(buf + off, len - off, "%s-%c %s%s%s",
- off != 0 ? " " : "", entry->flag, quotes, entry->value,
- quotes);
- if (used > len - off)
- used = len - off;
- off += used;
+ args_print_add(&buf, &len, "%s", entry->value);
}
/* And finally the argument vector. */
for (i = 0; i < args->argc; i++) {
- if (off >= len) {
- /* snprintf will have zero terminated. */
- return (len);
- }
-
+ if (*buf != '\0')
+ args_print_add(&buf, &len, " ");
if (strchr(args->argv[i], ' ') != NULL)
- quotes = "\"";
+ args_print_add(&buf, &len, "\"%s\"", args->argv[i]);
else
- quotes = "";
- used = xsnprintf(buf + off, len - off, "%s%s%s%s",
- off != 0 ? " " : "", quotes, args->argv[i], quotes);
- if (used > len - off)
- used = len - off;
- off += used;
+ args_print_add(&buf, &len, "%s", args->argv[i]);
}
- return (off);
+ return (buf);
}
/* Return if an argument is present. */