-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreak-input.c
59 lines (52 loc) · 1.13 KB
/
break-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
48
49
50
51
52
53
54
55
56
57
58
59
/*
* break_input.c - Tokenizer for Simpe shell project
* Author: Joana Casallas and Rodrigo Zárate A
* Date: August 22, 2021
*/
#include "simple_shell.h"
#define TOKEN_BUFFER 128
#define TOKEN_DELIMITATOR " \a\r\t\n"
/**
* break_input - Create tokens
* @input: char pointer
* Return: char double pointer
*/
char **break_input(char *input)
{
int buffertoken = TOKEN_BUFFER, position = 0;
char **alltokens = malloc(buffertoken * sizeof(char *));
char **talltok;
char *token;
if (!alltokens)
{
free(alltokens);
exit(EXIT_FAILURE);
}
token = strtok(input, TOKEN_DELIMITATOR);
while (token != NULL)
{
alltokens[position] = token;
position++;
if (position >= buffertoken)
{
buffertoken += TOKEN_BUFFER;
talltok = malloc(buffertoken * sizeof(char *));
talltok = alltokens;
free(alltokens);
/* new size */
alltokens = malloc(buffertoken * sizeof(char *));
alltokens = talltok;
free(talltok);
if (!alltokens)
{
free(alltokens);
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, TOKEN_DELIMITATOR);
}
/* end all now */
alltokens[position] = NULL;
/* send to execute command */
return (alltokens);
}