Skip to content

Commit 80fef6c

Browse files
author
ajsarmah
committed
The basic shell is good to go :)
1 parent 2693804 commit 80fef6c

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

headers/utils.h

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
#define _UTILS__H__
33

44
void parse_input_string(char* line, char** cmd,int max_args);
5+
int parse_built_in_cmd(char **cmd);
56

67
#endif

main.c

+9-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#define STD_OUT 1
1212
#define ARGS_MAX 20
1313

14-
int status = 1;
1514

1615
int main(int argc, char **argv)
1716
{
@@ -28,16 +27,19 @@ int main(int argc, char **argv)
2827

2928
printf("\033c");
3029
printf("Hello %s ;) \n",user);
31-
while(status)
30+
for(;;)
3231
{
33-
int i = 0;
34-
bzero(cmd_with_args,sizeof(cmd_with_args));
35-
print_status_line(user,SIZE);
32+
int i = 0;
33+
bzero(cmd_with_args,sizeof(cmd_with_args));
34+
print_status_line(user,SIZE);
3635
write(STD_OUT,"> ",2);
3736
read_user_input(line, STD_IN, SIZE);
3837
parse_input_string(line,cmd_with_args,ARGS_MAX);
39-
execute_command(cmd_with_args);
38+
int cmd_status = parse_built_in_cmd(cmd_with_args);
39+
if(cmd_status == 0)
40+
{
41+
execute_command(cmd_with_args);
42+
}
4043
}
41-
printf("\033c");
4244
return 0;
4345
}

utils.c

+32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include <string.h>
2+
#include <unistd.h>
3+
#include <stdlib.h>
24
#include <stdio.h>
35
#include "utils.h"
46

@@ -15,3 +17,33 @@ void parse_input_string(char* line, char** cmd, int max_args)
1517
}
1618
cmd[i + 1] = NULL;
1719
}
20+
21+
22+
int parse_built_in_cmd(char **cmd)
23+
{
24+
int no_of_cmd = 2, i, cmd_no = 0;
25+
char* cmd_list[no_of_cmd];
26+
cmd_list[0] = "exit";
27+
cmd_list[1] = "cd";
28+
for(i=0;i<no_of_cmd;i++)
29+
{
30+
if(strcmp(cmd[0],cmd_list[i]) == 0)
31+
{
32+
cmd_no = i + 1;
33+
break;
34+
}
35+
}
36+
switch(cmd_no)
37+
{
38+
case 1:
39+
printf("Exiting shell...");
40+
printf("\033c");
41+
exit(0);
42+
case 2:
43+
chdir(cmd[1]);
44+
return 1;
45+
default:
46+
break;
47+
}
48+
return 0;
49+
}

0 commit comments

Comments
 (0)