-
Notifications
You must be signed in to change notification settings - Fork 0
/
inbuilt_utils.c
59 lines (50 loc) · 1003 Bytes
/
inbuilt_utils.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
#include "shell.h"
/**
* env_name_verify - Validates a env var name
* @name: String to be validated
*
* Return: 0, on success, -1 on error
*/
int env_name_verify(char *name)
{
if (name == NULL)
{
print_builtin_error("setenv: Needs 2 args", "");
return (-1);
}
if (!var_name_is_valid(name))
{
print_builtin_error("setenv: Invalid name ", name);
return (-1);
}
return (0);
}
/**
* var_name_is_valid - Checks if the env name is valid
* @name: String to be checked
*
* Return: 1 if is valid, 0 otherwise
*/
int var_name_is_valid(char *name)
{
int i;
for (i = 0; name[i] != '\0'; i++)
if (name[i] == '=')
return (0);
return (1);
}
/**
*get_env_postn - Check if a env variable exists and returns the index
* @name: Name to be searched
*
* Return: Index of the env var, -1 on error
*/
int get_env_postn(char *name)
{
int i;
int len = strlen(name);
for (i = 0; __environ[i] != NULL; i++)
if (strncmp(__environ[i], name, len) == 0)
return (i);
return (-1);
}