-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_strtok_r.c
More file actions
38 lines (33 loc) · 1.02 KB
/
_strtok_r.c
File metadata and controls
38 lines (33 loc) · 1.02 KB
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
#include "shell.h"
/**
* _strtok_r - returns a bit of a line
* @result: where to put the result
* @line: line to extract part from, always malloc'ed
* @delim: string of all delimeters to chop the line with
* @remain: the rest of the line to consider, it is always malloc'd
* Each time I get out I free the line and malloc the remain if need be
* It is necessary to obtain line through a previous malloc
* Return: a pointer to the part of the line to consider
*/
char *_strtok_r(char **result, char *line, char *delim, char **remain)
{
int index1, index2;
if (!result || !remain)
return (NULL);
if (line == NULL)
line = *remain;
index1 = _strspn(line, delim); /*get rid of junk in the beginning*/
index2 = _strcspn(line + index1, delim);
if (*(line + index1 + index2) && *(line + index1 + index2 + 1))
{
*remain = line + index1 + index2 + 1;
}
else
{
*remain = NULL;
}
*result = malloc(sizeof(char) * (index2 + 2));
*result = _memcpy(*result, line + index1, index2);
(*result)[index2] = '\0';
return (*result);
}