-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.c
58 lines (48 loc) · 1017 Bytes
/
parser.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include "shell.h"
/**
* parse_input - takes input from stdin and tokenizes
* it into an array of arguments
* @input: a buffer that contains the input from stdin
* @input_copy: a buffer for storing arguments.
* @argv: an array of arguments
* Return: the number of arguments parsed
*/
int parse_input(char *input, char *input_copy, char **argv)
{
char *arg;
int argc = 0, i = 0;
arg = strtok(input, " \n");
while (arg != NULL)
{
argc++;
arg = strtok(NULL, " \n");
}
argc++;
argv = malloc(sizeof(char *) * argc);
if (argv == NULL)
{
free(argv);
exit(EXIT_FAILURE);
}
arg = strtok(input_copy, " \n");
for (i = 0; arg != NULL; i++)
{
argv[i] = malloc(sizeof(char) * _strlen(arg));
if (argv[i] == NULL)
{
free(argv[i]);
exit(EXIT_FAILURE);
}
strcpy(argv[i], arg);
arg = strtok(NULL, " \n");
}
argv[i] = NULL;
execute_command(argv);
free(argv);
return (i);
}