LCOV - code coverage report
Current view: top level - src/utils - ExecutablePath.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #32971 (54bef8) with base c6cf66 Lines: 21 22 95.5 %
Date: 2026-05-29 20:35:17 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          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      281933 : getExec()
      25             : {
      26      281933 :   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      281933 :   std::ostringstream oss;
      39      281933 :   oss << "/proc/" << getpid() << "/exe";
      40      281933 :   int ch = readlink(oss.str().c_str(), path, 1024);
      41      281933 :   if (ch != -1)
      42             :   {
      43      281933 :     path[ch] = 0;
      44      281933 :     exec_path = path;
      45             :   }
      46             : #endif
      47      563866 :   return exec_path;
      48      281933 : }
      49             : 
      50             : std::string
      51      138472 : getExecutablePath()
      52             : {
      53      138472 :   auto exec_path = getExec();
      54             :   // strip off the exeuctable to get the PATH
      55      138472 :   std::string::size_type t = exec_path.find_last_of("/");
      56      276944 :   return exec_path.substr(0, t) + "/";
      57      138472 : }
      58             : 
      59             : std::string
      60           9 : getExecutableName()
      61             : {
      62           9 :   auto name = getExec();
      63             :   // strip off the path to get the name
      64           9 :   std::string::size_type start = name.find_last_of("/");
      65           9 :   if (start == std::string::npos)
      66           0 :     start = 0;
      67          18 :   return name.substr(start, std::string::npos);
      68           9 : }
      69             : 
      70             : } // Namespace MOOSE

Generated by: LCOV version 1.14