-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf_builtin_utils.c
More file actions
59 lines (51 loc) · 1.06 KB
/
f_builtin_utils.c
File metadata and controls
59 lines (51 loc) · 1.06 KB
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"
/**
* validate_env_name - Validates a env var name
* @name: String to be validated
*
* Return: 0, on success, -1 on error
*/
int validate_env_name(char *name)
{
if (name == NULL)
{
print_builtin_error("setenv: Needs 2 args", "");
return (-1);
}
if (!is_valid_env_var_name(name))
{
print_builtin_error("setenv: Invalid name ", name);
return (-1);
}
return (0);
}
/**
* is_valid_env_var_name - Checks if the env name is valid
* @name: String to be checked
*
* Return: 1 if is valid, 0 otherwise
*/
int is_valid_env_var_name(char *name)
{
int i;
/* Verify that name is a valid env_var name (without "=") */
for (i = 0; name[i] != '\0'; i++)
if (name[i] == '=')
return (0);
return (1);
}
/**
* get_env_index - 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_index(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);
}