-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_getntokenise.c
53 lines (49 loc) · 1.02 KB
/
_getntokenise.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
#include "main.h"
/**
* getntok - get string and tokenise to an array of strings
* @mpath: pointer to array of mpath to clean
* @exit_stat: exit status of previous program
*
* Return: array of input strings
*/
char **getntok(char **mpath, int exit_stat)
{
char *lineptr = NULL, **tokens, *token;
size_t n = 0;
int i = 0;
if (isatty(STDIN_FILENO))
write(1, "$ ", 2);
if ((getline(&lineptr, &n, stdin)) == -1)
{
free(lineptr);
free_arr(mpath);
exit(exit_stat);
}
for (i = 0; lineptr[i]; i++)
{
if (lineptr[i] == '\n')
lineptr[i] = '\0';
}
tokens = malloc(sizeof(char *) * (wordcount(lineptr) + 1));
if (tokens == NULL)
{
perror("malloc");
return (NULL);
}
token = strtok(lineptr, " ");
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(lineptr);
return (tokens);
}