The following is a project description of a project that I have to complete:
Write a shell-like program that illustrates how UNIX spawns processes. This program will provide its own prompt to the user, read the command from the input and execute the command. Let the shell allow arguments to the commands. For example, it should be able to execute commands such as more filename and ls –l etc. (MyShell.c -> MyShell)
#include <sys/types.h>
#include <unistd.h>
#include <iostream>
#include <wait.h>
#include <fstream>
int main(){
char cmd[100], command_array[20];
int flag = 0;
int pid = fork();
while(flag !=1){
if (pid < 0) { // Create child process
perror("Fork call.");
return (2);
}
if (pid == 0) {
printf("myShell > ");
scanf("%s", cmd);
strncat(command_array, cmd, 100); //error on this line
if (execvp(command_array[0],command_array) == -1){
printf("Error: running command: '%s'\n", cmd);
//exit(-1);
flag = 0;
}
exit(0);
flag = 1;
}
if (pid > 0) {
wait((int *) 0);
}
}
return (0);
}
I put together this piece of code from the TAs lecture and from various examples from websites.
My first question:
Since there isn't a boolean in type in C, I declared a int flag = 0 and until the value of the flag changes to 1it need to prompt the user. Basically if the user enters a wrong command, error message appears and prompts the user again for an input. This isn't working.
My second question:
What am I doing wrong? I am following my TA instructions.
I'm actually going to see him tomorrow during office hours but it would be great if I could finish it before as I have a packed day tomorrow.
Thanks
drjay