-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
55 lines (43 loc) · 939 Bytes
/
main.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
#include <sys/wait.h>
#include <sys/queue.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "dat.h"
struct token_list {
SIMPLEQ_ENTRY(token_list) next;
char token[BUFFER_SIZE];
};
SIMPLEQ_HEAD(, token_list) head;
static void
add_node(char *token)
{
struct token_list *p;
if ((p = malloc(sizeof(*p))) == NULL)
perror("Failed to allocate node\n");
strlcpy(p->token, token, BUFFER_SIZE);
SIMPLEQ_INSERT_TAIL(&head, p, next);
}
int
main(void)
{
char *delimiters = " \t";
char *inputstring;
char *word[BUFFER_SIZE];
*word = NULL;
struct token_list *p;
SIMPLEQ_INIT(&head);
while ((inputstring = readline("sh34: ")) != NULL) {
while ((*word = strsep(&inputstring, delimiters)) != NULL) {
add_node(*word);
}
*word = NULL;
SIMPLEQ_FOREACH(p, &head, next) {
puts(p->token);
}
free(inputstring);
}
}