I've found a way to call unix external commands without arguments(ex. "ls", "pwd"). It goes like that:
//Child process
char cwd[1024];
getcwd(cwd, sizeof(cwd));
char *argv[] = {*args, NULL}//(ex.) {"ls", NULL}
char *env[] = {cwd, NULL};
//concat():method that connects 2 strings
char *command_source = concat("/bin/", *args);
execve(command_source, argv, env);
return 0;
I'm trying to convert this code in order to accept external commands with arguments like "ls -l"
You can also make a pipeline; look at how the commands are built up and see the structure, they are arrays that end with 0 and the quotes are stripped:
Here are some utility function when you create a pipeline with arguments. They are well tested and bug-free.
pipeline.c
The code comes from a prior shell project from an earlier question where a bounty was awarded for the above code. There is an extensive answer already with details — How to fix these errors in my code. There's also the earlier answer to C minishell: adding pipelines which contains most of the quoted code.
You can also read pipe(2), pipe(7) and Advanced Linux Programming
Assuming that you know the number of arguments in
args
and that it'sargcs
:If not, you can easily determine
argcs
by iterating through the array searching for the ending NULL.