-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwarp.c
More file actions
98 lines (96 loc) · 2.64 KB
/
warp.c
File metadata and controls
98 lines (96 loc) · 2.64 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
#include "headers.h"
char prev_dir[100];
char pres_dir[100];
int warp(char **command, int bg, int cmd_len, int *pipe_fd, int *pipe_fd2, int active_pipe)
{
char home_dir[100];
readlink("/proc/self/exe", home_dir, sizeof(home_dir));
char *abs_home = strrchr(home_dir, '/');
*abs_home = '\0';
getcwd(pres_dir, sizeof(pres_dir));
if (cmd_len == 1 || (cmd_len == 2 && !strcmp(command[1], "~")))
{
getcwd(prev_dir, sizeof(prev_dir));
printf("%s\n", home_dir);
chdir(home_dir);
}
else if (cmd_len == 2 && !strcmp(command[1], "."))
{
getcwd(prev_dir, sizeof(prev_dir));
printf("%s\n", pres_dir);
}
else if (cmd_len == 2 && !strcmp(command[1], ".."))
{
getcwd(prev_dir, sizeof(prev_dir));
char *temp = strrchr(pres_dir, '/');
*temp = '\0';
printf("%s\n", pres_dir);
chdir(pres_dir);
}
else if (cmd_len == 2 && !strcmp(command[1], "-"))
{
if (prev_dir == NULL)
{
strcpy(prev_dir, home_dir);
}
chdir(prev_dir);
printf("%s\n", prev_dir);
strcpy(prev_dir, pres_dir);
}
else if (cmd_len == 2 && (!chdir(command[1])))
{
getcwd(prev_dir, sizeof(prev_dir));
chdir(command[1]);
getcwd(pres_dir, sizeof(pres_dir));
printf("%s\n", pres_dir);
}
else if (cmd_len == 2 && (command[1][0] == '~') && (command[1][1] == '/'))
{
getcwd(prev_dir, sizeof(prev_dir));
chdir(home_dir);
if (chdir(command[1] + 2))
{
printf("No such folder\n");
}
else
{
getcwd(pres_dir, sizeof(pres_dir));
printf("%s\n", pres_dir);
}
}
else if (cmd_len > 2)
{
for (int i = 1; i < cmd_len; i++)
{
getcwd(prev_dir, sizeof(prev_dir));
if (!chdir(command[i]))
{
getcwd(pres_dir, sizeof(pres_dir));
printf("%s\n", pres_dir);
}
else if ((command[i][0] == '~') && (command[i][1] == '/'))
{
chdir(home_dir);
if (chdir(command[i] + 2))
{
printf("No such folder\n");
}
else
{
getcwd(pres_dir, sizeof(pres_dir));
printf("%s\n", pres_dir);
}
}
else
{
printf("No such folder\n");
break;
}
}
}
else if (cmd_len == 2 && chdir(command[1]))
{
printf("No such Folder Present\n");
}
return 1;
}