-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilt-in.c
159 lines (144 loc) · 3.09 KB
/
built-in.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
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "main.h"
/**
* _env - prints the environment
*
* Return: Nothing.
*/
int _env(void)
{
int i;
for (i = 0; environ[i] != NULL; i++)
{
_printf("%s\n", environ[i]);
}
return (0);
}
/**
* env_manipulation - manipulates the environ array
* @newenv: the new element to be added
* @i: number of elements to be copied from _environ
*
* Return: success 0, or failure -1
*/
int env_manipulation(char *newenv, int i)
{
int j;
char **new_environ = NULL;
new_environ = (char **)malloc(sizeof(char *) * (i + 2));
if (new_environ == NULL)
{
errno = ENOMEM;
perror("");
return (-1);
}
for (j = 0; j < i; j++)
new_environ[j] = environ[j];
new_environ[i] = newenv;
new_environ[i + 1] = NULL;
free(environ);
environ = new_environ;
return (0);
}
/**
* _setenv - Initialize a new environ, or modify an existing one
* @arg: a pointer to an array of pointers
* @count: the number of commands until moment
*
* Return: return zero on success, or -1 on error
*/
int _setenv(char **arg, int count)
{
char *newenv = NULL;
int i;
if (arg[1] == NULL || _strlen(arg[1]) == 0 || arg[1][0] == '=')
{
/*errno = EINVAL;*/
/*fprintf(stderr, "./hsh: %d: setenv: ", count);*/
/*perror("");*/
return (-1);
}
if (arg[2] == NULL)
return (_unsetenv(arg, count));
newenv = malloc(sizeof(char) * (_strlen(arg[1]) + _strlen(arg[2]) + 2));
if (newenv == NULL)
{
errno = ENOMEM;
/*fprintf(stderr, "./hsh: %d: setenv: ", count);*/
perror("");
return (-1);
}
newenv[0] = '\0';
newenv = _strcpy(newenv, arg[1]);
newenv = _strcat(newenv, "=");
newenv = _strcat(newenv, arg[2]);
for (i = 0; environ[i] != NULL; i++)
{
if (_strncmp(environ[i], arg[1], _strlen(arg[1])) == 0)
{
if (_strcmp(environ[i], newenv) == 0)
{
free(newenv);
return (0);
}
free(environ[i]);
environ[i] = newenv;
return (0);
}
}
return (env_manipulation(newenv, i));
}
/**
* _unsetenv - Remove an environment variable
* @arg: a pointer to an array of pointers
* @count: the number of commands until moment
*
* Return: zero on success, or -1 on error,
*/
int _unsetenv(char **arg, int count __attribute__((unused)))
{
int n, m;
if (arg[1] == NULL || _strlen(arg[1]) == 0 || arg[1][0] == '=')
{
/*errno = EINVAL;*/
/*fprintf(stderr, "./hsh: %d: unsetenv: ", count);*/
/*perror("");*/
return (-1);
}
for (n = 0; environ[n] != NULL; n++)
{
if (_strncmp(environ[n], arg[1], _strlen(arg[1])) == 0)
{
free(environ[n]);
environ[n] = NULL;
for (m = n + 1; environ[m] != NULL; n++, m++)
environ[n] = environ[m];
environ[n] = NULL;
return (0);
}
}
return (0);
}
/**
* exiting - process arguments for exiting
* @arg: the argument which determines the status code
* @count: the number of arguments until now
*
* Return: the status code to exit
*/
int exiting(char *arg, int count)
{
int i;
for (i = 0; i < (int)_strlen(arg); i++)
{
if (!(arg[i] > 47 && arg[i] < 58))
{
fprintf(stderr, "./hsh: %d: exit: Illegal number: %s\n", count, arg);
return (2);
}
}
return (_atoi(arg));
}