-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_cd.c
More file actions
59 lines (55 loc) · 1.28 KB
/
_cd.c
File metadata and controls
59 lines (55 loc) · 1.28 KB
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
#include "holberton.h"
/**
* uDirectory - updates OLDPWD to current PWD
* @ssh: input ssh
* Return: index in linked list of PWD on success -
* If PWD or OLDPWD does not exist in environ vars,
* return -1
*/
int uDirectory(_unix *ssh)
{
register int _pwd = 0, index = 0;
static char old[BUFSIZE];
char *current = NULL;
_strcat(old, "OLD");
_pwd = searchNode(ssh->env, "PWD");
if (_pwd == -1)
{
return (-1);
}
current = getNodeAtIndex(ssh->env, _pwd);
_strcat(old, current);
insertNullByte(old, _strlen(current) + 4);
free(current);
index = searchNode(ssh->env, "OLDPWD");
if (index == -1)
{
return (-1);
}
deleteNodeAtIndex(&ssh->env, index);
addNodeAtIndex(&ssh->env, index, old);
insertNullByte(old, 0);
return (_pwd);
}
/**
* current - updates PWD to accurately reflect current directory
* @ssh: input ssh
* @index: index in linked list of where to insert PWD env var
* Return: true on success, false on failure
*/
_Bool current(_unix *ssh, int index)
{
static char tmp[BUFSIZE], cwd[BUFSIZE];
getcwd(tmp, BUFSIZE);
_strcat(cwd, "PWD=");
_strcat(cwd, tmp);
if (index > -1)
{
deleteNodeAtIndex(&ssh->env, index);
addNodeAtIndex(&ssh->env, index, cwd);
} else
addNodeAtIndex(&ssh->env, 0, cwd);
insertNullByte(tmp, 0);
insertNullByte(cwd, 0);
return (true);
}