I am trying to create a simple c program that takes user input, passes it to underlying shell and return the ouput of the user command. For eg: pwd will give the present working directory.
I want to do this using fork() and exec() in an infinite loop but I am facing two problems:
- My loop terminates after first run
It only takes first argument. 'ls -ltr' will give me the output of 'ls' and not 'ls -ltr'
int runit(char*); void main() { int pid=0; char command[50]; while(1) { int d=0; printf("Please enter your command!\n"); scanf("%s", &command); switch (pid = fork()) { case 0: // a fork returns 0 to the child printf("Child process \n"); d=runit(command); if(d==-1){ printf("command not found \n");} break; default: wait(5); // a fork returns a pid to the parent printf("Parent process \n"); break; case -1: //if something went wrong perror("fork"); exit(1); } } } int runit(char* command) { //executing the command char path[50]="/bin/"; int d = execl(strcat(path,command),command,NULL,NULL); return(d); }
Can somebody tell me what I am doing wrong or guide me how to correct this.