diff options
| author | 2015-11-27 15:06:43 +0000 | |
|---|---|---|
| committer | 2015-11-27 15:06:43 +0000 | |
| commit | 8ae71cbb81e6a97f6b0f8f8f0ddba8c9837e743f (patch) | |
| tree | facedba4a31a3bcb326d5918ed3f195673c8cbf7 /usr.bin/tmux/arguments.c | |
| parent | Keep lo(4) definitions inside if_loop.c (diff) | |
| download | wireguard-openbsd-8ae71cbb81e6a97f6b0f8f8f0ddba8c9837e743f.tar.xz wireguard-openbsd-8ae71cbb81e6a97f6b0f8f8f0ddba8c9837e743f.zip | |
Do not set a limit on the length of commands when printing them.
Diffstat (limited to 'usr.bin/tmux/arguments.c')
| -rw-r--r-- | usr.bin/tmux/arguments.c | 84 |
1 files changed, 40 insertions, 44 deletions
diff --git a/usr.bin/tmux/arguments.c b/usr.bin/tmux/arguments.c index feb4faffc41..af2ffa8395d 100644 --- a/usr.bin/tmux/arguments.c +++ b/usr.bin/tmux/arguments.c @@ -1,4 +1,4 @@ -/* $OpenBSD: arguments.c,v 1.11 2015/08/29 23:19:52 nicm Exp $ */ +/* $OpenBSD: arguments.c,v 1.12 2015/11/27 15:06:43 nicm Exp $ */ /* * Copyright (c) 2010 Nicholas Marriott <nicm@users.sourceforge.net> @@ -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. */ |
