问题
On Solaris I got a pointer to argv[0]
with getexecname
and then I can memcpy
at that location. (per Finding current executable's path without /proc/self/exe)
I was wondering how to get a pointer to argv[0]
in Linux I did readlink
on /proc/self/exe
but it doesn't give me a pointer to it.
THanks
回答1:
&argv[0]
gets you a pointer to argv[0]
.
You can overwrite the characters stored in the array that argv[0]
is pointing at, so long as you don't go past the existing null terminator; however it might cause UB to try and modify the pointer argv[0]
.
回答2:
For readlink
, Bring Your Own Buffer. You allocate a buffer, pass in a pointer to it, and readlink
will store the results there:
#include <unistd.h>
#include <linux/limits.h>
int main() {
char buffer[PATH_MAX];
int size = readlink("/proc/self/exe", buffer, sizeof(buffer));
buffer[size] = '\0';
// buffer is now the char* holding the filename
printf("The executable is %s\n", buffer);
}
来源:https://stackoverflow.com/questions/29158026/finding-pointer-to-argv0-so-i-can-change-it