-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtokenizer.c
53 lines (49 loc) · 1.05 KB
/
tokenizer.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
#include "shell.h"
/**
* tokenizer - function that tokenizes a string
* @path: pointer to array of path to token from string string path
* @exit_status: exit status of old program
*
* Return: tokenized string
*/
char **tokenizer(char **path, int exit_status)
{
char *input_line = NULL, **tokens, *token;
size_t n = 0;
int i = 0;
if (isatty(STDIN_FILENO))
write(1, "$ ", 2);
if ((getline(&input_line, &n, stdin)) == -1)
{
free(input_line);
free_args(path);
exit(exit_status);
}
for (i = 0; input_line[i]; i++)
{
if (input_line[i] == '\n')
input_line[i] = '\0';
}
tokens = malloc(sizeof(char *) * (count_words(input_line) + 1));
if (!tokens)
{
perror("malloc");
return (NULL);
}
token = strtok(input_line, " ");
i = 0;
while ((token != NULL) && !(_strcmp(token, "#") == 0))
{
tokens[i] = malloc(sizeof(char) * (_strlen(token) + 1));
if (tokens[i] == NULL)
{
perror("malloc");
return (NULL);
}
_strcpy(tokens[i++], token);
token = strtok(NULL, " ");
}
tokens[i] = NULL;
free(input_line);
return (tokens);
}