-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_old.c
More file actions
204 lines (113 loc) · 3.25 KB
/
Copy pathshell_old.c
File metadata and controls
204 lines (113 loc) · 3.25 KB
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
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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/wait.h>
//step-1 : Read input.
int read_input(char **line, size_t *len){
if(getline(line, len, stdin) == -1){
return 0;
}
return 1;
}
//step-2: Parse Input -> splits string into pieces using delimiters
/*
Why char **args ?
Because:
char *args[100];
is: array of char pointers
When passed to function:
array decays into pointer
So type becomes:
char **
| Code | Meaning |
| ----------------- | ---------------------------- |
| `char *line` | one string pointer |
| `char *args[100]` | array of 100 string pointers |
*/
void parse_input(char *line, char **args){
int i=0;
char *token = strtok(line, " \n");
while(token != NULL){
args[i++] = token;
token = strtok(NULL, " \n");
}
args[i]=NULL;
}
//step-3: Execute command -
void execute_command(char **args){
pid_t pid = fork();
if(pid == 0){
//child ...
execvp(args[0], args);
perror("Exec error");
exit(1);
}else{
//parent
wait(NULL);
}
}
//## handles built in commands here - cd, pwd, help, exit
//(we can do many more but i am just taking this main main things...)
int handle_builtIn(char **args){
// 1. exit --> if exit types by user check by comparing and then, exit, since exit(0) - becz no error exit it is just by user.
if(strcmp(args[0], "exit") == 0){
exit(0);
}
// 2. cd
if(strcmp(args[0], "cd") == 0){
if(args[1] == NULL){
printf("cd: missing argument\n");
} else {
if(chdir(args[1]) != 0){ //folder doesn't exist here...therefore it not return success i.e 0
perror("cd failed");
}
}
return 1; //means found the cd
}
//3. pwd
if(strcmp(args[0], "pwd") == 0){
char cwd[1024]; //i should use dma later...
if(getcwd(cwd, sizeof(cwd)) != NULL){
printf("%s\n", cwd);
}else{
perror("pwd failed");
}
return 1;
}
//4. help
if(strcmp(args[0], "help") == 0){
printf("Hey thsese are some built-in commands:\n");
printf("cd <dir>\n");
printf("pwd\n");
printf("exit\n");
printf("help\n");
return 1;
}
return 0; //means builtin is not there so return 0 now it will do execute work...using execute funtion.
}
int main(){
while(1){
printf("Madhan-Shell :> ");
fflush(stdout); //output may wait in buffer so it force print now.
/* here line is a pointer to character(string) ,
since string becz - not single char is there multiple char is there */
char *line = NULL;
size_t len = 0;
char *args[100]; //here i think we should define dynamic like things not fixed...
//#1
if (!read_input(&line, &len)) {
printf("\nExiting shell...\n");
break;
}
//#2
parse_input(line, args);
// Check is there is any builtin command ??? if there handling manually...by own builtin function
if(handle_builtIn(args) == 1){
continue;
}
//#3:
execute_command(args);
}
return 0;
}