-
Notifications
You must be signed in to change notification settings - Fork 0
/
_cd.c
79 lines (72 loc) · 1.52 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
#include "shell.h"
/**
* _cd_helper - function helper of cd
* that takes key and look it up
* in enviroments variable and change
* directory to it
*
* @key: envs keys
* Return: 0 success otherwise error
*/
int _cd_helper(const char *key)
{
char *s, buffer[200];
getcwd(buffer, 200);
s = _enviroment_management(GET_VALUE, key, NULL);
if (chdir(s) == -1)
{
free(s);
return (errno);
}
_enviroment_management(SET_ENTRY, "OLDPWD", buffer);
free(s);
return (0);
}
/**
* _cd_helper2 - function that takes
* path as parameter and change
* directory to it
*
* @path: path to change directory to it
* Return: 0 on success ortherwise error
*/
int _cd_helper2(const char *path)
{
char buffer[200];
getcwd(buffer, 200);
if (chdir(path) == -1)
{
perror(_global_states(GET_SHELL_NAME, NULL));
return (errno);
}
_enviroment_management(SET_ENTRY, "OLDPWD", buffer);
return (0);
}
/**
* _cd - builtin function cd
* is a function that allows as
* to navigate through out different
* folders (directories) in our operating
* system
*
* @command: struct the stores information
* about passed commands
* Return: (0) success otherwise errors
*/
int _cd(command_t *command)
{
int len;
len = _str2dlen(command->arguments + 1);
if (len >= 1)
{
if (_strcmp("-", command->arguments[1]))
return (_cd_helper("OLDPWD"));
else if (_strcmp("~", command->arguments[1]))
return (_cd_helper("HOME"));
else
return (_cd_helper2(command->arguments[1]));
}
else if (!len)
return (_cd_helper("HOME"));
return (0);
}