+#include <cstring>
#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"
if(argv0_)
throw logic_error("startup info already set");
- argv0_ = argv0;
+ 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();
data_ = data;
}
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
/// 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