Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 3 : //* 4 : //* All rights reserved, see COPYRIGHT for full restrictions 5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT 6 : //* 7 : //* Licensed under LGPL 2.1, please see LICENSE for details 8 : //* https://www.gnu.org/licenses/lgpl-2.1.html 9 : 10 : #include "Moose.h" 11 : #include "ExecutablePath.h" 12 : #include "MooseError.h" 13 : 14 : #ifdef __APPLE__ 15 : #include <mach-o/dyld.h> 16 : #endif 17 : 18 : #include <unistd.h> 19 : 20 : namespace Moose 21 : { 22 : 23 : std::string 24 136820 : getExec() 25 : { 26 136820 : std::string exec_path; 27 : char path[1024]; 28 : 29 : #if defined(__APPLE__) 30 : uint32_t size = sizeof(path); 31 : if (_NSGetExecutablePath(path, &size) == 0) 32 : exec_path = path; 33 : else 34 : mooseError("Unable to retrieve executable path"); 35 : #elif defined(__WIN32__) 36 : return "./"; 37 : #else // Linux with Proc 38 136820 : std::ostringstream oss; 39 136820 : oss << "/proc/" << getpid() << "/exe"; 40 136820 : int ch = readlink(oss.str().c_str(), path, 1024); 41 136820 : if (ch != -1) 42 : { 43 136820 : path[ch] = 0; 44 136820 : exec_path = path; 45 : } 46 : #endif 47 273640 : return exec_path; 48 136820 : } 49 : 50 : std::string 51 136799 : getExecutablePath() 52 : { 53 136799 : auto exec_path = getExec(); 54 : // strip off the exeuctable to get the PATH 55 136799 : std::string::size_type t = exec_path.find_last_of("/"); 56 410397 : return exec_path.substr(0, t) + "/"; 57 136799 : } 58 : 59 : std::string 60 21 : getExecutableName() 61 : { 62 21 : auto name = getExec(); 63 : // strip off the path to get the name 64 21 : std::string::size_type start = name.find_last_of("/"); 65 21 : if (start == std::string::npos) 66 0 : start = 0; 67 42 : return name.substr(start, std::string::npos); 68 21 : } 69 : 70 : } // Namespace MOOSE