Skip to content

Commit 19636aa

Browse files
committed
Added new features
1 parent db4ce31 commit 19636aa

35 files changed

+954
-57
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,21 @@ It's a basic shell written in C.
1010
6. main.c : Wraps all the components together and contains shell loop
1111
7. parse.c : Tokenizes the input string
1212
8. pinfo.c : Implements the `pinfo` function
13-
8. prompt.c : Prepares and asks for the prompt
13+
9. prompt.c : Prepares and asks for the prompt
14+
10. bg.c : Implements the `bg` function
15+
11. fg.c : Implements the `fg` function
16+
12. env.c : Implements the `setenv` and `unsetenv` function
17+
13. jobs.c : Implements the `jobs` function
18+
14. kjob.c : Implements the `kjob` function
19+
15. list.c : Contains the structs for Nodes and Processes
20+
16. overkill.c : Implements the `overkill` function
21+
17. sighandle.c : Contains code for signal handling
22+
23+
# Additional features
24+
1. Supports background processes
25+
2. Supports Ctrl + C and Ctrl + D functionality
1426

1527
# Steps to run the shell:
1628
1. Type `make`.
1729
2. Then type `./dash` to run the shell.
18-
3. Type `exit` or `quit` to exit the shell. You can also press Ctrl + C to quit the shell.
30+
3. Type `quit` to exit the shell. You can also press Ctrl + D to quit the shell.

bg.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "bg.h"
2+
#include "list.h"
3+
#include "main.h"
4+
#include "headers.h"
5+
6+
void bg(char ** parsed, int args)
7+
{
8+
if(args != 2)
9+
{
10+
printf("Usage: bg <job number>\n");
11+
}
12+
13+
else
14+
{
15+
pid_t bgPid;
16+
bgPid = get_Pid(procList, atoi(parsed[1]) - 1);
17+
18+
if(bgPid == -1)
19+
{
20+
printf("Error: No job with the given job number found\n");
21+
}
22+
23+
if(kill(bgPid, SIGCONT))
24+
{
25+
perror("bg");
26+
}
27+
}
28+
29+
}

bg.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef __BG_h
2+
#define __BG_h
3+
4+
void bg(char ** parsed, int args);
5+
6+
#endif // !__BG_h

cd.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#include "cd.h"
22
#include "prompt.h"
33
#include "headers.h"
4+
#include "cwd.h"
45

5-
void cd(char **path)
6+
void cd(char **path, char * prev, bool prevset)
67
{
78
if(path[1] == NULL)
89
{
@@ -22,6 +23,25 @@ void cd(char **path)
2223
}
2324
}
2425

26+
else if(!strcmp(path[1], "-"))
27+
{
28+
if(prevset)
29+
{
30+
if(chdir(prev) != 0)
31+
{
32+
perror("Error");
33+
}
34+
35+
pwd();
36+
}
37+
38+
else
39+
{
40+
printf("Error: No previous directory set yet\n");
41+
}
42+
43+
}
44+
2545
else
2646
{
2747
if(chdir(path[1]) != 0)

cd.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
#include "headers.h"
2+
13
#ifndef __CD_H
24
#define __CD_H
35

4-
void cd(char **path);
6+
void cd(char **path, char * prev, bool prevset);
57

68
#endif

cmdhandle.c

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,31 @@
55
#include "ls.h"
66
#include "pinfo.h"
77
#include "echo.h"
8+
#include "env.h"
9+
#include "jobs.h"
10+
#include "bg.h"
11+
#include "fg.h"
12+
#include "overkill.h"
13+
#include "kjob.h"
14+
#include "sighandle.h"
15+
#include "main.h"
816

9-
int cmdhandle(char **parsed)
17+
int cmdhandle(char **parsed, int args)
1018
{
11-
if(!strcmp(parsed[0], "quit"))
12-
{
13-
printf("You have exited the shell.\n");
14-
exit(0);
15-
return 1;
16-
}
17-
18-
else if(!strcmp(parsed[0], "pwd"))
19+
if(!strcmp(parsed[0], "pwd"))
1920
{
2021
pwd();
2122
return 0;
2223
}
2324

2425
else if(!strcmp(parsed[0], "cd"))
2526
{
26-
cd(parsed);
2727
return 0;
2828
}
2929

3030
else if(!strcmp(parsed[0], "echo"))
3131
{
32-
echo(parsed);
32+
echo(parsed, args);
3333
return 0;
3434
}
3535

@@ -45,6 +45,48 @@ int cmdhandle(char **parsed)
4545
return 0;
4646
}
4747

48+
else if(!strcmp(parsed[0], "setenv"))
49+
{
50+
setEnv(parsed, args);
51+
return 0;
52+
}
53+
54+
else if(!strcmp(parsed[0], "unsetenv"))
55+
{
56+
unsetEnv(parsed, args);
57+
return 0;
58+
}
59+
60+
else if(!strcmp(parsed[0], "jobs"))
61+
{
62+
jobs(args);
63+
return 0;
64+
}
65+
66+
else if(!strcmp(parsed[0], "kjob"))
67+
{
68+
kjob(parsed, args);
69+
return 0;
70+
}
71+
72+
else if(!strcmp(parsed[0], "overkill"))
73+
{
74+
overkill(args);
75+
return 0;
76+
}
77+
78+
else if(!strcmp(parsed[0], "fg"))
79+
{
80+
fg(parsed, args);
81+
return 0;
82+
}
83+
84+
else if(!strcmp(parsed[0], "bg"))
85+
{
86+
bg(parsed, args);
87+
return 0;
88+
}
89+
4890
else if(!strcmp(parsed[0], "clear"))
4991
{
5092
printf("\e[1;1H\e[2J");
@@ -53,6 +95,6 @@ int cmdhandle(char **parsed)
5395

5496
else
5597
{
56-
return 2;
98+
return 1;
5799
}
58100
}

cmdhandle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifndef __CMDHANDLE_H
22
#define __CMDHANDLE_H
33

4-
int cmdhandle(char **parsed);
4+
int cmdhandle(char **parsed, int args);
55

66
#endif

dash

-7.45 KB
Binary file not shown.

echo.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
#include "echo.h"
22
#include "headers.h"
3+
#include "env.h"
34

4-
void echo(char **parsed)
5-
{
6-
if(parsed[1] == NULL)
5+
void echo(char **parsed, int args)
6+
{
7+
char temp[1024];
8+
9+
if(args == 1)
710
{
811
printf("\n");
12+
return;
913
}
1014

1115
else
1216
{
13-
int i = 1;
17+
int i;
1418

15-
while(parsed[i] != NULL)
19+
for(i = 1; i < args; i++)
1620
{
17-
printf("%s ", parsed[i]);
18-
i++;
21+
// if(parsed[i][0] == '$')
22+
// {
23+
// strcpy(temp, &parsed[i][1]);
24+
// printf("%s ", temp);
25+
// printf("%s ", getenv(temp));
26+
// }
27+
28+
if(parsed[i])
29+
{
30+
printf("%s ", parsed[i]);
31+
}
1932
}
2033
printf("\n");
2134
}

echo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifndef __ECHO_H
22
#define __ECHO_H
33

4-
void echo(char **parsed);
4+
void echo(char **parsed, int args);
55

66
#endif

0 commit comments

Comments
 (0)