forked from glyif/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcd.c
81 lines (66 loc) · 1.46 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
#include "header.h"
/**
* yellow_brick_road - assembles path to cd into
* @commands: array of pointer pointg to commands
* @envlist: head of env linked list
*
* Return: full filepath
*/
char *yellow_brick_road(char **commands, env_t *envlist)
{
env_t *fetched_home, *fetched_old;
int hyphen;
char *path;
path = safe_malloc(1024);
fetched_home = fetch_node(envlist, "HOME");
fetched_old = fetch_node(envlist, "OLDPWD");
if (commands[1] != NULL)
hyphen = _strncmp(commands[1], "-", 1);
if (commands[1] == NULL)
path = _strcpy(path, fetched_home->val);
else if (hyphen == 0)
path = _strcpy(path, fetched_old->val);
else if (commands[1][0] == '/')
path = _strcpy(path, commands[1]);
else
{
getcwd(path, 1024);
_strncat(path, "/", 1);
_strncat(path, commands[1], _strlen(commands[1]));
}
return (path);
}
/**
* _cd - builtin cd function
* @arginv: arg inventory
*
* Return: 0 if good, -1 if bad
*/
int _cd(arg_inventory_t *arginv)
{
char *path, *oldpwd, *pwd, **commands;
int check;
oldpwd = safe_malloc(1024);
pwd = safe_malloc(1024);
getcwd(oldpwd, 1024);
commands = (char **)arginv->commands;
path = yellow_brick_road(commands, arginv->envlist);
check = chdir(path);
if (check == -1)
{
free(oldpwd);
free(pwd);
free(path);
return (2);
}
else
{
getcwd(pwd, 1024);
modify_node_env(&arginv->envlist, "PWD", pwd);
modify_node_env(&arginv->envlist, "OLDPWD", oldpwd);
}
free(oldpwd);
free(pwd);
free(path);
return (0);
}