]> xenbits.xensource.com Git - libvirt.git/commitdiff
commandhelper: Factor out printCwd
authorTim Wiederhake <twiederh@redhat.com>
Mon, 1 Feb 2021 11:27:56 +0000 (12:27 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 2 Feb 2021 14:00:54 +0000 (15:00 +0100)
Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
tests/commandhelper.c

index cceeb1a119c4956c608cdb7cdb247b1b58b2907b..ce3f64fc9d77df543c07c770127eff5dbce8a2e9 100644 (file)
@@ -176,10 +176,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];
@@ -201,22 +225,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));