I've been tasked with creating a nested Unix shell that executes commands using the fork/exec/wait model. One of the instructions states that if the command doesn't exist (like "cd", then I need to print the attempted command. E.G. (If input is cd, output is "cd: command not found.")
But I cannot for the life of me figure out how to print the command that was input.
I declared an array, tried using scanf, tried converting from char ** to char *, or to int; none of my attempts to brute force it are working.
Here's the code:
int main() {
char **command;
char *input;
pid_t child_pid;
int stat_loc;
char arg[20];
while (1)
{
input = readline("minor2> ");
command = get_input(input);
if(!command[0])
{
free(input);
free(command);
continue;
}
else if(command[0]=="quit")
{
exit(1);
}
child_pid = fork();
if (child_pid == 0)
{
/*Never returns if call is a success*/
execvp(command[0], command);
printf("%s", arg);
printf(": command not found.\n");
}
else
{
waitpid(child_pid, &stat_loc, WUNTRACED);
}
free(input);
free(command);
}
char **get_input(char *input) {
char **command = malloc(8 * sizeof(char *));
char *separator = " ";
char *parsed;
int index = 0;
parsed = strtok(input, separator);
while (parsed != NULL) {
command[index] = parsed;
index++;
parsed = strtok(NULL, separator);
}
command[index] = NULL;
return command;
}
Some of the errors I've receieved: "assignment makes integer from pointer without a cast -wint-conversion"
"Cast to pointer of integer with different size"
"1value required as left operand of assignment"
I'm just confused. I'm sure there's an elegant solution, I just am not experienced enough to figure it out.