-
Notifications
You must be signed in to change notification settings - Fork 0
/
3parse_input.c
47 lines (46 loc) · 1.01 KB
/
3parse_input.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
#include "shell.h"
/**
* parse_input - function to divide the arguments
* into different strings with tokens
* @buf: pointer to a string of the command
* Return: a variable contain the command
*/
char **parse_input(char *buf)
{
char **all_tokens = NULL;
char *token = NULL;
int j = 0;
int counter = 0;
int x;
if (!buf)
return (all_tokens);
buf[_strlen(buf) - 1] = '\0';
/* Checking number of delimiters to know how many*/
/* spaces will be open in the tokenazation*/
for (x = 0; buf[x] != '\0'; x++)
{
if (buf[x] != ' ')/* si el caracter es valido*/
{
counter++;
/*if (buf[x + 1] == ' ' || buf[x + 1] == '\0');*/
}
}
/*printf("valor del counter %d \n", counter);*/
if (counter == 0)
{
return (all_tokens);
}
all_tokens = malloc(sizeof(char *) * (counter + 1));
if (!all_tokens)
{
return (all_tokens);
}
token = strtok(buf, "WHITESPACE ");
for (j = 0; token != NULL; j++)
{
all_tokens[j] = token;
token = strtok(NULL, "WHITESPACE ");
}
all_tokens[j] = NULL;
return (all_tokens);
}