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 147934 : getExec() 25 : { 26 147934 : 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 147934 : std::ostringstream oss; 39 147934 : oss << "/proc/" << getpid() << "/exe"; 40 147934 : int ch = readlink(oss.str().c_str(), path, 1024); 41 147934 : if (ch != -1) 42 : { 43 147934 : path[ch] = 0; 44 147934 : exec_path = path; 45 : } 46 : #endif 47 295868 : return exec_path; 48 147934 : } 49 : 50 : std::string 51 147913 : getExecutablePath() 52 : { 53 147913 : auto exec_path = getExec(); 54 : // strip off the exeuctable to get the PATH 55 147913 : std::string::size_type t = exec_path.find_last_of("/"); 56 443739 : return exec_path.substr(0, t) + "/"; 57 147913 : } 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