-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.c
41 lines (35 loc) · 803 Bytes
/
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
#include "input.h"
#include <errno.h>
#include <input.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TURSH_TOKEN_MAX 32
char** tursh_input() {
char *line, *token;
size_t readlen, len = 0;
int index = 0;
char** argv = malloc(sizeof(char*) * TURSH_TOKEN_MAX);
/* get stdin */
readlen = getline(&line, &len, stdin);
if (readlen == -1) {
return NULL;
}
/* parse */
const char* delimiter = " \n";
token = strtok(line, delimiter);
if (!token) {
return NULL;
}
argv[index++] = token;
while (token) {
token = strtok(
NULL,
delimiter); /* strtok() expects NULL for 1st argument in 2nd run. */
argv[index++] = token;
}
/* Add NULL at the end of argv list. */
argv[index] = NULL;
return argv;
}