How do I set the command line arguments in a C program so that it's visible when users type “ps aux”?

孤街浪徒 提交于 2019-11-30 15:07:41

You had the right idea, but you don't change the pointers in argv[n], you must change the string pointed to by argv[0] itself:

#include <string.h>
#include <unistd.h>

int main(int argc,char **argv)
{
    size_t maxlen = strlen(argv[0]);

    memset(argv[0], 0, maxlen);
    strncat(argv[0], "Hi Mom!", maxlen);
    pause();

    return 0;
}

(Note that whether or not this actually changes the command name shown by ps is system-dependent).

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