summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPino Toscano <toscano.pino@tiscali.it>2013-05-21 10:23:22 (GMT)
committerPino Toscano <toscano.pino@tiscali.it>2013-05-21 10:23:22 (GMT)
commite92098f312eaecd4c89cd1d2f7d6445732950ab3 (patch)
tree1e27fd731f92a157f557a602d64740dbf4ca76d7
parent5a2b257611cd2ca6c15b3b495890f15198c6c41a (diff)
downloadNinja-e92098f312eaecd4c89cd1d2f7d6445732950ab3.zip
Ninja-e92098f312eaecd4c89cd1d2f7d6445732950ab3.tar.gz
Ninja-e92098f312eaecd4c89cd1d2f7d6445732950ab3.tar.bz2
do not unconditionally use PATH_MAX with getcwd
Instead, grow a buffer until getcwd either succeeds or fails with an errno different than ERANGE (meaning the passed buffer was too short to represent the actual path). Reset errno before calling getcwd to check it eventually did not fail.
-rw-r--r--src/ninja.cc10
1 files changed, 7 insertions, 3 deletions
diff --git a/src/ninja.cc b/src/ninja.cc
index b4797ed..4f77210 100644
--- a/src/ninja.cc
+++ b/src/ninja.cc
@@ -495,9 +495,13 @@ void EncodeJSONString(const char *str) {
int ToolCompilationDatabase(Globals* globals, int argc, char* argv[]) {
bool first = true;
- char cwd[PATH_MAX];
+ vector<char> cwd;
- if (!getcwd(cwd, PATH_MAX)) {
+ do {
+ cwd.resize(cwd.size() + 1024);
+ errno = 0;
+ } while (!getcwd(&cwd[0], cwd.size()) && errno == ERANGE);
+ if (errno != 0 && errno != ERANGE) {
Error("cannot determine working directory: %s", strerror(errno));
return 1;
}
@@ -511,7 +515,7 @@ int ToolCompilationDatabase(Globals* globals, int argc, char* argv[]) {
putchar(',');
printf("\n {\n \"directory\": \"");
- EncodeJSONString(cwd);
+ EncodeJSONString(&cwd[0]);
printf("\",\n \"command\": \"");
EncodeJSONString((*e)->EvaluateCommand().c_str());
printf("\",\n \"file\": \"");