-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcd.c
77 lines (72 loc) · 1.83 KB
/
cd.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
#include "header.h"
void callingcd(char *str,int zip, char *str1)
{
char inp[10000];
strcpy(inp,"");
char *temp2=strtok(str, " \t");
ll i=0;
while( temp2 != NULL && i<2 )
{
if(strcmp(temp2,"cd")!=0){
if(i==0){
strcpy(inp,temp2);
}
i++;
}
temp2 = strtok(NULL, " \t");
}
if(strlen(inp)==0)
{
// chdir(str1);
// exit(0);
return;
}
else
{
if(i>1)
{
fprintf(stdout,"\033[0;33m--> WARNING : syntax invalid, multiple arguments output for : [cd %s]\033[0m\n",inp);
}
char *temp=(char *)malloc(100000*sizeof(char));
strcpy(temp,inp);
if(temp[0]=='~')
{
if(chdir(str1)<0)
{
fprintf(stdout,"\033[1;31m--> ERROR : directory not found [ %s ]\033[0m\n",temp);
exit_fail=1;
exit(0);
}
if(strlen(temp)!=1)
{
temp[0]='.';
if(chdir(inp)<0)
{
fprintf(stdout,"\033[1;31m--> ERROR : directory not found [ %s ]\033[0m\n",temp);
exit_fail=1;
// exit(0);
}
}
}
else if(temp[0]=='-')
{
printf("%s\n",predir);
if(chdir(predir)<0){
fprintf(stdout,"\033[1;31m--> ERROR : directory not found [ %s ]\033[0m\n",temp);
exit_fail=1;
// exit(0);
}
}
else
{
if(chdir(temp)<0){
fprintf(stdout,"\033[1;31m--> ERROR : directory not found [ %s ]\033[0m\n",temp);
exit_fail=1;
// exit(0);
}
}
free(temp);
}
free(temp2);
return ;
}