https://mooseframework.inl.gov
Loading...
Searching...
No Matches
ExecutablePath.C
Go to the documentation of this file.
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
20namespace Moose
21{
22
23std::string
25{
26 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 std::ostringstream oss;
39 oss << "/proc/" << getpid() << "/exe";
40 int ch = readlink(oss.str().c_str(), path, 1024);
41 if (ch != -1)
42 {
43 path[ch] = 0;
44 exec_path = path;
45 }
46#endif
47 return exec_path;
48}
49
50std::string
52{
53 auto exec_path = getExec();
54 // strip off the exeuctable to get the PATH
55 std::string::size_type t = exec_path.find_last_of("/");
56 return exec_path.substr(0, t) + "/";
57}
58
59std::string
61{
62 auto name = getExec();
63 // strip off the path to get the name
64 std::string::size_type start = name.find_last_of("/");
65 if (start == std::string::npos)
66 start = 0;
67 return name.substr(start, std::string::npos);
68}
69
70} // Namespace MOOSE
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
std::string getExecutableName()
Gets the name of the running executable on Mac OS X and linux.
std::string getExec()
Gets the full path to the running executable on Mac OS X and linux.
std::string getExecutablePath()
Gets the directory the running executable is on Mac OS X and linux.