-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path_getline.c
106 lines (87 loc) · 1.39 KB
/
_getline.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 "simishell.h"
/**
* _getchar - a function that reads a character from the standard input
*
* Return: returns the read character
*/
char _getchar(void)
{
char *buf;
char c;
int i = 0;
buf = malloc(2);
if (!buf)
return (-1);
fflush(stdout);
i = read(1, buf, 1);
if (i == -1)
{
perror("Reading Char");
return (-1);
}
else if (i == 0)
{
write(1, "\n", 2);
fflush(stdout);
exit(1);
}
c = *buf;
return (c);
}
/**
* _getline - a function to read a line from the standard input
* @line: a pointer to a pointer of location to save the string
* @len: the size of the characters read
*
* Return: returns the size of the read string
*/
int _getline(char **line, size_t *len)
{
size_t limit = 25;
char *tmp;
line[0] = malloc(25);
if (!line[0])
return (-1);
*len = 0;
while (line[0][*len - 1] != '\n')
{
line[0][*len] = _getchar();
*len += 1;
if (*len > (limit - 3))
{
tmp = realloc(line[0], limit + 10);
if (tmp)
line[0] = tmp;
else
perror("Reallocation");
limit += 10;
}
}
return (*len);
}
/**
* echoer - a function to echo back any text you gave it
* @line: an array of command and arguments
*
* Return: returns 1 in success and -1 if it fails
*/
int echoer(char *line[])
{
int i = 1;
if (line[i] == NULL)
{
write(1, "\n", 2);
return (1);
}
while (line[i] != NULL)
{
write(1, line[i], strleng(line[i]));
i++;
if (line[i] != NULL)
{
write(1, " ", 2);
}
}
write(1, "\n", 2);
return (1);
}