Reputation: 65
So here's my issue. I have a list of commands inputted from the user that I need to use to pass into execvp()
. Here's an example with the ls
command.
char *cmdList = {"ls", "-l", "folder1/folder2"}
Now I need a way to modify that first entry so that is has the form "/bin/ls"
Is the only way to do it via the strcat command? Ideally I'd like to directly modify the ls
entry so I could pass cmdList
directly into execvp()
.
Upvotes: 1
Views: 60
Reputation: 94
This workes for me.
char *cmdList[]= {"ls", "-l", "folder1/folder2"};
cmdList[0]="/bin/1s"; printf("%s", *cmdList);
Upvotes: 0
Reputation: 121407
You don't need to specify fullpath to use execvp()
. So you don't need to worry about modifying the first argument. For example, if you have an array with arguments:
#include <unistd.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
char *cmdList[] = {"ls", "-l", "folder1/folder2", 0};
execvp(cmdList[0], cmdList);
}
execvp()
will search ls
in your PATH
and execute it.
Upvotes: 1