How to use execvp()

前端 未结 1 1848
太阳男子
太阳男子 2020-12-01 11:20

The user will read a line and i will retain the first word as a command for execvp.

Lets say he will type \"cat file.txt\" ... command wil

相关标签:
1条回答
  • 2020-12-01 11:47

    The first argument is the file you wish to execute, and the second argument is an array of null-terminated strings that represent the appropriate arguments to the file as specified in the man page.

    For example:

    char *cmd = "ls";
    char *argv[3];
    argv[0] = "ls";
    argv[1] = "-la";
    argv[2] = NULL;
    
    execvp(cmd, argv); //This will run "ls -la" as if it were a command
    
    0 讨论(0)
提交回复
热议问题