Skip to content

Commit

Permalink
add argv_prepend/argv_prependn
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandwalker committed Aug 15, 2017
1 parent a9e57d5 commit d743cfd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/tig/argv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
22 changes: 22 additions & 0 deletions src/argv.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,35 @@ 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)
{
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[])
{
Expand Down

0 comments on commit d743cfd

Please sign in to comment.