-
Notifications
You must be signed in to change notification settings - Fork 0
/
_split_line.c
51 lines (48 loc) · 1.2 KB
/
_split_line.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
#include "main.h"
/**
* _split_line - splits line to array of words.
* @line: the line to be splitted.
* @delimiter: delimiter.
*
* Return: the array of tokens.
*/
char **_split_line(char *line, char *delimiter)
{
char *line_copy, *token, **ptr_token;
int size = BUFFER_SIZE, i = 0;
/* Create a copy of the line and assigns it to a new memory location */
line_copy = _strdup(line);
/* Split the input line into individual tokens using delimiter arg */
token = strtok(line_copy, delimiter);
/* Allocate memory for an array to store the tokens */
ptr_token = malloc(sizeof(char *) * size);
if (ptr_token == NULL)
{
free(ptr_token);
free(token);
return (NULL);
}
/* Loop through the tokens and store them in the array */
while (token != NULL)
{
ptr_token[i] = token;
i++;
/* If we've filled up the array, resize it */
if (i >= size)
{
size += size;
ptr_token = _realloc(ptr_token, size,
(sizeof(char *) * size));
}
/* Get the next token */
token = strtok(NULL, delimiter);
}
/*
* Add a null terminator to the end of the array
* to be able to execute execve function later.
*/
ptr_token[i] = NULL;
/* Free allocated memory*/
free(token);
return (ptr_token);
}