-
Notifications
You must be signed in to change notification settings - Fork 0
/
_change.c
95 lines (91 loc) · 1.9 KB
/
_change.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
#include "sshell.h"
/**
* _updatepwd - change the OLDPWD env variable
* @buf: pointer to old pwd
* @myenv: copy of environmental variable
* Return: the string inside PWD env variable
*/
void _updatepwd(char *buf, char **myenv)
{
char *entirepwd;
char str[] = "PWD=";
int i = 0, j = 0, cont = 0, k = 0, myenvlen = 0, buflen = 0;
entirepwd = str_concat(str, buf);
for (i = 0; myenv[i] != NULL; i++, cont = 0)
{
for (j = 0; myenv[i][j] != '\0' && j < 4; j++)
{
if (myenv[i][j] == str[j])
{
cont++;
}
else
break;
}
if (cont == 4)
break;
}
if (cont == 4)
{
myenvlen = _strlen(myenv[i]);
buflen = _strlen(entirepwd);
if (buflen < myenvlen)
{
for (k = 0; entirepwd[k] != '\0'; k++)
myenv[i][k] = entirepwd[k];
for (; k < myenvlen; k++)
myenv[i][k] = 0;
}
else
{
myenv[i] = _realloc(myenv[i], myenvlen, buflen + 1);
for (k = 0; entirepwd[k] != '\0'; k++)
myenv[i][k] = entirepwd[k];
}
}
free(entirepwd);
}
/**
* _updateoldpwd - change the OLDPWD env variable
* @buf: pointer to old pwd
* @myenv: copy of environmental variable
* Return: the string inside PWD env variable
*/
void _updateoldpwd(char *buf, char **myenv)
{
char *entirepwd;
char str[] = "OLDPWD=";
int i = 0, j = 0, cont = 0, k = 0, myenvlen = 0, buflen = 0;
entirepwd = str_concat(str, buf);
for (i = 0; myenv[i] != NULL; i++)
{
for (j = 0; myenv[i][j] != '\0' && j < 7; j++)
{
if (myenv[i][j] == str[j])
cont++;
else
break;
}
if (cont == 7)
break;
}
if (cont == 7)
{
myenvlen = _strlen(myenv[i]);
buflen = _strlen(entirepwd);
if (buflen < myenvlen)
{
for (k = 0; entirepwd[k] != '\0'; k++)
myenv[i][k] = entirepwd[k];
for (; k < myenvlen; k++)
myenv[i][k] = 0;
}
else
{
myenv[i] = _realloc(myenv[i], myenvlen, buflen + 1);
for (k = 0; entirepwd[k] != '\0'; k++)
myenv[i][k] = entirepwd[k];
}
}
free(entirepwd);
}