Ticket #2086: pathDetection.diff
File pathDetection.diff, 1.8 KB (added by , 9 years ago) |
---|
-
source/lib/sysdep/os/linux/linux.cpp
7 7 * distribute, sublicense, and/or sell copies of the Software, and to 8 8 * permit persons to whom the Software is furnished to do so, subject to 9 9 * the following conditions: 10 * 10 * 11 11 * The above copyright notice and this permission notice shall be included 12 12 * in all copies or substantial portions of the Software. 13 * 13 * 14 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. … … 27 27 #define GNU_SOURCE 28 28 #include "mocks/dlfcn.h" 29 29 #include "mocks/unistd.h" 30 31 30 #include <cstdio> 32 31 32 33 static 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 33 54 OsPath sys_ExecutablePathname() 34 55 { 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 35 63 // Find the executable's filename 36 64 Dl_info dl_info; 37 65 memset(&dl_info, 0, sizeof(dl_info)); … … 66 94 return resolved; 67 95 } 68 96 69 // If it's not a path at all, i.e. it's just a filename, we'd70 // probably have to search through PATH to find it.71 // That's complex and should be uncommon, so don't bother.72 97 return OsPath(); 73 98 }