summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
ce0b610)
Working directory or $PATH may be changed by the program, so it may not
be possible to recover the executable later. It's still not completely
reliable since the parent process can pass any value for argv[0] through
execve().
#include <typeinfo>
#include <signal.h>
#include <msp/debug/demangle.h>
#include <msp/debug/errorreporter.h>
#include <typeinfo>
#include <signal.h>
#include <msp/debug/demangle.h>
#include <msp/debug/errorreporter.h>
+#include <msp/fs/dir.h>
+#include <msp/fs/path.h>
#include <msp/fs/utils.h>
#include <msp/io/print.h>
#include "application.h"
#include <msp/fs/utils.h>
#include <msp/io/print.h>
#include "application.h"
if(argv0_)
throw logic_error("startup info already set");
if(argv0_)
throw logic_error("startup info already set");
+ static FS::Path exe;
+
+ bool has_slash = strchr(argv0, FS::DIRSEP);
+ if(!has_slash)
+ exe = FS::path_lookup(argv0);
+ if(exe.empty())
+ exe = FS::realpath(argv0);
+
+ argv0_ = exe.c_str();
throw system_error("chdir");
}
throw system_error("chdir");
}
+Path path_lookup(const string &name, const list<Path> &paths)
+{
+ for(list<Path>::const_iterator i=paths.begin(); i!=paths.end(); ++i)
+ {
+ Path full = *i/name;
+ if(exists(full))
+ return realpath(full);
+ }
+
+ return Path();
+}
+
+Path path_lookup(const string &name)
+{
+ const char *path = getenv("PATH");
+ vector<string> dirs = split(path, ITEMSEP);
+ return path_lookup(name, list<Path>(dirs.begin(), dirs.end()));
+}
+
} // namespace FS
} // namespace Msp
} // namespace FS
} // namespace Msp
/// Changes the current working directory
void chdir(const Path &);
/// Changes the current working directory
void chdir(const Path &);
+/** Looks for a file in a list of paths. Returns the absolute path to the
+first existing location, or an empty Path if the file is not found at all. */
+Path path_lookup(const std::string &, const std::list<Path> &);
+
+/** Looks for a file using the PATH environment variable. */
+Path path_lookup(const std::string &);
+
} // namespace FS
} // namespace Msp
} // namespace FS
} // namespace Msp