LCOV - code coverage report
Current view: top level - source/lib/sysdep/os/unix - unix_executable_pathname.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 15 20 75.0 %
Date: 2023-01-19 00:18:29 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* Copyright (C) 2014 Wildfire Games.
       2             :  *
       3             :  * Permission is hereby granted, free of charge, to any person obtaining
       4             :  * a copy of this software and associated documentation files (the
       5             :  * "Software"), to deal in the Software without restriction, including
       6             :  * without limitation the rights to use, copy, modify, merge, publish,
       7             :  * distribute, sublicense, and/or sell copies of the Software, and to
       8             :  * permit persons to whom the Software is furnished to do so, subject to
       9             :  * the following conditions:
      10             :  *
      11             :  * The above copyright notice and this permission notice shall be included
      12             :  * in all copies or substantial portions of the Software.
      13             :  *
      14             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      15             :  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      16             :  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      17             :  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      18             :  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      19             :  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      20             :  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      21             :  */
      22             : 
      23             : #include "precompiled.h"
      24             : 
      25             : #include "lib/sysdep/sysdep.h"
      26             : 
      27             : #define GNU_SOURCE
      28             : #include "mocks/dlfcn.h"
      29             : #include "mocks/unistd.h"
      30             : 
      31             : #include <cstdio>
      32             : 
      33           6 : OsPath unix_ExecutablePathname()
      34             : {
      35             :     // Find the executable's filename
      36             :     Dl_info dl_info;
      37           6 :     memset(&dl_info, 0, sizeof(dl_info));
      38           6 :     if (!T::dladdr((void *)sys_ExecutablePathname, &dl_info) || !dl_info.dli_fname)
      39           0 :         return OsPath();
      40           6 :     const char* path = dl_info.dli_fname;
      41             : 
      42             :     // If this looks like an absolute path, use realpath to get the normalized
      43             :     // path (with no '.' or '..')
      44           6 :     if (path[0] == '/')
      45             :     {
      46             :         char resolved[PATH_MAX];
      47           2 :         if (!realpath(path, resolved))
      48           0 :             return OsPath();
      49           2 :         return resolved;
      50             :     }
      51             : 
      52             :     // If this looks like a relative path, resolve against cwd and use realpath
      53           4 :     if (strchr(path, '/'))
      54             :     {
      55             :         char cwd[PATH_MAX];
      56           3 :         if (!T::getcwd(cwd, PATH_MAX))
      57           0 :             return OsPath();
      58             : 
      59             :         char absolute[PATH_MAX];
      60           3 :         int ret = snprintf(absolute, PATH_MAX, "%s/%s", cwd, path);
      61           3 :         if (ret < 0 || ret >= PATH_MAX)
      62           0 :             return OsPath(); // path too long, or other error
      63             :         char resolved[PATH_MAX];
      64           3 :         if (!realpath(absolute, resolved))
      65           0 :             return OsPath();
      66           3 :         return resolved;
      67             :     }
      68             : 
      69             :     // If it's not a path at all, i.e. it's just a filename, we'd
      70             :     // probably have to search through PATH to find it.
      71             :     // That's complex and should be uncommon, so don't bother.
      72           1 :     return OsPath();
      73           3 : }

Generated by: LCOV version 1.13