-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle_env.c
More file actions
47 lines (42 loc) · 776 Bytes
/
handle_env.c
File metadata and controls
47 lines (42 loc) · 776 Bytes
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
#include "shell.h"
/**
* word_count - counts the tokenised string
* @av: the string
* @args: the token
* Return: the count
*/
int word_count(char *av, char ***args)
{
int count = 0, i;
char *token;
char **tokens = malloc((MAX_ARGS + 1) * sizeof(char *));
if (tokens == NULL)
return (-1);
token = strtok(av, " \t\r\n");
while (token != NULL && count < MAX_ARGS)
{
(tokens)[count] = _strdup(token);
token = strtok(NULL, " \t\r\n");
count++;
}
tokens[count] = NULL;
*args = tokens;
for (i = 0; i < count; i++)
free(tokens[i]);
free(tokens);
return (count);
}
/**
* free_environ - frees the environ
* @env: the eniron to be freed
*/
void free_environ(char **env)
{
int i = 0;
while (env[i] != NULL)
{
free(env[i]);
i++;
}
free(env);
}