0% found this document useful (0 votes)
135 views

Assignment Toyshell

Prafulla saxena completed an assignment for building a toy shell. The code implements a basic shell that can execute commands like date, ls -l, cat, pwd, cd, and ps. The shell uses fork to create child processes that execute the commands. Output is printed to show the child running and results of commands. Testing showed the shell works as expected for various commands and changing directories.

Uploaded by

Prafulla Saxena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views

Assignment Toyshell

Prafulla saxena completed an assignment for building a toy shell. The code implements a basic shell that can execute commands like date, ls -l, cat, pwd, cd, and ps. The shell uses fork to create child processes that execute the commands. Output is printed to show the child running and results of commands. Testing showed the shell works as expected for various commands and changing directories.

Uploaded by

Prafulla Saxena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Information Security lab Assignment 4

Prafulla saxena(2016pis5249)
MNIT
23/09/2016

Problem 1

Building a toy shell

1.1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

Code

# include
# include
# include
# include
# include

< stdlib .h >


< stdio .h >
< string .h >
< unistd .h >
< errno .h >

void parseCmd ( char * cmd , char ** params ) ;


int executeCmd ( char ** params ) ;
# define M AX_ CO MM AN D_ LE NG TH 100
# define M A X _ N U M B E R _ O F _ P A R A M S 20
// int pipes [20]={0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0};
int main ()
{
char cmd [ MA X_C OM MA ND _L EN GT H + 1];
char * params [ M A X _ N U M B E R _ O F _ P A R A M S + 1];
int cmdCount = 0;
while (1) {
// Print command prompt
char * username = getenv ( " USER " ) ;
printf ( " CSP - SDL -% s / Shell$ " , username ) ;
// Read command from standard input , exit on Ctrl + D
if ( fgets ( cmd , sizeof ( cmd ) , stdin ) == NULL ) break ;
// Remove trailing newline character , if any
if ( cmd [ strlen ( cmd ) -1] == \ n ) {
cmd [ strlen ( cmd ) -1] = \0 ;
}
// Split cmd into array of parameters

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

parseCmd ( cmd , params ) ;


// Exit ?
if ( strcmp ( params [0] , " exit " ) == 0) break ;
// cd for

changing Directory -- do pwd for current directory

if ( strcmp ( params [0] , " cd " ) == 0) {


int k = chdir ( params [1]) ;
if ( k ==0) {
printf ( " directory Changed \ n " ) ;
}
else {
printf ( " no Such Directory \ n " ) ;
}
}
else {
// Execute command
if ( executeCmd ( params ) == 0) break ;
}
}
return 0;
}
// Split cmd into array of parameters like cd / home then params [0]= cd and params [1]=/ home
void parseCmd ( char * cmd , char ** params )
{
for ( int i = 0; i < M A X _ N U M B E R _ O F _ P A R A M S ; i ++) {
params [ i ] = strsep (& cmd , " " ) ;
if ( params [ i ] == NULL ) break ;
// printf ( " % d " , pipes [ i ]) ;
}
// for printing commands and its parameters
// printf ( " \n - - - Your commands with parameters - - -\ n " ) ;
// for ( int i = 0; i < M A X _ N U M B E R _ O F _ P A R A M S ; i ++) {
// if ( params [ i ] == NULL ) break ;
// else
// printf ( " \ n % d .% s \ n " ,i , params [ i ]) ;
//}

}
int executeCmd ( char ** params )
{
// creating Fork process
pid_t pid = fork () ;
// fork error if pid is -1
if ( pid == -1) {

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

char * error = strerror ( errno ) ;


printf ( " fork : % s \ n " , error ) ;
return 1;
}
// Child process
else if ( pid == 0) {
printf ( " child Running ....\ n " ) ;
// Execute command
sleep (2) ;
printf ( " --- Results of commands - - -\ n " ) ;
int kk = execvp ( params [0] , params ) ;
printf ( " \ n % d \ nthis will print on failure \ n " , kk ) ;
// Error occurred
char * error = strerror ( errno ) ;
printf ( " shell : % s : % s \ n " , params [0] , error ) ;
return 0;
}
// Parent process waiting for child
else {
// Wait for child process to finish
int childStatus ;
waitpid ( pid , & childStatus , 0) ;
printf ( " Child exited \ n " ) ;
return 1;
}
}

1.2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Result/Observation

bash$ ./ toyshell
CSP - SDL - prafulla / Shell$

/ - - shell prompt

CSP - SDL - prafulla / Shell$ date


