[PATCH 2/2] build: fix getcwd portability problems

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* bootstrap.conf (gnulib_modules): Add getcwd-lgpl.
* tests/commandtest.c (checkoutput): Drop unused cwd.
* tests/commandhelper.c (main): Let getcwd malloc.
* tests/testutils.c (virTestMain): Likewise.
* tools/virsh.c (cmdPwd): Likewise.
(virshCmds): Expose cmdPwd and cmdCd on mingw.
---

> Oh, and now that gnulib getcwd-lgpl exists, we could use it
> to fix our uses of getcwd(NULL,0), followup coming later.

 bootstrap.conf        |    1 +
 tests/commandhelper.c |    7 ++++---
 tests/commandtest.c   |    4 ----
 tests/testutils.c     |    3 +--
 tools/virsh.c         |   34 ++++++++--------------------------
 5 files changed, 14 insertions(+), 35 deletions(-)

diff --git a/bootstrap.conf b/bootstrap.conf
index 3b3a90f..fde00da 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -36,6 +36,7 @@ dirname-lgpl
 fcntl-h
 func
 getaddrinfo
+getcwd-lgpl
 gethostname
 getpass
 gettext-h
diff --git a/tests/commandhelper.c b/tests/commandhelper.c
index 46c00f4..d60d505 100644
--- a/tests/commandhelper.c
+++ b/tests/commandhelper.c
@@ -51,6 +51,7 @@ int main(int argc, char **argv) {
     int i, n;
     char **origenv;
     char **newenv;
+    char *cwd;
     FILE *log = fopen(abs_builddir "/commandhelper.log", "w");

     if (!log)
@@ -99,13 +100,13 @@ int main(int argc, char **argv) {
     }

     fprintf(log, "DAEMON:%s\n", getpgrp() == getsid(0) ? "yes" : "no");
-    char cwd[1024];
-    if (!getcwd(cwd, sizeof(cwd)))
+    if (!(cwd = getcwd(NULL, 0)))
         return EXIT_FAILURE;
-    if (strlen(cwd) > strlen("/commanddata") &&
+    if (strlen(cwd) > strlen(".../commanddata") &&
         STREQ(cwd + strlen(cwd) - strlen("/commanddata"), "/commanddata"))
         strcpy(cwd, ".../commanddata");
     fprintf(log, "CWD:%s\n", cwd);
+    VIR_FREE(cwd);

     VIR_FORCE_FCLOSE(log);

diff --git a/tests/commandtest.c b/tests/commandtest.c
index 509c888..fa0061c 100644
--- a/tests/commandtest.c
+++ b/tests/commandtest.c
@@ -49,15 +49,11 @@ mymain(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
 static int checkoutput(const char *testname)
 {
     int ret = -1;
-    char cwd[1024];
     char *expectname = NULL;
     char *expectlog = NULL;
     char *actualname = NULL;
     char *actuallog = NULL;

-    if (!getcwd(cwd, sizeof(cwd)))
-        return -1;
-
     if (virAsprintf(&expectname, "%s/commanddata/%s.log", abs_srcdir,
                     testname) < 0)
         goto cleanup;
diff --git a/tests/testutils.c b/tests/testutils.c
index 456a735..91035a2 100644
--- a/tests/testutils.c
+++ b/tests/testutils.c
@@ -478,7 +478,6 @@ int virtTestMain(int argc,
                  int (*func)(void))
 {
     int ret;
-    char cwd[PATH_MAX];
 #if TEST_OOM
     int approxAlloc = 0;
     int n;
@@ -491,7 +490,7 @@ int virtTestMain(int argc,

     abs_srcdir = getenv("abs_srcdir");
     if (!abs_srcdir)
-        abs_srcdir = getcwd(cwd, sizeof(cwd));
+        abs_srcdir = getcwd(NULL, 0);
     if (!abs_srcdir)
         exit(EXIT_AM_HARDFAIL);

diff --git a/tools/virsh.c b/tools/virsh.c
index 0212b99..506572b 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -9893,7 +9893,6 @@ editReadBackFile (vshControl *ctl, const char *filename)
 }


-#ifndef WIN32
 /*
  * "cd" command
  */
@@ -9936,9 +9935,6 @@ cmdCd(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
     return ret;
 }

-#endif
-
-#ifndef WIN32
 /*
  * "pwd" command
  */
@@ -9952,30 +9948,20 @@ static bool
 cmdPwd(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
 {
     char *cwd;
-    size_t path_max;
-    bool err = true;
-
-    path_max = (size_t) PATH_MAX + 2;
-    cwd = vshMalloc (ctl, path_max);
-    while (cwd) {
-        err = getcwd (cwd, path_max) == NULL;
-        if (!err || errno != ERANGE)
-            break;
-
-        path_max *= 2;
-        cwd = vshRealloc (ctl, cwd, path_max);
-    }
+    bool ret = true;

-    if (err)
+    cwd = getcwd(NULL, 0);
+    if (!cwd) {
         vshError(ctl, _("pwd: cannot get current directory: %s"),
                  strerror(errno));
-    else
+        ret = false;
+    } else {
         vshPrint (ctl, _("%s\n"), cwd);
+        VIR_FREE(cwd);
+    }

-    VIR_FREE(cwd);
-    return !err;
+    return ret;
 }
-#endif

 /*
  * "echo" command
@@ -10862,15 +10848,11 @@ static const vshCmdDef secretCmds[] = {
 };

 static const vshCmdDef virshCmds[] = {
-#ifndef WIN32
     {"cd", cmdCd, opts_cd, info_cd},
-#endif
     {"echo", cmdEcho, opts_echo, info_echo},
     {"exit", cmdQuit, NULL, info_quit},
     {"help", cmdHelp, opts_help, info_help},
-#ifndef WIN32
     {"pwd", cmdPwd, NULL, info_pwd},
-#endif
     {"quit", cmdQuit, NULL, info_quit},
     {NULL, NULL, NULL, NULL}
 };
-- 
1.7.4.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]