summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnders F Björklund <anders.f.bjorklund@gmail.com>2018-06-23 15:04:58 +0200
committerAnders F Björklund <anders.f.bjorklund@gmail.com>2018-06-23 18:02:35 +0200
commit4e8c0c53d15a68b622aa6e0169e6d9496d9141f1 (patch)
tree41bf1dd466b7acc6f8deac9392dcccab20055d70
parent13377bb1d28a7199a9a39d9fb8c7b0d002c55e1e (diff)
downloadccache-4e8c0c53d15a68b622aa6e0169e6d9496d9141f1.tar.gz
Add util to return the command as a string
We had print_command for logging to a file
-rw-r--r--src/ccache.h1
-rw-r--r--src/execute.c18
-rw-r--r--unittest/test_util.c8
3 files changed, 27 insertions, 0 deletions
diff --git a/src/ccache.h b/src/ccache.h
index 973d42dd..ace546f5 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 8c4849a9..4589a477 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 38b44191..4fed42cc 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