-
Notifications
You must be signed in to change notification settings - Fork 0
/
_display_help.c
185 lines (173 loc) · 3.22 KB
/
_display_help.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "sshell.h"
/**
* read_help - reads all builtins text file and prints it to POSIX stdout
* @m: copy of environment variables
* Return: number of letters to write, otherwise 0.
*/
ssize_t read_help(char **m)
{
int fd, r, w;
char *buf;
char helpfiles[] = "/simple_shell/_helpfiles/help_all.txt";
char *home, *helpdir;
size_t letters = 1024;
buf = malloc((sizeof(char) * letters + 1));
if (buf == NULL)
return (0);
home = _gethome(m);
helpdir = str_concat(home, helpfiles);
fd = open(helpdir, O_RDONLY);
if (fd == -1)
{
free(helpdir);
free(buf);
return (0);
}
r = read(fd, buf, letters);
if (r == -1)
{
free(helpdir);
free(buf);
return (0);
}
w = write(STDOUT_FILENO, buf, r);
if (w == -1)
{
free(helpdir);
free(buf);
return (0);
}
close(fd);
free(helpdir);
free(buf);
return (r);
}
/**
* read_cdhelp - reads cd text file and prints it to POSIX stdout
* @m: copy of environment variables
* Return: number of letters to write, otherwise 0.
*/
ssize_t read_cdhelp(char **m)
{
int fd, r, w;
char *buf;
char helpfiles[] = "/simple_shell/_helpfiles/help_cd.txt";
char *home, *helpdir;
size_t letters = 1024;
buf = malloc((sizeof(char) * letters + 1));
if (buf == NULL)
return (0);
home = _gethome(m);
helpdir = str_concat(home, helpfiles);
fd = open(helpdir, O_RDONLY);
if (fd == -1)
{
free(helpdir);
free(buf);
return (0);
}
r = read(fd, buf, letters);
if (r == -1)
{
free(helpdir);
free(buf);
return (0);
}
w = write(STDOUT_FILENO, buf, r);
if (w == -1)
{
free(helpdir);
free(buf);
return (0);
}
close(fd);
free(helpdir);
free(buf);
return (r);
}
/**
* read_exithelp - reads exit text file and prints it to POSIX stdout
* @m: copy of environment variables
* Return: number of letters to write, otherwise 0.
*/
ssize_t read_exithelp(char **m)
{
int fd, r, w;
char *buf;
char helpfiles[] = "/simple_shell/_helpfiles/help_exit.txt";
char *home, *helpdir;
size_t letters = 1024;
buf = malloc((sizeof(char) * letters + 1));
if (buf == NULL)
return (0);
home = _gethome(m);
helpdir = str_concat(home, helpfiles);
fd = open(helpdir, O_RDONLY);
if (fd == -1)
{
free(helpdir);
free(buf);
return (0);
}
r = read(fd, buf, letters);
if (r == -1)
{
free(helpdir);
free(buf);
return (0);
}
w = write(STDOUT_FILENO, buf, r);
if (w == -1)
{
free(helpdir);
free(buf);
return (0);
}
close(fd);
free(helpdir);
free(buf);
return (r);
}
/**
* read_helphelp - reads help text file and prints it to POSIX stdout
* @m: copy of environment variables
* Return: number of letters to write, otherwise 0.
*/
ssize_t read_helphelp(char **m)
{
int fd, r, w;
char *buf;
char helpfiles[] = "/simple_shell/_helpfiles/help_help.txt";
char *home, *helpdir;
size_t letters = 1024;
buf = malloc((sizeof(char) * letters + 1));
if (buf == NULL)
return (0);
home = _gethome(m);
helpdir = str_concat(home, helpfiles);
fd = open(helpdir, O_RDONLY);
if (fd == -1)
{
free(helpdir);
free(buf);
return (0);
}
r = read(fd, buf, letters);
if (r == -1)
{
free(helpdir);
free(buf);
return (0);
}
w = write(STDOUT_FILENO, buf, r);
if (w == -1)
{
free(helpdir);
free(buf);
return (0);
}
close(fd);
free(helpdir);
free(buf);
return (r);
}