X-Git-Url: http://git.nguyen.vg/gitweb/?p=tatoo.git;a=blobdiff_plain;f=remake.cpp;h=30440a8a97198961bfc2095d02bdf5dc9b8eb52b;hp=b8387be2b0230e44382f322657cdf4875c00df94;hb=30675098fce9aaa56027adc42042ea70c986fe01;hpb=5bdd670738e0af306581819ef41659f961a0df12 diff --git a/remake.cpp b/remake.cpp index b8387be..30440a8 100644 --- a/remake.cpp +++ b/remake.cpp @@ -724,6 +724,12 @@ static std::string prefix_dir; */ static bool propagate_vars = false; + +/** + * Whether to print that we changed working directory (to be nice with emacs) + */ +static bool print_change_working_dir = false; + #ifndef WINDOWS static volatile sig_atomic_t got_SIGCHLD = 0; @@ -865,13 +871,28 @@ static void init_working_dir() */ static void init_prefix_dir() { + std::string old_prefix_dir = prefix_dir; for (;;) { struct stat s; if (stat((prefix_dir + "/Remakefile").c_str(), &s) == 0) { - chdir(prefix_dir.c_str()); - return; + if (0 == chdir(prefix_dir.c_str())) + { + if (old_prefix_dir != prefix_dir) + { + std::cout << "remake: Entering directory `" + << prefix_dir << "'" + << std::endl; + print_change_working_dir = true; + } + return; + } + else + { + std::cerr << "Cannot change working directory to '" << prefix_dir << "'"; + exit(EXIT_FAILURE); + } } size_t pos = prefix_dir.find_last_of('/'); if (pos == std::string::npos) @@ -2539,7 +2560,28 @@ static void create_server() if (sigaction(SIGINT, &sa, NULL) == -1) goto error; // Prepare a named unix socket in temporary directory. - socket_name = tempnam(NULL, "rmk-"); + struct stat tmpstat; + char * tmpdir; + if ((0 == stat(tmpdir = getenv("TMPDIR"), &tmpstat) + && (S_ISDIR(tmpstat.st_mode))) + || ((0 == stat(tmpdir = (char*)P_tmpdir, &tmpstat)) + && (S_ISDIR(tmpstat.st_mode))) + || ((0 == stat(tmpdir = (char*)"/tmp", &tmpstat)) + && (S_ISDIR(tmpstat.st_mode)))); + else goto error; + std::stringstream tmpname; + long int rnd = now ^ getpid(); + do + { + srandom(rnd); + rnd = random(); + tmpname << tmpdir << "/rmk-"; + tmpname.fill('0'); + tmpname.width(8); + tmpname << rnd; + } + while (access (tmpname.str().c_str(), F_OK) == 0); + socket_name = strdup(tmpname.str().c_str()); if (!socket_name) goto error2; struct sockaddr_un socket_addr; size_t len = strlen(socket_name); @@ -2792,6 +2834,9 @@ static void server_mode(std::string const &remakefile, string_list const &target free(socket_name); #endif save_dependencies(); + if (print_change_working_dir) + std::cout << "remake: Leaving directory `" << prefix_dir + << "'" <