-
Notifications
You must be signed in to change notification settings - Fork 0
/
cd.c
101 lines (100 loc) · 2.16 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
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
#include "shell.h"
/**
*_cd - change directory builtin
* @av: argument to work with
* Return: int
*/
int _cd(char *av[])
{
char *oldpwd = NULL, *newpath, *pathbit, *newptr;
char *homestr = "HOME", *oldpwdstr = "OLDPWD";
int ret, printpath = 0;
oldpwd = getcwd(oldpwd, 0);
if (oldpwd == NULL)
return (1);
if (av[1] == NULL || av[1][0] == 0)
{
newpath = _getenv(homestr);
if (newpath == homestr)
newpath = _strdup("");
av[1] = newpath;
av[2] = NULL;
}
else if (av[1][0] == '-' && av[1][1] == 0)
{
/*check getenv malloc error here and above*/
newpath = _getenv(oldpwdstr);
if (newpath == oldpwdstr)
newpath = _strdup("");
printpath = 1;
free(av[1]);
av[1] = newpath;
}
/*{*/
#ifdef DEBUGCD
printf("Making new path %s:%c\n", av[1], av[1][0]);
#endif
newpath = malloc(sizeof(char) * (_strlen(oldpwd) + _strlen(av[1]) + 2));
if (newpath == NULL)
return (-1);
newptr = newpath;
pathbit = oldpwd;
if (av[1][0] != '/' && pathbit[1] != 0)
while (*pathbit)
*newptr++ = *pathbit++;
*newptr++ = '/';
pathbit = strtok(av[1], "/");
#ifdef DEBUGCD
printf("starting newpath:%s:Pathbit got:%s\n", newpath, pathbit);
printf("newpath/ptr diff:%p\n", newptr - newpath);
#endif
while (pathbit != NULL)
{
if (pathbit[0] == '.' && pathbit[1] == '.'
&& pathbit[2] == 0)
{
#ifdef DEBUGCD
printf("going back a directory%s:%s\n", newpath, newpath);
#endif
newptr--;
if (newptr != newpath)
newptr--;
while (*newptr != '/')
newptr--;
*newptr++ = '/';
}
else if (!(pathbit[0] == '.' && pathbit[1] == 0))
{
while (*pathbit)
*newptr++ = *pathbit++;
*newptr++ = '/';
}
pathbit = strtok(NULL, "/");
#ifdef DEBUGCD
printf("Got pathbit:%s\n", pathbit);
#endif
}
if (newptr != newpath && newptr != newpath + 1)
newptr--;
*newptr = 0;
#ifdef DEBUGCD
printf("New path:%s\n", newpath);
#endif
/*}*/
ret = chdir(newpath);
if (ret == 0)
{
_setenv("OLDPWD", oldpwd);
free(oldpwd);
_setenv("PWD", newpath);
if (printpath)
fprintstrs(1, newpath, "\n", NULL);
free(newpath);
return (0);
}
printerr(": cd: can't cd to ");
fprintstrs(STDERR_FILENO, av[1], "\n", NULL);
free(oldpwd);
free(newpath);
return (ret);
}