int i;
char *line = malloc(BUFFER);
char *origLine = line;
fgets(line, 128, stdin); // get a line from stdin
// get complete diagnostics on the given string
lineData info = runDiagnostics(line);
char command[20];
sscanf(line, "%20s ", command);
line = strchr(line, ' ');
printf("The Command is: %s\n", command);
int currentCount = 0; // number of elements in the line
int *argumentsCount = ¤tCount; // pointer to that
// get the elements separated
char** arguments = separateLineGetElements(line,argumentsCount);
// here we call a method that would execute the commands
pid_t pid = fork();
if (pid < 0)
printf("Could not fork!");
elseif (pid == 0) // fork was successful
{
if (execvp(*arguments,arguments) < 0) // execute the command
{
printf("ERROR: FORK failed\n");
exit(1);
}
}
else // parent
{
int status = 0;
pid = wait(&status);
printf("Process %d returned with status %d.", pid, status);
}
if (execvp(arguments[0],arguments) < 0) // execute the command
{
printf("ERROR: exec failed\n");
exit(1);
}
The fork() fails and also when I try to execute execvp(arguments[0],arguments) it also fails .
My question now is : when I want to execute a command (FYI , I'm building a shell for the bash command) , after I supply the name of the program that I want to run , with its arguments : if I need to write from a file , or write to a file , do I need to open it manually , or is it enough to give execvp() the arguments , and execvp() would find the file by itself ?