1 #include <msp/core/application.h>
2 #include <msp/core/environ.h>
3 #include <msp/strings/utils.h>
26 /** Helper function to determine the location of the program's executable.
27 Caches the last result to cut down filesystem access with repeated calls. */
28 const Path &get_bin_dir(const string &argv0)
30 static string last_argv0;
33 if(!(argv0==last_argv0))
36 if(argv0.find(DIRSEP)==string::npos)
38 string path = getenv("PATH");
41 for(const string &d: split(path, ITEMSEP))
42 if(exists(Path(d)/argv0))
44 exe = realpath(Path(d)/argv0);
51 exe = realpath(argv0);
54 bin_dir = dirname(exe);
63 not_a_directory::not_a_directory(const Path &p):
64 runtime_error(p.str())
68 void mkpath(const Path &path, int mode)
71 for(const string &c: path)
75 if(p.size()==1 && p.is_absolute())
78 if(FS::Stat st = stat(p))
80 if(!st.is_directory())
81 throw not_a_directory(p);
89 void rmpath(const Path &path)
91 for(const string &f: list_files(path))
103 vector<string> list_files(const Path &path)
105 return list_filtered(path, string());
108 Path get_sys_conf_dir()
110 const char *argv0 = Application::get_argv0();
112 throw logic_error("no startup command");
114 Path dir = get_bin_dir(argv0);
116 if(dir[-1]=="bin" || dir[-1]=="sbin")
127 Path get_sys_data_dir()
129 const char *argv0 = Application::get_argv0();
131 throw logic_error("no startup command");
133 Path dir = get_bin_dir(argv0);
135 if(dir[-1]=="bin" || dir[-1]=="sbin")
136 return dir/".."/"share"/Application::get_name();
137 else if(dir[-1]=="MacOS")
138 return dir/".."/"Resources";
143 Path get_sys_lib_dir()
145 const char *argv0 = Application::get_argv0();
147 throw logic_error("no startup command");
149 Path dir = get_bin_dir(argv0);
151 if(dir[-1]=="bin" || dir[-1]=="sbin")
152 return dir/".."/"lib"/Application::get_name();
157 Path path_lookup(const string &name, const vector<Path> &paths)
159 for(const Path &p: paths)
163 return realpath(full);
169 Path path_lookup(const string &name)
171 string path = getenv("PATH");
172 vector<string> dirs = split(path, ITEMSEP);
173 return path_lookup(name, vector<Path>(dirs.begin(), dirs.end()));