MCPcopy Create free account
hub / github.com/NatronGitHub/Natron / applicationFilePath_fromArgv

Function applicationFilePath_fromArgv

Global/ProcInfo.cpp:195–252  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

193
194#if defined( __NATRON_UNIX__ )
195static std::string
196applicationFilePath_fromArgv(const char* argv0Param)
197{
198 std::string argv0;
199 if (argv0Param) {
200 argv0 = std::string(argv0Param);
201 }
202 std::string absPath;
203
204 if ( !argv0.empty() && ( argv0[0] == '/' ) ) {
205 /*
206 If argv0 starts with a slash, it is already an absolute
207 file path.
208 */
209 absPath = argv0;
210 } else if ( argv0.find_first_of("/") != std::string::npos ) {
211 /*
212 If argv0 contains one or more slashes, it is a file path
213 relative to the current directory.
214 */
215 char* cwd = rpl_getcwd(NULL, 0);
216 absPath = cwd;
217 std::free(cwd);
218 absPath.append(argv0);
219 } else {
220 /*
221 Otherwise, the file path has to be determined using the
222 PATH environment variable.
223 */
224 std::string pEnv = getenv("PATH");
225 char* cwd = rpl_getcwd(NULL, 0);
226 std::string currentDirPath = cwd;
227 std::free(cwd);
228 std::vector<std::string> paths = StrUtils::split(pEnv, ':');
229 for (std::vector<std::string>::const_iterator it = paths.begin(); it != paths.end(); ++it) {
230 if (it->empty()) {
231 continue;
232 }
233 std::string candidate = currentDirPath;
234 candidate.append(*it + '/' + argv0);
235
236 struct stat _s;
237 if (stat(candidate.c_str(), &_s) == -1) {
238 continue;
239 }
240 if ( S_ISDIR(_s.st_mode) ) {
241 continue;
242 }
243
244 absPath = candidate;
245 break;
246 }
247 }
248
249 absPath = StrUtils::cleanPath(absPath);
250
251 return absPath;
252} // applicationFilePath_fromArgv

Callers 1

applicationFilePathMethod · 0.85

Calls 8

rpl_getcwdFunction · 0.85
cleanPathFunction · 0.85
emptyMethod · 0.80
appendMethod · 0.80
splitFunction · 0.70
statClass · 0.70
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected