]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add util to return the command as a string
authorAnders F Björklund <anders.f.bjorklund@gmail.com>
Sat, 23 Jun 2018 13:04:58 +0000 (15:04 +0200)
committerAnders F Björklund <anders.f.bjorklund@gmail.com>
Sat, 23 Jun 2018 16:02:35 +0000 (18:02 +0200)
We had print_command for logging to a file

src/ccache.h
src/execute.c
unittest/test_util.c

index 973d42dd3c7af9cda1fb245117f9533430e738d2..ace546f546ff4cb8c0808abff22a8d6440a20898 100644 (file)
@@ -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
index 8c4849a9fbb0aa2f3f7aa52a306521833d9ff3f1..4589a477f489ad15e6816b47cc7c024f2c5c5b81 100644 (file)
@@ -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;
+}
index 38b44191157298b9e630641b30d693818b33bbcc..4fed42ccd59dcd195341a06f39951f3c73229a0f 100644 (file)
@@ -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