-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcd.c
72 lines (71 loc) · 1.88 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
#include "./include/cd.h"
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
void changeDirectory(char *string, int *flagPointer)
{
if (string == NULL)
{
if (!chdir(HOMEDIR))
{
strcpy(PREVPATH, CURRPATH);
getcwd(CURRPATH, PATHANDNAMESIZE);
return;
}
}
else if (!strcmp(string, ".."))
{
if (!chdir(string))
{
strcpy(PREVPATH, CURRPATH);
getcwd(CURRPATH, PATHANDNAMESIZE);
return;
}
}
else if (!strcmp(string, "."))
{
strcpy(PREVPATH, CURRPATH);
getcwd(CURRPATH, PATHANDNAMESIZE);
return;
}
else if (!strcmp(string, "~") || string == NULL)
{
if (!chdir(HOMEDIR))
{
strcpy(PREVPATH, CURRPATH);
getcwd(CURRPATH, PATHANDNAMESIZE);
return;
}
}
else if (!strcmp(string, "-"))
{
if (!chdir(PREVPATH))
{
printf("%s\n", PREVPATH);
strcpy(PREVPATH, CURRPATH);
getcwd(CURRPATH, PATHANDNAMESIZE);
return;
}
}
else if (strcmp(string, "-") != 0 && strcmp(string, "~") != 0 && strcmp(string, ".") != 0 && strcmp(string, "..") != 0)
{
if ((*flagPointer) == 0) // flag is used for handling commands Downloads/Test so that it doesn't copy <somepath>/Downloads as PREVPATH
{
strcpy(PREVPATH, CURRPATH);
*flagPointer = 1;
}
if (chdir(string) == 0)
{
getcwd(CURRPATH, PATHANDNAMESIZE);
return;
}
else
{
if ((*flagPointer) == 1 && chdir(string) != 0)
chdir(PREVPATH); // for handling strings such as abc/dff where dff is not a valid directory name
perror("\033[0;91;49mError:\033[0m");
return;
}
}
}