-
Notifications
You must be signed in to change notification settings - Fork 0
/
warp.c
86 lines (81 loc) · 2.61 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
#include "warp.h"
void swap1(char **str1_ptr, char **str2_ptr)
{
char *temp = *str1_ptr;
*str1_ptr = *str2_ptr;
*str2_ptr = temp;
}
void warps(int proccessid, int num_tokens, char *tokens[])
{
if (proccessid)
{
printf("[%d]\n", getpid());
}
if (num_tokens == 1)
{
chdir(homeworkingdir);
strcpy(currentworkingdir, homeworkingdir);
printf("%s\n", currentworkingdir);
}
else
{
for (int i = 1; i < num_tokens; i++)
{
tokens[i] = trimspace(tokens[i]);
if (strcmp(tokens[i], "-") == 0)
{
chdir(prevworkingdir);
swap1(¤tworkingdir, &prevworkingdir);
printf("%s\n", currentworkingdir);
}
else if (strcmp(tokens[i], "~") == 0)
{
strcpy(prevworkingdir, currentworkingdir);
strcpy(currentworkingdir, homeworkingdir);
chdir(currentworkingdir);
printf("%s\n", currentworkingdir);
}
else if (strcmp(tokens[i], ".") == 0)
{
printf("%s\n", currentworkingdir);
}
else if (strcmp(tokens[i], "") == 0)
{
chdir(homeworkingdir);
strcpy(prevworkingdir, currentworkingdir);
strcpy(currentworkingdir, homeworkingdir);
printf("%s\n", currentworkingdir);
}
else if (strcmp(tokens[i], " ") == 0)
{
chdir(homeworkingdir);
strcpy(prevworkingdir, currentworkingdir);
strcpy(currentworkingdir, homeworkingdir);
printf("%s\n", currentworkingdir);
}
else if (tokens[i][0] == '~')
{
strcpy(prevworkingdir, currentworkingdir);
chdir(homeworkingdir);
char *s1 = (char *)malloc(sizeof(char) * 1024);
for (int j = 2; j < strlen(tokens[i]); j++)
{
s1[j - 2] = tokens[i][j];
}
s1[strlen(tokens[i]) - 2] = '\0';
if (chdir(s1) == -1)
printf("error");
getcwd(currentworkingdir, 1024);
printf("%s\n", currentworkingdir);
}
else
{
strcpy(prevworkingdir, currentworkingdir);
if (chdir(tokens[i]) == -1)
printf("error");
getcwd(currentworkingdir, 1024);
printf("%s\n", currentworkingdir);
}
}
}
}