Finding pointer to argv[0] so I can change it

有些话、适合烂在心里 提交于 2020-02-08 10:35:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!