LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   build a shell (https://www.linuxquestions.org/questions/programming-9/build-a-shell-261223/)

ej25 12-01-2004 03:37 AM

build a shell
 
Code:


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

 

main (int argc, char *argv[] ){

        int i;
        int status;
        int c=1;
    int  forkresult ;
 
    forkresult = fork ( ) ;

    if (forkresult != 0)

            wait(&status); 
   

      else   
                         
            execvp (argv[1],&argv[1] );

             
      printf ("EXEC Failed\n") ; 
           
        exit(1);

}

I wrote this code which take as argment a command from the user and exexuit it

how can I make it loop for infinity and take each time a coomand from the user and how can I make it wait untiul the programme exits befor it print the promot again to recive the second coomand?

bigearsbilly 12-01-2004 03:49 AM

shouldn't it be:
Code:

wait (forkresult)
status is a non initialised variable.

billy

jlliagre 12-01-2004 04:43 AM

Quote:

shouldn't it be:
code:
wait (forkresult)
status is a non initialised variable.
wait(&status) is right, no need to initialize status as the call purpose is precisely to affect it.

Quote:

how can I make it loop for infinity and take each time a coomand from the user
Something like:
Code:

while(1)
{
printf(prompt);
command=fgets(buffer,SIZE,stdin);
...
fork stuff
...
}

Quote:

and how can I make it wait untiul the programme exits befor it print the promot again to recive the second coomand?
the wait call should be enough

DaZjorz 12-02-2004 07:01 AM

Cool, make your own shell... Heh :) Is the bash-shell a big program? I mean, if the shell that this guy made is as good as that one then it would be nice heh :) cuz that makes it very small and easy-to-compile :)

Are you going to build in some build-in programs?


All times are GMT -5. The time now is 07:00 PM.