-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_cd.c
More file actions
40 lines (38 loc) · 765 Bytes
/
Copy path_cd.c
File metadata and controls
40 lines (38 loc) · 765 Bytes
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
#include "shell.h"
/**
* _cd - function to change directory
* @args: arguments
* @envl: environmental linked list
* @pathl: path linked list
* @histl: history linked list
* Return: 0 for success
*/
int _cd(char **args, char ***envl, node_t **pathl, node_t **histl)
{
int i;
char *home;
char *prev;
(void) pathl;
(void) histl;
if (args[1] == NULL)
home = _strdup(_getenv("HOME", *envl));
else if (*args[1] == '-')
{
if (_getenv("OLDPWD", *envl) != NULL)
home = _strdup(_getenv("OLDPWD", *envl));
else
return (-1);
}
else
home = _strdup(args[1]);
prev = _strdup(_getenv("PWD", *envl));
i = chdir(home);
if (i == 0)
{
_setenv("OLDPWD", prev, 1, envl);
_setenv("PWD", home, 1, envl);
}
free(prev);
free(home);
return (0);
}