-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_token.c
More file actions
42 lines (37 loc) · 929 Bytes
/
Copy path_token.c
File metadata and controls
42 lines (37 loc) · 929 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
#include "shell.h"
/**
* _getstoken - get the token from tokenized strings.
* @lineptre: command entered by the user to be tokenized
* Return: pointer to an array of tokens
*
* Description: This function takes a command entered by the user and tokenizes
* it based on spaces, newlines, tabs, and carriage returns.
* It returns an array of pointers to the tokens.
*/
char **_getstoken(char *lineptre)
{
size_t j = 0;
int size = 0;
char **user_cmd = NULL;
char *token = NULL;
if (lineptre == NULL)
return (NULL);
for (j = 0; lineptre[j]; j++)
{
if (lineptre[j] == ' ')
size++;
}
if ((size + 1) == _strlen(lineptre))
return (NULL);
user_cmd = malloc(sizeof(char *) * (size + 2));
if (user_cmd == NULL)
return (NULL);
token = _strtok(lineptre, " \n\t\r");
for (j = 0; token != NULL; j++)
{
user_cmd[j] = token;
token = _strtok(NULL, " \n\t\r");
}
user_cmd[j] = NULL;
return (user_cmd);
}