[libvirt PATCH 12/19] commandhelper: Factor out printCwd

Tim Wiederhake twiederh at redhat.com
Fri Jan 29 16:16:22 UTC 2021


Signed-off-by: Tim Wiederhake <twiederh at redhat.com>
---
 tests/commandhelper.c | 42 ++++++++++++++++++++++++++----------------
 1 file changed, 26 insertions(+), 16 deletions(-)

diff --git a/tests/commandhelper.c b/tests/commandhelper.c
index 01dd6f9e45..929de7a05d 100644
--- a/tests/commandhelper.c
+++ b/tests/commandhelper.c
@@ -166,10 +166,34 @@ static void printDaemonization(FILE *log, struct Arguments *args)
     fprintf(log, "DAEMON:%s\n", getpgrp() != getppid() ? "yes" : "no");
 }
 
+static int printCwd(FILE *log)
+{
+    char *cwd = NULL;
+    char *display;
+
+    if (!(cwd = getcwd(NULL, 0)))
+        return -1;
+
+    if ((strlen(cwd) > strlen(".../commanddata")) &&
+        (STREQ(cwd + strlen(cwd) - strlen("/commanddata"), "/commanddata"))) {
+        strcpy(cwd, ".../commanddata");
+    }
+
+    display = cwd;
+
+# ifdef __APPLE__
+    if (strstr(cwd, "/private"))
+        display = cwd + strlen("/private");
+# endif
+
+    fprintf(log, "CWD:%s\n", display);
+    free(cwd);
+    return 0;
+}
+
 int main(int argc, char **argv) {
     struct Arguments *args = parseArguments(argc, argv);
     size_t i;
-    char *cwd;
     FILE *log = fopen(abs_builddir "/commandhelper.log", "w");
     int ret = EXIT_FAILURE;
     struct pollfd fds[3];
@@ -191,22 +215,8 @@ int main(int argc, char **argv) {
 
     printDaemonization(log, args);
 
-    if (!(cwd = getcwd(NULL, 0)))
+    if (printCwd(log) != 0)
         goto cleanup;
-    if (strlen(cwd) > strlen(".../commanddata") &&
-        STREQ(cwd + strlen(cwd) - strlen("/commanddata"), "/commanddata"))
-        strcpy(cwd, ".../commanddata");
-# ifdef __APPLE__
-    char *noprivateprefix = NULL;
-    if (strstr(cwd, "/private"))
-        noprivateprefix = cwd + strlen("/private");
-    else
-        noprivateprefix = cwd;
-    fprintf(log, "CWD:%s\n", noprivateprefix);
-# else
-    fprintf(log, "CWD:%s\n", cwd);
-# endif
-    free(cwd);
 
     fprintf(log, "UMASK:%04o\n", umask(0));
 
-- 
2.26.2




More information about the libvir-list mailing list