-
Notifications
You must be signed in to change notification settings - Fork 0
/
warp.c
103 lines (97 loc) · 2.68 KB
/
warp.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
#include "headers.h"
void substitute(char **string_1, char **string_2)
{
char *res = *string_1;
*string_1 = *string_2;
*string_2 = res;
}
void warps(int pid, int tk_freq, char *tokens[])
{
if (pid)
{
printf("[%d]\n", getpid());
}
// for (int i = 0; i < tk_freq; i++)
// {
// printf("%s\n", tokens[i]);
// }
// return;
/*
The below code follows the change directory phase whenever necessary tokens
are provided after the warp.
*/
// printf("hello\n");
if (tk_freq == 1)
{
if (chdir(original) == -1)
{
perror("chdir");
}
strcpy(current_direc, original);
printf("%s\n", current_direc);
}
else
{
// different specifications start from here:
for (int i = 1; i < tk_freq; i++)
{
tokens[i] = trimspace(tokens[i]);
if (strcmp(tokens[i], "-") == 0)
{
if (chdir(undo) == -1)
{
perror("chdir");
}
substitute(¤t_direc, &undo);
printf("%s\n", current_direc);
}
else if (strcmp(tokens[i], ".") == 0)
{
printf("%s\n", current_direc);
}
else if (strcmp(tokens[i], "") == 0)
{
if (chdir(original) == -1)
{
perror("chdir");
}
strcpy(undo, current_direc);
strcpy(current_direc, original);
printf("%s\n", current_direc);
}
else if (strcmp(tokens[i], "~") == 0)
{
if (chdir(original) == -1)
{
perror("chdir");
}
strcpy(undo, current_direc);
strcpy(current_direc, original);
printf("%s\n", current_direc);
}
else if (strcmp(tokens[i], " ") == 0)
{
if (chdir(original) == -1)
{
perror("chdir");
}
strcpy(undo, current_direc);
strcpy(current_direc, original);
printf("%s\n", current_direc);
}
else
{
strcpy(undo, current_direc);
if (chdir(tokens[i]) == -1)
perror("No such type of directory exists");
else
{
getcwd(current_direc, MAX);
printf("%s\n", current_direc);
}
// printf("yes\n");
// return;
}
}
}
}