11#include <sys/types.h>
22#include <stdio.h>
33#include <unistd.h>
4+ #include <string.h>
45#include <stdlib.h>
56
7+ typedef int bool ;
8+
9+ #define true 1
10+ #define false 0
611#define LSH_RL_BUFSIZE 1024 /* buffer size for reading user input */
712#define LSH_TOK_BUFSIZE 64
813#define LSH_TOK_DELIM " \t\r\n\a"
914
15+ /* global variable to check parent and child process concurrency */
16+ bool conc = false;
17+
1018/* Function declarations for built-in shell commands */
1119int lsh_cd (char * * args );
1220int lsh_help (char * * args );
@@ -46,7 +54,7 @@ int lsh_cd(char **args)
4654int lsh_help (char * * args )
4755{
4856 int i ;
49- printf ("Aman Dalmia's LSH\n" , );
57+ printf ("Aman Dalmia's LSH\n" );
5058 printf ("Type program names and arguments, and press enter\n" );
5159 printf ("The following are built in:\n" );
5260
@@ -73,13 +81,16 @@ int lsh_launch(char **args){
7381 if (execvp (args [0 ], args ) == -1 ) perror ("lsh" );
7482 exit (EXIT_FAILURE );
7583 }else if (pid > 0 ){ /* parent process */
76- do {
77- wpid = waitpid (pid , & status , WUNTRACED );
78- }while (!WIFEXITED (status ) && !WIFSIGNALED (status ));
84+ if (!conc ){
85+ do {
86+ wpid = waitpid (pid , & status , WUNTRACED );
87+ }while (!WIFEXITED (status ) && !WIFSIGNALED (status ));
88+ }
7989 }else { /* error forking */
8090 perror ("lsh" );
8191 }
8292
93+ conc = false;
8394 return 1 ;
8495}
8596
@@ -102,7 +113,7 @@ int lsh_execute(char **args){
102113
103114/* Parse input to get the arguments */
104115char * * lsh_split_line (char * line ){
105- int bufsize = LSH_TOK_DELIM , position = 0 ;
116+ int bufsize = LSH_TOK_BUFSIZE , position = 0 ;
106117 char * * tokens = malloc (bufsize * sizeof (char * ));
107118 char * token ;
108119
@@ -127,6 +138,10 @@ char **lsh_split_line(char *line){
127138
128139 token = strtok (NULL , LSH_TOK_DELIM );
129140 }
141+ if (strcmp (tokens [position - 1 ], "&" ) == 0 ) {
142+ conc = true;
143+ tokens [position - 1 ] = NULL ;
144+ }
130145 tokens [position ] = NULL ;
131146 return tokens ;
132147}
@@ -146,7 +161,7 @@ char *lsh_read_line(void)
146161
147162 while (1 ){
148163 /* Read a character */
149- c = getchar ()
164+ c = getchar ();
150165
151166 if (c == EOF || c == '\n' ){
152167 buffer [position ] = '\0' ;
@@ -178,8 +193,8 @@ void lsh_loop(void)
178193 do {
179194 printf (">" );
180195 line = lsh_read_line ();
181- args = lsh_split_line ();
182- status = lsh_execute (line );
196+ args = lsh_split_line (line );
197+ status = lsh_execute (args );
183198
184199 free (line );
185200 free (args );
0 commit comments