-
Notifications
You must be signed in to change notification settings - Fork 3
/
cd_help.c
202 lines (168 loc) · 4.76 KB
/
cd_help.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "shell.h"
/**
* cd_arg - move to given path
* @arg: The directory to go to
*
* Return: 0 on Success, 1 on Failure
*/
int cd_arg(char *arg)
{
char *target_dir = NULL;
char *path = NULL;
size_t len = 0;
if (chdir(arg) == 0) /* if arg is valid dir */
{
if (path_check(&arg)) /* if arg is path */
{
set_OLDPWD();
set_PWD(arg);
return (0);
}
else /* concat arg to PWD */
{
/* retrieve target path */
target_dir = get_target("PWD=");
/* take lengths of path and arg */
len = (_strlen(target_dir) + _strlen(arg));
/* +2 for '/' and null-byte */
path = alloc_mngr(path, (sizeof(char) * (len + 2)));
if (!path) /* if malloc fail */
return (-1);
/* copy path to new string */
_strncpy(path, target_dir, _strlen(target_dir));
_strcat(path, "/"); /* concatenate dir name to path */
_strcat(path, arg);
set_OLDPWD(); /* update env vars */
set_PWD(path);
return (0);
}
}
errno = ENOENT; /* invalid path */
return (-1);
}
/**
* cd_user - move to user home
* @argv: User name
*
* Return: 0 on Success, 1 on Failure
*/
int cd_user(char *argv)
{
char *userdir = NULL;
size_t user_len = _strlen(argv);
userdir = alloc_mngr(userdir, (sizeof(char) * (6 + user_len)));
if (!userdir) /* if malloc fail */
return (-1);
_strncpy(userdir, "/home/", 6); /* copy "/home/" to new string */
_strcat(userdir, &argv[1]); /* concat username to dir */
if (chdir(userdir) == 0) /* move to user home dir */
{
set_OLDPWD();
set_PWD(userdir);
return (0);
}
errno = ENOENT; /* invalid username - dir not found */
return (-1);
}
/**
* get_target - copy target path from environ
* @var_name: name of target env variable
*
* Return: target path on Success, NULL on Failure
*/
char *get_target(char *var_name)
{
char *path = NULL;
size_t i = 0;
size_t path_len = 0;
size_t name_len = _strlen(var_name); /* length of var name including "=" */
for (; environ[i]; i++) /* iterate thru environ */
{
/* if env variable name matches input */
if ((_strncmp(var_name, environ[i], name_len)) == 0)
{
/* take length of path */
path_len = (_strlen(environ[i]) - name_len);
/* allocate for target path */
path = alloc_mngr(path, (sizeof(char) * (path_len + 1)));
if (!path) /* if malloc fail */
return (NULL); /* return error */
/* copy path from env to new string */
_strncpy(path, &environ[i][name_len], path_len);
return (path);
}
}
errno = ENOENT; /* set error to f or dir not found */
return (NULL); /* variable not found in environ */
}
/**
* set_PWD - set path value of environ var PWD
* @value: desired value of variable
*
* Return: 0 on Success, 1 on Failure
*/
int set_PWD(char *value)
{
char *update = NULL;
char *name = "PWD=";
size_t i = 0;
size_t val_len;
size_t name_len = 4;
val_len = _strlen(value); /* take length of target value */
for (; environ[i]; i++) /* iterate thru environ */
{
/* if env variable name matches input */
if ((_strncmp(name, environ[i], name_len)) == 0)
{
/* reallocate space for updated env var */
environ[i] = _realloc(update, (sizeof(char) * (name_len + val_len + 1)));
if (!environ[i]) /* if malloc fail */
return (-1);
add_mem_node(&static_mem_head, environ[i]); /* add to lasting free list */
_strncpy(environ[i], name, name_len); /* copy var name to realloc */
_strcat(environ[i], value); /* concat new value to realloc */
/* if path ends with '/', replace with another null-byte */
if (environ[i][name_len + val_len - 1] == '/')
environ[i][name_len + val_len - 1] = '\0';
return (0);
}
}
errno = ENOENT; /* env var name not found */
return (-1);
}
/**
* set_OLDPWD - set path value of environ var OLDPWD
*
* Return: 0 on Success, 1 on Failure
*/
int set_OLDPWD(void)
{
char *update = NULL;
char *pwd = NULL;
char *owd_ref = "OLDPWD=";
char *pwd_ref = "PWD=";
size_t i;
size_t pwd_len = 0;
for (i = 0; environ[i]; i++) /* iterate thru environ for PWD */
if ((_strncmp(pwd_ref, environ[i], 4)) == 0) /* if PWD variable found */
pwd = &environ[i][4]; /* point to PWD value */
if (!pwd) /* could not find PWD var */
return (-1);
pwd_len = _strlen(pwd); /* take length pwd path */
for (i = 0; environ[i]; i++) /* iterate thru environ for OLDPWD */
{
if ((_strncmp(owd_ref, environ[i], 7)) == 0) /* if OLDPWD variable found */
{
/* reallocate space for updated env var */
environ[i] = _realloc(update, (sizeof(char) * (7 + pwd_len + 1)));
if (!environ[i]) /* if malloc fail */
return (-1);
add_mem_node(&static_mem_head, environ[i]); /* add to lasting free list */
_strncpy(environ[i], owd_ref, 7); /* copy OLDPWD var name to realloc */
_strcat(environ[i], pwd); /* concat new value to realloc */
return (0);
}
}
errno = ENOENT; /* env var name not found */
return (-1);
}