-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCs1.c
More file actions
301 lines (290 loc) · 7.41 KB
/
Copy pathCs1.c
File metadata and controls
301 lines (290 loc) · 7.41 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
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
293
294
295
296
297
298
299
300
301
#include "header.h"
//extern int pipe exists, so maybe we can do >, | and ;
void init_buffer()
{
Terminal_Buffer = malloc(sizeof(char*) * capcity);
for(int i=0;i<capcity;i++)Terminal_Buffer[i]=NULL;
size_BUFER=0;
return;
}
char** split(char* line)
{
char** args = malloc(130 * sizeof(char*));
int position = 0,size=130;
char* x = strtok(line,sprtr);
while(x)
{
if(position == size-1)
{
args = realloc(args,2*size*sizeof(char*));
size=2*size;
}
args[position++]=x;
x=strtok(NULL,sprtr);
}
args[position]=NULL;//to terminate the while loop in freeing args
return args;
}
char* relpath_calc(char* abs_a,char* abs_b)
{
char* str = malloc(4096);
int count = 1;
str[0]='.';
char *a=abs_a,*b=abs_b;
while(*a && *b && *a ==*b)
{
a++;
b++;
}
//backtrack for (./a/bc ./a/b) to get (/bc /b) instead of (c NULL)
if(!a && !b)return NULL;
while(*(a)!='/' && *(b)!='/'){a--;b--;}
while(*a)
{
if(*a=='/')
{
str[count++]='/';
str[count++]='.';
str[count++]='.';
}
a++;
}
while(*b)
{
str[count++]=*(b++);
}
return str;
}
bool cd(char* args)
{
bool status = 0;
if(!args || args[0]=='~')
{
free(OLD_DIR);
OLD_DIR=getcwd(NULL,0);
status = (cd(STARTING_DIR)==0);
}
else if(args[0]=='-')
{
char* x = getcwd(NULL,0);
status = (chdir(OLD_DIR)==0);
free(OLD_DIR);
OLD_DIR=x;
}
else if(args[0]=='/')
{
free(OLD_DIR);
OLD_DIR=getcwd(NULL,0);
status = (chdir(args)==0);
if(status==0)printf("Directory doesn't exist\n");
}
else if(strncmp(args,"./",2)==0)
{
free(OLD_DIR);
OLD_DIR=getcwd(NULL,0);
status = (chdir(args)==0);
if(status==0)printf("No such directory exists\n");
}
if(status)
{
if(RELP_DIR)free(RELP_DIR);
RELP_DIR = relpath_calc(STARTING_DIR,getcwd(NULL,0));
}
return status;
}
bool run(char** args)
{
bool status = 0;
int len = strlen(args[0]);
if(strncmp(args[0],"exit",4)==0)
{
int x=0;
if(args[2])printf("User has chosen to exit, we will ingone all further flags / following commands\n");
args[0]+=4;
if(args[0][0]!='(')exit(atoi(args[1]));
else args[0]++;
args[0][strlen(args[0]) - 1]='\0';
if(args[0])x = atoi(args[0]);
exit(x);
}
else if(len == 5 && strncmp(args[0],"clear",5)==0)
{
if(args[1])
{
printf("This command doesn't support flags/arguments\nskipping the rest\n");
sleep(1);
}
printf("\033[H\033[J");
printf("%s",LINUX);
status=1;
}
else if(len == 4 && strncmp(args[0],"echo",4)==0)
{
if(!args[1])
{
printf("USAGE: echo x prints x where x is single line string");
return 0;
}
for(int i=1; args[i]; i++)
{
printf("%s ",args[i]);
}
printf("\n");
status=1;
}
else if(len == 4 && strncmp(args[0],"help",4)==0 && strlen(args[0])==4)
{
printf(CYN"Welcome to the CzShell.\nCongratulations for making it this far."
"\n\nList of commands with their description is as follows:\n\n"RST);
printf("%s",docstrin);
printf("press any key to return\n");
char c;
if(c = fgetc(stdin))return 1;
}
else if(len == 3 && strncmp(args[0],"pwd",3)==0 )
{
if(args[1])printf(RD"WARNING: "RST"Chosen command doesn't support arguments/flags\nIgnoring "CYN"%s"RST" and onwards\n",args[1]);
printf("%s\n",getcwd(NULL,0));
status=1;
}
else if(len == 2 && strncmp(args[0],"cd",2)==0)
{
status = cd(args[1]);
}
else if(len == 2 && strncmp(args[0],"ls",2)==0)
{
if(args[1]==NULL)
{
DIR* x = opendir(".");
if(!x)return 0;
printf("listing out Directory: %s\n",OLD_DIR?OLD_DIR:STARTING_DIR);
printf("Name\n");
struct dirent* f=NULL;
while(f=readdir(x))
{
printf("%s\n",f->d_name);
}
closedir(x);
printf("\n\n");
status = 1;
}
else if(args[1][0] != '-')
{
DIR* x = opendir(args[1]);
if(!x)return 0;
printf("listing out Directory: %s\n",OLD_DIR?OLD_DIR:STARTING_DIR);
printf("Name\n");
struct dirent* f=NULL;
while(f=readdir(x))
{
printf("%s\n",f->d_name);
}
closedir(x);
printf("\n\n");
status = 1;
}
else status = 0;
}
else if(len == 3 && strncmp(args[0],"cat",3)==0)
{
if(!args[1])printf("USAGE : cat filepath, prints contents of file(if not binary)\n");
else
{
FILE* f = fopen(args[1],"r");
if(!f)perror("Error while opening file");
else
{
int c;
c = fgetc(f);
if (!isprint(c) && !isspace(c) && c != EOF)
{
printf("This looks like a binary file.\npress 'Y' (case sensitive) if you want to print regardless");
if(getc(stdin)!='Y')
{
ungetc(c,f);
fclose(f);
return 0;
}
}
ungetc(c,f);
while((c=fgetc(f))!=-1)printf("%c",c);
fclose(f);
}
}
}
else if(len == 5 && strncmp(args[0],"touch",5)==0)
{
FILE* f = fopen(args[1],"a");
if(!f)perror("Error while opening file");
else fclose(f);
}
else
{
printf("It seems you have entered an invalid command.\nPlease try calling help for a list of possible commands\n");
}
return status;
}
bool parse()
{
char* line = Terminal_Buffer[(size_BUFER == 0) ? (capcity - 1) : (size_BUFER - 1)];
char** args = split(line);
bool s = run(args);
free(args);
return s;
}
bool input()
{
char* s = malloc(4196*sizeof(char));
char c;
int position=0,iter=1;
while((c = fgetc(stdin)))
{
if(c<0)return position>0;
if(c == '\n' )
{
s[position]=' ';
s[position+1]='\0';
break;
}
else if (c==-1)break;
if(position>=4192 * iter)
{
iter++;
s = realloc(s,sizeof(char)*4192 * iter + 4);
}
s[position++]=c;
}
if(Terminal_Buffer[size_BUFER])free(Terminal_Buffer[size_BUFER]);
Terminal_Buffer[size_BUFER++]=s;
size_BUFER=size_BUFER%capcity;
return 1;
}
void term(void)
{
printf("exiting in \0337");
fflush(stdout);
for(int i=0;i<3;i++)
{
printf("\0338\0337%d seconds",3-i);
fflush(stdout);
sleep(1);
}
printf("\r\x1B[2K");
return;
}
int main()
{
atexit(term);
init_buffer();
STARTING_DIR=getcwd(NULL,0);
OLD_DIR=NULL;
printf("%s",LINUX);
bool SUCCEEDED = 1;
while(1)
{
printf("%s%s>%s", (SUCCEEDED? GREN:RD), (RELP_DIR==NULL?"":RELP_DIR), RST);
if(input())SUCCEEDED = parse();
// sleep(1);
}
return 0;
}