-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcd.c
63 lines (58 loc) · 1.67 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
#include "headers.h"
void cd(char *path)
{
if(path == NULL)
{
int temp = chdir(HOME);
if (temp < 0)
perror(DFLT "change directory error");
return;
}
char delim[] = " \n\t\r";
char *s = (char *) malloc(sizeof(char) * MX_L2); // handling extra spaces, tabs etc. in the path entered
strcpy(s, path);
char *token;
token = strtok(s, delim);
if (strcmp("~", token) == 0 || strcmp("", path) == 0)
token = HOME;
else if(strcmp("-", token) == 0)
{
char *found, *pseu;
char *lastdir = (char *) malloc(sizeof(char) * MX_L2);
strcpy(lastdir, LWD);
found = strstr(lastdir, HOME);
if(found)
{
pseu = found + strlen(HOME);
char *home = (char *) malloc(256 * sizeof(char));
strcpy(home, "~"); strcat(home,pseu); strcpy(pseu,home);
printf("%s\n",pseu);
free(home);
}
else
printf("%s", LWD);
token = get_abs_path(LWD);
free(lastdir);
}
else
token = get_abs_path(path); // else get absolute path
int temp = chdir(token);
if (temp < 0)
{
perror(DFLT "change directory error");
strcpy(emoji,":'(");
}
else
{
char *dir = (char *) malloc(sizeof(char) * MX_L2);
strcpy(dir, LWD);
char *dirr = (char *) malloc(sizeof(char) * MX_L2);
strcpy(dirr, CWD1);
strcpy(LWD, dirr);
dir = getcwd(NULL, 0);
strcpy(CWD1, dir);
free(dir);
free(dirr);
}
free(s);
}