-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_help.c
More file actions
95 lines (87 loc) · 1.7 KB
/
_help.c
File metadata and controls
95 lines (87 loc) · 1.7 KB
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
#include "shell.h"
#define BUFF_SIZE 32
/**
* read_line - print a line of fd
* @fd: file descriptor
* @line: line of text
* Return: execution of read_line: 1 if success
*/
int read_line(const int fd, char **line)
{
int reader;
char *tmp;
char buffer[BUFF_SIZE + 1];
static char *str[4864];
if (fd < 0 || BUFF_SIZE <= 0 || line == NULL || read(fd, buffer, 0) < 0)
return (-1);
while ((reader = read(fd, buffer, BUFF_SIZE)) > 0)
{
buffer[reader] = '\0';
if (!str[fd])
str[fd] = _strdup(buffer);
else
{
tmp = f_strjoin(str[fd], buffer);
free(str[fd]);
str[fd] = tmp;
}
if (_strchr(str[fd], '\n'))
break;
}
if (str[fd] == NULL && reader == 0)
return (-1);
return (f_read_line(str, line, fd));
}
/**
* f_read_line - read a line from fd
* @str: grip of documment
* @fd: file descriptor
* @line: line of text
* Return: 1 if success
*/
int f_read_line(char **str, char **line, int fd)
{
char *tmp;
int count;
count = 0;
while (str[fd][count] != '\0' && str[fd][count] != '\n')
count++;
*line = f_strsub(str[fd], 0, count);
if (str[fd][count] == '\n')
{
tmp = _strdup(&str[fd][count + 1]);
free(str[fd]);
str[fd] = tmp;
if (str[fd][0] == '\0')
f_strdel(&str[fd]);
}
else
f_strdel(&str[fd]);
return (1);
}
/**
* _help - functions that simulate help command
* @commands: array of strings to execute
* Return: 0 if is positive (1 is true min.); -1 if is error
*/
int _help(char **commands)
{
int fd, i = 1, result = -1;
char *line = NULL;
while (commands[i])
{
fd = open(commands[i], O_RDONLY);
if (fd != -1)
{
result = 0;
while (read_line(fd, &line) == 1)
{
puts(line);
free(line);
}
close(fd);
}
i++;
}
return (result);
}