-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c~
292 lines (267 loc) · 5.99 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<dirent.h>
#include<termios.h>
#define MAXTOKLEN 1024
FILE* source;
char pwd[MAXTOKLEN+1];
char PATH[MAXTOKLEN+1];
char HOME[MAXTOKLEN+1];
char temp[MAXTOKLEN+1];
typedef enum{
nothing,cd,clr,dir,environ,echo,pause1,help,quit
}COMMANDS;
char* line;
char** args;
int status;
void do_cd()
{
int i;
//fprintf(stdout, "%d\n", args[1][0]);
if(args[1]!=NULL)
{
if(args[1][0]=='~' && args[1][1]=='/')
{
chdir(HOME);
for(i=2;i<strlen(args[1]);i++)
{
temp[i-2]=args[1][i];
}
temp[i-2]='\0';
//fprintf(stdout, "%s\n", temp);
chdir(temp);
getcwd(pwd,MAXTOKLEN+1);
}
else if(!chdir(args[1]))
{
//fprintf(stdout, "%s\n", pwd);
getcwd(pwd,MAXTOKLEN+1); //update pwd
}
else
{
// errors in chdir
if(errno==ENOENT)
fprintf(stdout,"cd:%s:No such directory exists\n",args[1]);
else if(errno==ENOTDIR)
fprintf(stdout,"cd:%s:Specified path not a directory\n",args[1]);
}
}
else
fprintf(stdout, "No directory argument provided\n");
fprintf(stdout, "Current Directory: %s\n", pwd);
}
void do_clr()
{
/*char clear [ 5 ] = { 27, '[', '2', 'J', 0 };
printf ( "%s", clear );*/
const char* CLEAR_SCREE_ANSI = "\e[1;1H\e[2J";
write(STDOUT_FILENO,CLEAR_SCREE_ANSI,12);
}
void do_dir()
{
DIR* dir=NULL;
if(args[1]!=NULL)
{
dir=opendir(args[1]);
int count;
struct dirent *dptr = NULL;
if(NULL == dir)
{
fprintf(stdout,"Could not open the working directory\n");
}
else
{
//printf("\n");
// Go through and display all the names (files or folders) contained in the directory.
for(count = 0; NULL != (dptr = readdir(dir)); count++)
{
printf("%s ",dptr->d_name);
}
printf("\nTotal %u\n", count);
}
}
else
{
dir=opendir(pwd);
int count;
struct dirent *dptr = NULL;
//printf("\n");
// Go through and display all the names (files or folders) contained in the directory.
for(count = 0; NULL != (dptr = readdir(dir)); count++)
{
printf("%s ",dptr->d_name);
}
printf("\nTotal %u\n", count);
}
}
void do_echo()
{
int i=1;
while(args[i]!=NULL)
{
fprintf(stdout,"%s ",args[i]);
i++;
}
fprintf(stdout,"\n");
}
void do_pause()
{
fprintf(stdout,"Press Enter to resume\n");
tcflow(STDOUT_FILENO, TCOOFF);
char temp_char='\0';
while(temp_char!='\n')
{
fscanf(source,"%c",&temp_char);
}
tcflush(STDOUT_FILENO, TCIOFLUSH);
tcflow(STDOUT_FILENO, TCOON);
}
void do_help()
{
;
}
void do_environ()
{
fprintf(stdout,"PWD=%s\n",pwd);
fprintf(stdout,"HOME=%s\n",HOME);
fprintf(stdout,"PATH=%s\n",PATH);
}
void do_quit()
{
exit(0);
}
void execute(char* token)
{
COMMANDS command=nothing;
if(!(strcmp(token,"cd")))
command=cd;
if(!(strcmp(token,"clr")))
command=clr;
if(!(strcmp(token,"dir")))
command=dir;
if(!(strcmp(token,"environ")))
command=environ;
if(!(strcmp(token,"echo")))
command=echo;
if(!(strcmp(token,"pause")))
command=pause1;
if(!(strcmp(token,"help")))
command=help;
if(!(strcmp(token,"quit")))
command=quit;
switch(command)
{
case cd:
do_cd();
break;
case dir:
do_dir();
break;
case environ:
do_environ();
break;
case echo:
do_echo();
break;
case pause1:
do_pause();
break;
case help:
do_help();
break;
case quit:
do_quit();
break;
case clr:
do_clr();
break;
case nothing:
fprintf(stdout,"%s: Command not found\n",args[0]);
break;
default:
break;
}
}
char *read_line()
{
char *line1 = NULL;
size_t buffersize = 0;
if((getline(&line1, &buffersize, source))!=-1)
return line1;
else
exit(0);
}
#define TOKEN_DELIM " \t\r\n\a"
char** parse(char* line1)
{
int buffersize=MAXTOKLEN+1, position = 0;
char **tokens = malloc(buffersize * sizeof(char*));
char *token;
if (!tokens)
{
fprintf(stderr, "Allocation error\n");
exit(EXIT_FAILURE);
}
token = strtok(line,TOKEN_DELIM);
while (token != NULL)
{
tokens[position] = token;
position++;
if (position >= buffersize)
{
buffersize += MAXTOKLEN;
tokens = realloc(tokens, buffersize * sizeof(char*));
if (!tokens)
{
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, TOKEN_DELIM);
}
tokens[position] = NULL;
return tokens;
}
int main(int argc, char *argv[], char *evnp[])
{
strcpy(HOME,getenv("HOME"));
if(argc==1)
{
source=stdin;
}
else
{
source=fopen(argv[1],"r");
if(source==NULL)
{
fprintf(stderr, "File %s not found\n",argv[1]);
return 0;
}
}
int i;
getcwd(pwd,MAXTOKLEN+1);
strcpy(PATH,pwd);
for(i=1;i<strlen(argv[0]);i++)
{
temp[i-1]=argv[0][i];
}
temp[i-1]='\0';
//fprintf(stdout, "%s\n", temp);
strcat(PATH,temp);
//printf("%s\n",pwd);
do{
if(source==stdin)
fprintf(stdout, "shell:%s$ ", strrchr(pwd,'/')+1);
else
fprintf(stdout,"\n");
line=read_line();
args=parse(line);
//fprintf(stdout, "%s\n", args[0]);
if(args[0]!=NULL)
execute(args[0]);
}while(1);
return 0;
}