www.mooseframework.org
ExecutablePath.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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
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 
50 std::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 
59 std::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
std::string name(const ElemQuality q)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
std::string getExecutablePath()
This function returns the PATH of the running executable.
std::string getExec()
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
std::string getExecutableName()
This function returns the name of the running executable.