-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_token.c
45 lines (42 loc) · 923 Bytes
/
fetch_token.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
#include "shell.h"
/**
* fetch_token - get tokens from stream
*
* Return: 0 if successful; 1 otherwise
*/
int fetch_token(void)
{
FILE *instream = NULL;
int numchar;
size_t len = MAXLEN;
char **lineptr = malloc(sizeof(char) * MAXLEN);
char **token_str = malloc(sizeof(char) * MAXLEN);
char *exit_str = "exit";
char *delim = " ,;\t";
instream = fdopen(STDIN, "r");
if (instream == NULL)
{
perror("Fdopen Error");
return (EXIT_FAILURE);
}
while ((numchar = getline(lineptr, &len, instream)) != -1
&& numchar != EOF)
{
while ((*token_str = strtok(*lineptr++, delim)) != NULL)
{
if (strncmp(*token_str, exit_str, strlen(exit_str))
== 0)
_exit(EXIT_SUCCESS);
if ((which_command(*token_str)) == EXIT_FAILURE)
{
perror("Which Error");
return (EXIT_FAILURE);
}
}
printf("%s", PROMPT);
}
free(lineptr);
free(token_str);
fclose(instream);
return (EXIT_SUCCESS);
}