build: fix getcwd portability problems

* 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.
This commit is contained in:
Eric Blake 2011-04-29 11:14:23 -06:00
parent 20986e58aa
commit e39c46a5fd
5 changed files with 20 additions and 36 deletions

View File

@ -36,6 +36,7 @@ dirname-lgpl
fcntl-h fcntl-h
func func
getaddrinfo getaddrinfo
getcwd-lgpl
gethostname gethostname
getpass getpass
gettext-h gettext-h

View File

@ -51,6 +51,7 @@ int main(int argc, char **argv) {
int i, n; int i, n;
char **origenv; char **origenv;
char **newenv; char **newenv;
char *cwd;
FILE *log = fopen(abs_builddir "/commandhelper.log", "w"); FILE *log = fopen(abs_builddir "/commandhelper.log", "w");
if (!log) if (!log)
@ -99,13 +100,13 @@ int main(int argc, char **argv) {
} }
fprintf(log, "DAEMON:%s\n", getpgrp() == getsid(0) ? "yes" : "no"); fprintf(log, "DAEMON:%s\n", getpgrp() == getsid(0) ? "yes" : "no");
char cwd[1024]; if (!(cwd = getcwd(NULL, 0)))
if (!getcwd(cwd, sizeof(cwd)))
return EXIT_FAILURE; return EXIT_FAILURE;
if (strlen(cwd) > strlen("/commanddata") && if (strlen(cwd) > strlen(".../commanddata") &&
STREQ(cwd + strlen(cwd) - strlen("/commanddata"), "/commanddata")) STREQ(cwd + strlen(cwd) - strlen("/commanddata"), "/commanddata"))
strcpy(cwd, ".../commanddata"); strcpy(cwd, ".../commanddata");
fprintf(log, "CWD:%s\n", cwd); fprintf(log, "CWD:%s\n", cwd);
VIR_FREE(cwd);
VIR_FORCE_FCLOSE(log); VIR_FORCE_FCLOSE(log);

View File

@ -49,15 +49,11 @@ mymain(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
static int checkoutput(const char *testname) static int checkoutput(const char *testname)
{ {
int ret = -1; int ret = -1;
char cwd[1024];
char *expectname = NULL; char *expectname = NULL;
char *expectlog = NULL; char *expectlog = NULL;
char *actualname = NULL; char *actualname = NULL;
char *actuallog = NULL; char *actuallog = NULL;
if (!getcwd(cwd, sizeof(cwd)))
return -1;
if (virAsprintf(&expectname, "%s/commanddata/%s.log", abs_srcdir, if (virAsprintf(&expectname, "%s/commanddata/%s.log", abs_srcdir,
testname) < 0) testname) < 0)
goto cleanup; goto cleanup;

View File

@ -478,7 +478,7 @@ int virtTestMain(int argc,
int (*func)(void)) int (*func)(void))
{ {
int ret; int ret;
char cwd[PATH_MAX]; bool abs_srcdir_cleanup = false;
#if TEST_OOM #if TEST_OOM
int approxAlloc = 0; int approxAlloc = 0;
int n; int n;
@ -490,8 +490,10 @@ int virtTestMain(int argc,
#endif #endif
abs_srcdir = getenv("abs_srcdir"); abs_srcdir = getenv("abs_srcdir");
if (!abs_srcdir) if (!abs_srcdir) {
abs_srcdir = getcwd(cwd, sizeof(cwd)); abs_srcdir = getcwd(NULL, 0);
abs_srcdir_cleanup = true;
}
if (!abs_srcdir) if (!abs_srcdir)
exit(EXIT_AM_HARDFAIL); exit(EXIT_AM_HARDFAIL);
@ -624,6 +626,8 @@ cleanup:
ret = (func)(); ret = (func)();
#endif #endif
if (abs_srcdir_cleanup)
VIR_FREE(abs_srcdir);
virResetLastError(); virResetLastError();
if (!virTestGetVerbose()) { if (!virTestGetVerbose()) {
int i; int i;

View File

@ -9893,7 +9893,6 @@ editReadBackFile (vshControl *ctl, const char *filename)
} }
#ifndef WIN32
/* /*
* "cd" command * "cd" command
*/ */
@ -9936,9 +9935,6 @@ cmdCd(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
return ret; return ret;
} }
#endif
#ifndef WIN32
/* /*
* "pwd" command * "pwd" command
*/ */
@ -9952,30 +9948,20 @@ static bool
cmdPwd(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED) cmdPwd(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
{ {
char *cwd; char *cwd;
size_t path_max; bool ret = true;
bool err = true;
path_max = (size_t) PATH_MAX + 2; cwd = getcwd(NULL, 0);
cwd = vshMalloc (ctl, path_max); if (!cwd) {
while (cwd) {
err = getcwd (cwd, path_max) == NULL;
if (!err || errno != ERANGE)
break;
path_max *= 2;
cwd = vshRealloc (ctl, cwd, path_max);
}
if (err)
vshError(ctl, _("pwd: cannot get current directory: %s"), vshError(ctl, _("pwd: cannot get current directory: %s"),
strerror(errno)); strerror(errno));
else ret = false;
} else {
vshPrint (ctl, _("%s\n"), cwd); vshPrint (ctl, _("%s\n"), cwd);
VIR_FREE(cwd);
}
VIR_FREE(cwd); return ret;
return !err;
} }
#endif
/* /*
* "echo" command * "echo" command
@ -10862,15 +10848,11 @@ static const vshCmdDef secretCmds[] = {
}; };
static const vshCmdDef virshCmds[] = { static const vshCmdDef virshCmds[] = {
#ifndef WIN32
{"cd", cmdCd, opts_cd, info_cd}, {"cd", cmdCd, opts_cd, info_cd},
#endif
{"echo", cmdEcho, opts_echo, info_echo}, {"echo", cmdEcho, opts_echo, info_echo},
{"exit", cmdQuit, NULL, info_quit}, {"exit", cmdQuit, NULL, info_quit},
{"help", cmdHelp, opts_help, info_help}, {"help", cmdHelp, opts_help, info_help},
#ifndef WIN32
{"pwd", cmdPwd, NULL, info_pwd}, {"pwd", cmdPwd, NULL, info_pwd},
#endif
{"quit", cmdQuit, NULL, info_quit}, {"quit", cmdQuit, NULL, info_quit},
{NULL, NULL, NULL, NULL} {NULL, NULL, NULL, NULL}
}; };