Chg: updates from whereami repo. I am sure I broke something now

This commit is contained in:
iceman1001 2020-04-14 09:24:08 +02:00
commit 7ee49df717

View file

@ -154,20 +154,28 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
} }
*/ */
#elif defined(__linux__) #elif defined(__linux__) || defined(__CYGWIN__) || defined(__sun) || defined(WAI_USE_PROC_SELF_EXE)
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#if defined(__linux__)
#include <linux/limits.h> #include <linux/limits.h>
#else
#include <limits.h>
#endif
#ifndef __STDC_FORMAT_MACROS #ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS #define __STDC_FORMAT_MACROS
#endif #endif
#include <inttypes.h> #include <inttypes.h>
#if !defined(WAI_PROC_SELF_EXE) #if !defined(WAI_PROC_SELF_EXE)
#if defined(__sun)
#define WAI_PROC_SELF_EXE "/proc/self/path/a.out"
#else
#define WAI_PROC_SELF_EXE "/proc/self/exe" #define WAI_PROC_SELF_EXE "/proc/self/exe"
#endif #endif
#endif
WAI_FUNCSPEC WAI_FUNCSPEC
int WAI_PREFIX(getExecutablePath)(char *out, int capacity, int *dirname_length) { int WAI_PREFIX(getExecutablePath)(char *out, int capacity, int *dirname_length) {
@ -207,8 +215,12 @@ int WAI_PREFIX(getExecutablePath)(char *out, int capacity, int *dirname_length)
#endif #endif
#if !defined(WAI_PROC_SELF_MAPS) #if !defined(WAI_PROC_SELF_MAPS)
#if defined(__sun)
#define WAI_PROC_SELF_MAPS "/proc/self/map"
#else
#define WAI_PROC_SELF_MAPS "/proc/self/maps" #define WAI_PROC_SELF_MAPS "/proc/self/maps"
#endif #endif
#endif
#if defined(__ANDROID__) || defined(ANDROID) #if defined(__ANDROID__) || defined(ANDROID)
#include <fcntl.h> #include <fcntl.h>
@ -260,7 +272,7 @@ int WAI_PREFIX(getModulePath)(char *out, int capacity, int *dirname_length) {
char *begin; char *begin;
char *p; char *p;
begin = (char *)mmap(0, offset, PROT_READ, MAP_SHARED, fd, 0); begin = (char*)mmap(0, offset + sizeof(p), PROT_READ, MAP_SHARED, fd, 0);
p = begin + offset; p = begin + offset;
while (p >= begin) { // scan backwards while (p >= begin) { // scan backwards
@ -276,7 +288,7 @@ int WAI_PREFIX(getModulePath)(char *out, int capacity, int *dirname_length) {
break; break;
} }
p -= 4; --p;
} }
munmap(begin, offset); munmap(begin, offset);
@ -302,11 +314,15 @@ int WAI_PREFIX(getModulePath)(char *out, int capacity, int *dirname_length) {
} }
fclose(maps); fclose(maps);
maps = NULL;
if (length != -1) if (length != -1)
break; break;
} }
if (maps)
fclose(maps);
return length; return length;
} }