-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_parse.c
106 lines (92 loc) · 1.64 KB
/
read_parse.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include "main.h"
/**
* error_stat - returns the error stat according to input
* @code: determines if we will take input stored or store a new one
*
* Return: the error status
*/
int error_stat(int code)
{
static int status;
if (code == -16) /*this is the code to extract the value stored*/
return (status);
status = code;
return (code);
}
/**
* recieve_input - recieves input from the user
*
* Return: a string full on input
*/
char *recieve_input(void)
{
size_t len = 1024;
char *str = malloc(1024);
int input_len;
if (!str)
{
perror("Error: couldn't allocate memory\n");
return (NULL);
}
input_len = getline(&str, &len, stdin);
if (input_len == -1) /*basically EOF*/
{
if (isatty(STDIN_FILENO))
write(1, "\n", 1);
_alias(NULL, 0);
free(str);
free_grid(environ);
exit(error_stat(-16));
}
str[input_len - 1] = '\0';
if (strlen(str) != 0)
_alias(&str, 1);
return (str);
}
/**
* toker - creates tokens out of the a string
* @str: a string to be tokonized
*
* Return: an array of pointers
*/
char **toker(char *str)
{
int i = 0;
char **arr = malloc(sizeof(*arr) * 10);
while (1)
{
if (i == 0)
{
arr[i] = strtok(str, " \t");
}
else
{
arr[i] = strtok(NULL, " \t");
}
if (arr[i] == NULL)
break;
i++;
}
return (arr);
}
/**
* is_input_eof - make sure if we reached eof in non interactive mode
*
* Return: Nothing.
*/
int is_input_eof(void)
{
int c;
c = fgetc(stdin);
if (c == -1)
return (1);
ungetc(c, stdin);
return (0);
}