Ticket #2086: pathDetection.diff

File pathDetection.diff, 1.8 KB (added by starslayer, 9 years ago)

Patch for linux.cpp

  • source/lib/sysdep/os/linux/linux.cpp

     
    77 * distribute, sublicense, and/or sell copies of the Software, and to
    88 * permit persons to whom the Software is furnished to do so, subject to
    99 * the following conditions:
    10  * 
     10 *
    1111 * The above copyright notice and this permission notice shall be included
    1212 * in all copies or substantial portions of the Software.
    13  * 
     13 *
    1414 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    1515 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    1616 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     
    2727#define GNU_SOURCE
    2828#include "mocks/dlfcn.h"
    2929#include "mocks/unistd.h"
    30 
    3130#include <cstdio>
    3231
     32
     33static size_t getPathFromProc(char* buffer, size_t length)
     34{
     35    // Ask /proc/self/exe where we are
     36    if (readlink("/proc/self/exe", buffer, length-1) <= 0)
     37    {
     38        return -1;
     39    }
     40
     41    // Get the last slash
     42    char* endOfPath = strrchr(buffer, '/');
     43    if (endOfPath == NULL)
     44    {
     45        return -1;
     46    }
     47
     48    ++endOfPath;
     49    *endOfPath = '\0';
     50
     51    return (size_t)(endOfPath - buffer);
     52}
     53
    3354OsPath sys_ExecutablePathname()
    3455{
     56    // Try to find the Path with readlink
     57    char pathBuffer[PATH_MAX];
     58    if (getPathFromProc(pathBuffer, sizeof(pathBuffer)) > 0)
     59    {
     60        return pathBuffer;
     61    }
     62
    3563    // Find the executable's filename
    3664    Dl_info dl_info;
    3765    memset(&dl_info, 0, sizeof(dl_info));
     
    6694        return resolved;
    6795    }
    6896
    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.
    7297    return OsPath();
    7398}