Ok so I am trying to write a piece of code that when a user inputs say "A = 'ls'" instead of outputting the files from the directory it will store the output in 'A'... If you get what i mean.
SO say the directory /Desktop has stuff.txt and main.cpp and i call "A = 'ls'" A would have "stuff.txt main.cpp" in it which i can then print later on.
I understand you can't just say A = execve(blah blah);
So how can it be done.
Here's what i have
cmd1 has the variable name in it and cmd2 has the 'ls' or 'ps' or whatever.
void variable_cmd(char** cmd1, char** cmd2)
{
pid_t pid;
pid = fork();
int i;
if(pid == 0)
{
i = execvp(argv[0], argv);
if(i < 0)
{
cout << argv[0] << " command not found" << endl;
exit(1);
}
}
else
{
wait(NULL);
}
}
Thanks