找到指向 argv[0] 的指针,以便我可以更改它 [英] Finding pointer to argv[0] so I can change it

查看:18
本文介绍了找到指向 argv[0] 的指针,以便我可以更改它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Solaris 上,我通过 getexecname 获得了一个指向 argv[0] 的指针,然后我可以在该位置 memcpy.(根据 在没有/proc/self/的情况下查找当前可执行文件的路径exe)

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)

我想知道如何在 Linux 中获得指向 argv[0] 的指针我在 /proc/self/exe 上做了 readlink 但是它没有给我一个指向它的指针.

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.

谢谢

推荐答案

对于 readlink,自带缓冲区.你分配一个缓冲区,传入一个指向它的指针,readlink 将把结果存储在那里:

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);
}

这篇关于找到指向 argv[0] 的指针,以便我可以更改它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