From: Anders F Björklund Date: Sat, 23 Jun 2018 13:04:58 +0000 (+0200) Subject: Add util to return the command as a string X-Git-Tag: v3.5~34^2~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4e8c0c53d15a68b622aa6e0169e6d9496d9141f1;p=thirdparty%2Fccache.git Add util to return the command as a string We had print_command for logging to a file --- diff --git a/src/ccache.h b/src/ccache.h index 973d42dd3..ace546f54 100644 --- a/src/ccache.h +++ b/src/ccache.h @@ -243,6 +243,7 @@ void wipe_all(struct conf *conf); int execute(char **argv, int fd_out, int fd_err, pid_t *pid); char *find_executable(const char *name, const char *exclude_name); void print_command(FILE *fp, char **argv); +char *string_command(char **argv); // ---------------------------------------------------------------------------- // lockfile.c diff --git a/src/execute.c b/src/execute.c index 8c4849a9f..4589a477f 100644 --- a/src/execute.c +++ b/src/execute.c @@ -348,3 +348,21 @@ print_command(FILE *fp, char **argv) } fprintf(fp, "\n"); } + +char * +string_command(char **argv) +{ + size_t len = 0; + for (int i = 0; argv[i]; i++) { + len += (i == 0) ? 0 : 1; + len += strlen(argv[i]); + } + len += 1; + char *buf = x_calloc(1, len + 1); + for (int i = 0; argv[i]; i++) { + strcat(buf, (i == 0) ? "" : " "); + strcat(buf, argv[i]); + } + strcat(buf, "\n"); + return buf; +} diff --git a/unittest/test_util.c b/unittest/test_util.c index 38b441911..4fed42ccd 100644 --- a/unittest/test_util.c +++ b/unittest/test_util.c @@ -199,4 +199,12 @@ TEST(parse_size_with_suffix) } } +TEST(string_command) +{ + char *argv[] = {"foo", "bar", NULL}; + + CHECK_STR_EQ_FREE2("foo bar\n", string_command(argv)); + +} + TEST_SUITE_END