0

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

Community
  • 1
  • 1
Noitidart
  • 35,443
  • 37
  • 154
  • 323

2 Answers2

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].

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
1

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);
}
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • Thanks other guy! So after I do readlink, i can maniuplate that buffer, and that will make the change on the application? – Noitidart Mar 20 '15 at 03:25
  • 1
    @Noitidart No, readlink on `/proc/self/exe` is if you want to find the path to the executable. The buffer is yours and has no effect on anything else. If your question is "how do I change the executable name `top` shows for a running Python process", please ask about that instead. – that other guy Mar 20 '15 at 03:30
  • Thanks other guy ill post that right now. edit: http://www.stackoverflow.com/questions/29159130/get-pointer-to-argv0-so-i-can-change-it – Noitidart Mar 20 '15 at 03:51