From d743cfd58138dfe38084781c2839f3e749b15a46 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Tue, 1 Aug 2017 08:20:00 -0400 Subject: [PATCH] add argv_prepend/argv_prependn --- include/tig/argv.h | 2 ++ src/argv.c | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/tig/argv.h b/include/tig/argv.h index 72af2a0cb..156ae2aab 100644 --- a/include/tig/argv.h +++ b/include/tig/argv.h @@ -34,6 +34,8 @@ size_t argv_size(const char **argv); bool argv_append(const char ***argv, const char *arg); bool argv_appendn(const char ***argv, const char *arg, size_t arglen); bool argv_append_array(const char ***dst_argv, const char *src_argv[]); +bool argv_prepend(const char ***argv, const char *arg); +bool argv_prependn(const char ***argv, const char *arg, size_t arglen); bool argv_copy(const char ***dst, const char *src[]); bool argv_contains(const char **argv, const char *arg); diff --git a/src/argv.c b/src/argv.c index dbdf6873f..b159c056c 100644 --- a/src/argv.c +++ b/src/argv.c @@ -239,6 +239,22 @@ argv_appendn(const char ***argv, const char *arg, size_t arglen) return alloc != NULL; } +bool +argv_prependn(const char ***argv, const char *arg, size_t arglen) +{ + if (!argv_appendn(argv,arg,strlen(arg))) + return false; + + size_t last_i = argv_size(*argv) - 1; + + if (last_i > 0) { + const char *save = (*argv)[last_i]; + memmove(*argv + 1, *argv, last_i * sizeof(*argv)); + (*argv)[0] = save; + } + + return true; +} bool argv_append(const char ***argv, const char *arg) @@ -246,6 +262,12 @@ argv_append(const char ***argv, const char *arg) return argv_appendn(argv, arg, strlen(arg)); } +bool +argv_prepend(const char ***argv, const char *arg) +{ + return argv_prependn(argv, arg, strlen(arg)); +} + bool argv_append_array(const char ***dst_argv, const char *src_argv[]) {