X-Git-Url: http://git.nguyen.vg/gitweb/?p=tatoo.git;a=blobdiff_plain;f=remake.cpp;h=23a44f8af37efb8b16defe82f5a4dc11d857aaa1;hp=5d8dc3d9f1b8c03898cd6f0a2eab922eb3b4aecf;hb=82fa70675272c579a642c6e62c8fbf28a1bba56c;hpb=8af68de11421dacc2f77a8398dcb48c75a5ff3b1 diff --git a/remake.cpp b/remake.cpp index 5d8dc3d..23a44f8 100644 --- a/remake.cpp +++ b/remake.cpp @@ -676,9 +676,31 @@ static std::string escape_string(std::string const &s) /** * Initialize #working_dir. */ -void init_working_dir() +void init_working_dir(const char* argv0) { char buf[1024]; +#ifdef WINDOWS + char const *delim = "/\\"; +#else + char delim = '/'; +#endif + if (!getenv("REMAKE_SOCKET")) + { + std::string path = argv0; + + size_t found = path.find_last_of(delim); + if (found != std::string::npos) + { + path = path.substr(0, found); + std::cout << "Entering directory `" << path << "'" << std::endl; + if (chdir(path.c_str())) + { + perror("Failed to change working directory"); + exit(EXIT_FAILURE); + } + } + } + char *res = getcwd(buf, sizeof(buf)); if (!res) { @@ -2230,7 +2252,7 @@ void usage(int exit_status) */ int main(int argc, char *argv[]) { - init_working_dir(); + init_working_dir(argv[0]); string_list targets; bool indirect_targets = false;