-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_input.c
More file actions
41 lines (36 loc) · 796 Bytes
/
Copy pathparse_input.c
File metadata and controls
41 lines (36 loc) · 796 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
#include "main.h"
/**
* parse_input - parses input
* @input: input string
* @delim: deliminator
* @num_tokens: number of tokens
*
* Return: double pointer
*/
char **parse_input(char *input, const char *delim, int *num_tokens)
{
char *token, *input_copy;
int i;
char **argv;
input_copy = _strdup(input);
if (!input_copy)
error("string duplication error");
*num_tokens = 0;
token = _strtok(input_copy, delim);
while (token)
{
(*num_tokens)++;
token = _strtok(NULL, delim);
}
argv = safe_malloc(sizeof(char *) * ((*num_tokens) + 1));
token = _strtok(input, delim);
for (i = 0; token; i++)
{
argv[i] = safe_malloc(sizeof(char) * (strlen(token) + 1));
strcpy(argv[i], token);
token = _strtok(NULL, delim);
}
argv[i] = NULL;
free(input_copy);
return (argv);
}