-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
207 lines (171 loc) · 4.38 KB
/
main.c
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
204
205
206
207
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include<unistd.h>
#include<dirent.h>
#include"commands.c"
#include"help.c"
#define RL_BUFSIZE 1024 // Buffer Size for Read_Line
#define TOKEN_BUFSIZE 64 // Buffer Size for Parser
#define TOKEN_DELIM_STR " \n\t\r\a" //Creating a list of delimiters to pass to strtok().
char *read_line()
{
int bufsize = RL_BUFSIZE; // creating a buffer
int pos = 0;
char *buffer = malloc(sizeof(char) * bufsize); // allocating space for buffer
int c;
if(!buffer) // Used to check if memory is allocated successfully
{perror("Error ");}
while(1)
{
c = getchar(); // returns the character read as an unsigned char cast to an int or EOF on end of file
// If we hit EOF replace it with null character and return.
if(c == EOF || c == '\n')
{
buffer[pos] = '\0';
return buffer;
}
else
{
buffer[pos] = c;
}
pos++;
//If we exceed buffer limit, dynamically reallocate.
if(pos >= bufsize)
{
bufsize += RL_BUFSIZE;
buffer = realloc(buffer,bufsize);
if(!buffer)
{perror("Error : ");}
}
}
}
char **parse_line(char *line)
{
int bufsize = TOKEN_BUFSIZE, pos = 0;
char **tokens = (char**)malloc(sizeof(char *) * bufsize);
char *token;
if(!tokens)
{perror("Error : ");}
// strtok() is used to return pointers to the tokens in the string you give it and place '\0' at the end of each token
token = strtok(line, TOKEN_DELIM_STR); // Begin tokenising. Returns a pointer to the first token.
while(token != NULL )
{
// Reallocate memory if overflow occurs
if(pos >= bufsize)
{
bufsize += TOKEN_BUFSIZE;
tokens = realloc(tokens, bufsize * sizeof(char *));
if(!tokens)
{perror("Error : ");}
}
tokens[pos] = token;
pos++;
token = strtok(NULL, TOKEN_DELIM_STR);
}
tokens[pos] = NULL; // NUll terminate the list of tokens
return tokens;
}
char *builtin_str[] = {
"cd",
"mkdir",
"rmdir",
"ls",
"pwd",
"cat",
"cp",
"mv",
"touch",
"rm",
"clear",
"echo",
"help",
"exit"
};
int (*functions[]) (char **) = {
&cd,
&mkDir,
&rmDir,
&ls,
&pwd,
&cat,
&cp,
&mv,
&touch,
&rm,
&clear,
&echo,
&help,
&exit1
};
int num_builtins()
{
return (sizeof(builtin_str)/sizeof(char**));
}
void setup()
{
int i;
printf("\n Simple Linux Shell\n");
printf(" Type program names and arguments, and hit enter.\n");
printf(" The following are built in:\n");
for (i = 0; i < num_builtins(); i++)
{
printf(" %s\n", builtin_str[i]);
}
printf("\n\n");
}
void loop_shell()
{
char *line ;
char **args ;
int status = 1, i;
do
{
printf("> ");
line = read_line();
args = parse_line(line);
//printf("----------->>>>> %s??%s? ?",args[0],args[3]);
Execute(args);
printf("\n");
free(line);
free(args);
}while(status);
}
void Execute(char**args)
{
if( args[0]!=NULL)
for (int i = 0; i < num_builtins(); i++)
if (strcmp(args[0], builtin_str[i]) == 0)
return (*functions[i])(args);
/*if(strcmp(args[0],"exit")==0 || strcmp(args[0],"Exit")==0)
exit(0);
else if(strcmp(args[0],"mkdir")==0 )
mkDir(args[1]);
else if(strcmp(args[0],"rmdir")==0 )
rmDir(args[1]);
else if(strcmp(args[0],"cd")==0 )
cd(args);
else if(strcmp(args[0],"ls")==0 )
ls(args);
else if(strcmp(args[0],"pwd")==0 )
pwd();
else if(strcmp(args[0],"clear")==0)
clear();
else if(strcmp(args[0],"cat")==0 )
cat(args[1]);
else if(strcmp(args[0],"cp")==0 )
cp(args[1],args[2]);
else if(strcmp(args[0],"mv")==0)
mv(args[1],args[2]);
else if(strcmp(args[0],"touch")==0 )
touch(args[1]);
else if(strcmp(args[0],"rm")==0 )
rm(args[1]);*/
}
int main()
{
setup();
loop_shell();
return 0;
}