I use an std::vector<string> as a container for my command and arguments. And I need to manipulate it somehow in order to use execv.
My problem is that execv receives an array of strings (char**) and not an std::vector<string> like the one I use.
The simple option is just to make a new array and copy the string to it.
I want to do it without allocating new memory.
Is it somehow possible?
The data isn't formatted in the way that execv wants it. So you have to create a new area of memory that is formatted the way execv wants it, assign it properly, and pass it to execv.
I wouldn't use std::vector<string> if you're going to use execv directly since it obviously isn't very efficient in this situation.
According to it, the code should work. The guy there had the exact problem I have, and the solution offered to him there solved it. I don't know why it doesn't work for me.
As I understand it, the problem with this code is not the execv command, it's how it uses the data I give. For example, if I try running this with the command: "ls -l | head -3", it says "Unknown option: -l". If I'm just running ls (for example) it works.
You got a clue?
BTW, This is an assignment I got from school, and I'm using the csh because I was told to.
This code has multiple problems, for example:
arg_v can only hold char *, but "/bin/csh", "-f", and "-c" are const char *.
You are casting a const char * to a char * - NEVER cast away from const!
It didn't work for me. Maybe I didn't explain enough:
Maybe I didn't explain enough:
I'm creating a small shell, and I need to receive commands from the user. I have a list of commands to which I create the response, and for the other commands I want to use the csh to execute them.
For example, I implemented the cd, pwd, set etc. commands, and I want to use the csh program to execute the others (which are more complex).
The code a I put there was actually the function I call when I identify the command as complex.
If I use the command "ls -l | head -3", the argv will be:
argv = {"ls", "-l", "|", "head", "-3"}
(But of course as a vector of strings and not an array).
You can't just exec "ls -l | head -3". That pipe indicator is handled by the shell, by you in this case. It means you execute "ls -3" and "head -3" as different processes and tie stdout of ls to stdin of head.
The example I posted does work, I tested it on OS X.