-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharray_tokens.c
More file actions
45 lines (42 loc) · 1017 Bytes
/
Copy patharray_tokens.c
File metadata and controls
45 lines (42 loc) · 1017 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
#include "main.h"
/**
* arrayTokens - makes an of of tokens
* @inputbuffer: command that is entered
* Return: a pointer to pointer
*/
char **arrayTokens(void)
{
char *inputbuffer = read_line();
char *copy_inputbuffer;
int nchars;
char **tokenArray;
char *token;
const char *delim = " \n";
int numtokens = 0, i;
nchars = _strlen(inputbuffer);
copy_inputbuffer = malloc(sizeof(char) * nchars);
if (copy_inputbuffer == NULL)
{
perror("memory allocation for copy_inputbuffer failed\n");
exit(EXIT_FAILURE);
}
strcpy(copy_inputbuffer, inputbuffer);
token = strtok(inputbuffer, delim);
while (token != NULL)
{
numtokens++;
token = strtok(NULL, delim);
}
numtokens++;
token = strtok(copy_inputbuffer, delim);
tokenArray = malloc(sizeof(char *) * numtokens);
for (i = 0; token != NULL; i++)
{
tokenArray[i] = malloc(sizeof(char) * _strlen(token));
strcpy(tokenArray[i], token);
token = strtok(NULL, delim);
}
tokenArray[i] = NULL;
free(copy_inputbuffer);
return (tokenArray);
}