-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandlers.c
More file actions
97 lines (93 loc) · 1.97 KB
/
handlers.c
File metadata and controls
97 lines (93 loc) · 1.97 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
96
97
#include "holberton.h"
/**
* exit_handler - function exit correctly
* @argvs: arguments with exit
* @env: environment variables
* @line: actual line intrduced by user
* @status: status previus command
*/
void exit_handler(char **argvs, char **env, char *line, int status)
{
(void)env;
if (argvs[1] == NULL)
{
free(argvs);
free(line);
exit(status);
}
else
{
status = _atoi(argvs[1]);
if (status > 0)
exit(status);
else
{
write(STDERR_FILENO, "./hsh", 5);
write(STDERR_FILENO, ": 1: exit: Illegal number: ", 27);
write(STDERR_FILENO, argvs[1], _strlen(argvs[1]));
write(STDERR_FILENO, "\n", 1);
free(argvs);
free(line);
exit(2);
}
}
}
/**
* env_handler - function print env correctly
* @argvs: arguments with exit
* @env: environment variables
* @line: actual line intrduced by user
* @status: status previus command
*/
void env_handler(char **argvs, char **env, char *line, int status)
{
int iterator = 0;
(void)argvs;
(void)line;
(void)status;
while (env[iterator])
{
write(STDOUT_FILENO, env[iterator], _strlen(env[iterator]));
write(STDOUT_FILENO, "\n", 1);
iterator++;
}
}
/**
* help_handler - function print hep builds correctly
* @argvs: arguments with exit
* @env: environment variables
* @line: actual line intrduced by user
* @status: status previus command
*/
void help_handler(char **argvs, char **env, char *line, int status)
{
char *p = NULL, *path = NULL, *full_path = NULL;
int index = 0;
struct stat st;
(void) line;
(void) status;
p = get_value_env(env, "_");
index = get_index_last_char(p, '/');
p = str_copy_index(p, index - 1);
path = str_concat(p, "/help/");
free(p);
if (argvs[1])
full_path = str_concat(path, argvs[1]);
else
{
full_path = str_concat(path, argvs[0]);
}
if (stat(full_path, &st) == 0)
{
read_file(full_path);
}
else
{
free(full_path);
full_path = str_concat(path, argvs[0]);
read_file(full_path);
}
write(STDOUT_FILENO, "\n", 1);
free(path);
free(full_path);
}