-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwarp.c
More file actions
158 lines (154 loc) · 3.79 KB
/
warp.c
File metadata and controls
158 lines (154 loc) · 3.79 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
#include "./include/headers.h"
char originalshell[500];
char previouspath[500];
char currentpath[500];
int prevpathFlag = 0;
void originalpath()
{
char path[128];
getcwd(path, 128);
strcpy(originalshell, path);
return;
}
//hello
char *replace(char *token)
{
char *result = (char *)malloc(sizeof(char) * 500);
int size = strlen(token);
int pos = 0;
for (int i = 0; i < size; i++)
{
if (i == 0)
result[pos++] = '.';
else
result[pos++] = token[i];
}
result[pos] = '\0';
return result;
}
int gotoshell(char *token)
{
chdir(originalshell);
int t = chdir(replace(token));
if (t == -1)
{
fprintf(stderr,RED"Invalid directory or path\n"RESET);
error1();
return -1;
}
return 0;
}
int minussign(char *token)
{
char temp[500];
int size = strlen(token);
if (strcmp(token, "-") == 0)
{
strcpy(temp, currentpath);
strcpy(currentpath, previouspath);
strcpy(previouspath, temp);
}
else
{
fprintf(stderr,RED"Invalid path detected including '-'\n"RESET);
return -1;
}
chdir(currentpath);
return 0;
}
int warp(char *string)
{
int flag = 0;
char temp[100];
strcpy(temp, string);
char *token;
char delimeter[] = " \n\t\f\v\r";
char *ptr_in = NULL;
token = __strtok_r(temp, delimeter, &ptr_in);
getcwd(currentpath, sizeof(currentpath));
while (token != NULL)
{
if (strcmp(token, "warp") == 0)
{
token = __strtok_r(NULL, delimeter, &ptr_in);
continue;
}
else if (strstr(token, "~") == token)
{
getcwd(currentpath, sizeof(currentpath));
int ret = gotoshell(token);
if (ret == -1)
{
chdir(currentpath);
flag = 1;
break;
}
else
{
strcpy(previouspath, currentpath);
prevpathFlag = 1;
getcwd(currentpath, sizeof(currentpath));
printf("%s\n", currentpath);
}
flag = 1;
// prompt();
}
else if (strstr(token, "-") == token)
{
if (prevpathFlag == 0)
{
printf(YELLOW"There is no previous directory to jump\n"RESET);
flag = 1;
return -1;
}
getcwd(currentpath, sizeof(currentpath));
char temp[500];
strcpy(temp, previouspath);
int ret = minussign(token);
if (ret == -1)
{
strcpy(previouspath, temp);
flag = 1;
return -1;
}
else
{
getcwd(currentpath, sizeof(currentpath));
printf("%s\n", currentpath);
}
// printf("\n");
// prompt();
flag = 1;
}
else
{
getcwd(currentpath, sizeof(currentpath));
int check = chdir(token);
if (check == -1)
{
fprintf(stderr,RED"Invalid directory or path\n"RESET);
error1();
flag = 1;
return -1;
}
else
{
strcpy(previouspath, currentpath);
prevpathFlag = 1;
getcwd(currentpath, sizeof(currentpath));
printf("%s\n", currentpath);
}
flag = 1;
// printf("\n");
// prompt();
}
token = __strtok_r(NULL, delimeter, &ptr_in);
}
if (flag == 0)
{
getcwd(previouspath, sizeof(previouspath));
chdir(originalshell);
printf("%s\n", originalshell);
}
return 0;
}