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

前端 未结 1 539
时光取名叫无心
时光取名叫无心 2021-01-03 02:50

When you type \"ps aux\" the ps command shows command arguments that the program was run with. Some programs change this as a way of indicating status. I\'ve tried changing

1条回答
  •  北海茫月
    2021-01-03 03:05

    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 
    #include 
    
    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).

    0 讨论(0)
提交回复
热议问题