www.mooseframework.org
ExecutablePath.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "Moose.h"
16 #include "ExecutablePath.h"
17 #include "MooseError.h"
18 
19 #ifdef __APPLE__
20 #include <mach-o/dyld.h>
21 #endif
22 
23 #include <unistd.h>
24 
25 namespace Moose
26 {
27 
28 std::string
30 {
31  std::string exec_path;
32  char path[1024];
33 
34 #ifdef __APPLE__
35  uint32_t size = sizeof(path);
36  if (_NSGetExecutablePath(path, &size) == 0)
37  exec_path = path;
38  else
39  mooseError("Unable to retrieve executable path");
40 #else // Linux with Proc
41  std::ostringstream oss;
42  oss << "/proc/" << getpid() << "/exe";
43  int ch = readlink(oss.str().c_str(), path, 1024);
44  if (ch != -1)
45  {
46  path[ch] = 0;
47  exec_path = path;
48  }
49 #endif
50 
51  // Now strip off the exeuctable to get the PATH
52  std::string::size_type t = exec_path.find_last_of("/");
53  exec_path = exec_path.substr(0, t) + "/";
54 
55  return exec_path;
56 }
57 
58 } // Namespace MOOSE
std::string getExecutablePath()
This function returns the PATH of the running executable.
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
Definition: Moose.h:84