child Running ....
--- Results of commands - - Sun Sep 25 00:08:19 IST 2016
Child exited

/ - - date command

CSP - SDL - prafulla / Shell$ cta


child Running ....
--- Results of commands - - -1
this will print on failure
shell : cta : No such file or directory
Child exited

/ - - cta command ( wrong command )

CSP - SDL - prafulla / Shell$ ls -l


child Running ....
--- Results of commands - - total 3148

/ - - ls -l command

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

-rw -r - -r - - 1
- rwxrwxr - x 1
-rw - rw -r - - 1
-rw - rw -r - - 1
-rw - rw -r - - 1
drwxr - xr - x 2
-rw - rw -r - - 1
-rw - rw -r - - 1
-rw -r - -r - - 1
-rw - rw -r - - 1
-rw -r - -r - - 1
-rw - rw -r - - 1
-rw -r - -r - - 1
-rw - rw -r - - 1
-rw - rw -r - - 1
drwxrwxr - x 4
-rw -r - -r - - 1
-rw - rw -r - - 1
- rwxrwxr - x 1
-rw - rw -r - - 1
-rw - rw -r - - 1
-rw - rw -r - - 1
-rw - rw -r - - 1
-rw - rw -r - - 1
-rw - rw -r - - 1
-rw - rw -r - - 1
- rwxrwxr - x 1
-rw - rw -r - - 1
Child exited

prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla
prafulla

prafulla
580 Sep 8 18:43 2 d PCA
prafulla
13552 Sep 24 23:59 a . out
prafulla
94067 Sep 24 19:34 assignment_3_ ( proc ) . pdf
prafulla
0 Sep 24 14:24 cat
prafulla 1394065 Sep 24 10:11 Chapter11 . pdf
prafulla
4096 Sep 19 22:26 Crypto @ssig nment2
prafulla
80 Sep 24 19:28 data . c
prafulla
404 Sep 23 13:13 func . c
prafulla
1432 Aug 26 12:11 galois2 . py
prafulla 1586170 Sep 24 10:11 introduction . pdf
prafulla
1131 Sep 8 17:40 matrix3D . m
prafulla
28 Sep 24 15:44 mem . c
prafulla
320 Sep 8 18:37 ml . m
prafulla
1762 Sep 19 22:37 multiplicative . py
prafulla
1216 Aug 26 11:48 multi . py
prafulla
4096 Sep 25 00:02 new
prafulla
602 Sep 8 17:09 pca
prafulla
2726 Sep 24 14:07 ppp . c
prafulla
12968 Sep 24 19:25 proc_parse
prafulla
1920 Sep 24 19:23 proc_parse . c
prafulla
490 Sep 24 19:36 README PROC
prafulla
243 Sep 23 22:13 shell . c
prafulla
1652 Sep 23 23:38 shellnew . c
prafulla
1652 Sep 23 23:38 sss . c
prafulla
812 Aug 10 00:02 test . c
prafulla
2691 Sep 25 00:02 toy . c
prafulla
13552 Sep 25 00:03 toyshell
prafulla
1988 Sep 24 20:08 toyy . c

CSP - SDL - prafulla / Shell$ cat data . c


/ - - cat command
child Running ....
--- Results of commands - - This will print from this file data . c using cat command
...
...
end file
Child exited
CSP - SDL - prafulla / Shell$ pwd
child Running ....
--- Results of commands - - / home / prafulla / Desktop
Child exited

/ - - pwd command

/ - - cd .. command ( to -

CSP - SDL - prafulla / Shell$ cd ..


parent directory )
directory Changed
CSP - SDL - prafulla / Shell$ pwd
child Running ....
--- Results of commands - - / home / prafulla
Child exited

/ - - pwd for current directory

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

/ - - cd to child -

CSP - SDL - prafulla / Shell$ cd Documents


directory
directory Changed
CSP - SDL - prafulla / Shell$ pwd
child Running ....
--- Results of commands - - / home / prafulla / Documents
Child exited

/ - - ps command for process -

CSP - SDL - prafulla / Shell$ ps


detail toyshell pid =27727
child Running ....
--- Results of commands - - PID TTY
TIME CMD
27712 pts /4
00:00:00 bash
27727 pts /4
00:00:00 toyshell
27728 pts /4
00:00:00 ps
Child exited

CSP - SDL - prafulla / Shell$ exit


prafulla@prafulla - HP -15 - Notebook - PC :~/ Desktop$

/ - - exit for exiting shell

You might also like