-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgsh_execute.c
More file actions
153 lines (141 loc) · 3.41 KB
/
gsh_execute.c
File metadata and controls
153 lines (141 loc) · 3.41 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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* gsh_execute.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dlinkin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/01/21 18:10:13 by dlinkin #+# #+# */
/* Updated: 2018/01/21 18:10:14 by dlinkin ### ########.fr */
/* */
/* ************************************************************************** */
#include "gsh_core.h"
/*
** CHECK COMMAND NAMES 'ERE
*/
const char *g_sig[] = {
"SIGHUP",
"SIGINT",
"gsh: quit ",
"gsh: illegal instruction ",
"gsh: trace trap ",
"gsh: abort program ",
"gsh: emulate instruction executed ",
"gsh: floating-point exception ",
"gsh: killed >:[ ",
"gsh: bus error ",
"gsh: segmentation violation ",
"gsh: non-existent system call invoked ",
"gsh: broken pipe ",
"SIGALRM",
"SIGTERM",
"SIGURG",
"SIGSTOP",
"SIGTSTP",
"SIGCONT",
"SIGCHLD",
"SIGTTIN",
"SIGTTOU",
"SIGIO",
"SIGXCPU",
"SIGXFSZ",
"SIGVTALRM",
"SIGPROF",
"SIGWINCH",
"SIGINFO",
"SIGUSR1",
"SIGUSR2",
NULL
};
int gsh_get_path_name(char *out, char *path, char *name)
{
char *tmp;
int ret;
ft_bzero((void *)out, NAMESIZE);
if (!(tmp = ft_strchr(path, ':')))
{
ret = ft_strlen(path);
ft_strncpy(out, path, ret);
}
else
{
if (*path == 0)
return (0);
ft_strncpy(out, path, tmp - path);
ret = tmp - path;
}
ft_strcat(out, "/");
ft_strcat(out, name);
return (ret);
}
int gsh_exit_status(int status, char *name)
{
extern const char *g_sig[32];
int i;
if (WIFEXITED(status))
return (WEXITSTATUS(status));
if (WIFSIGNALED(status))
{
if (WTERMSIG(status))
{
i = WTERMSIG(status);
if (i > 2 && i < 33)
ft_dprintf(2, "%s%s\n", g_sig[WTERMSIG(status) - 1], name);
return (128 + WTERMSIG(status));
}
if (WCOREDUMP(status))
return (128 + WCOREDUMP(status));
}
if (WIFSTOPPED(status))
return (128 + WSTOPSIG(status));
return (0);
}
int gsh_execute(char *cmd, char **av)
{
if (!access(cmd, F_OK))
{
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
signal(SIGTSTP, SIG_IGN);
execve(cmd, av, gsh_bucket(RETURN_ENV, 0));
ft_dprintf(2, "|< permission denied: %s\n", cmd);
return (126);
}
ft_dprintf(2, "|< command not found: %s\n", cmd);
return (127);
}
int gsh_launch_cmnd(char *cmd, char **av)
{
int id;
int st;
id = fork();
if (id == 0)
exit(gsh_execute(cmd, av));
else if (id > 0)
{
waitpid(id, &st, 0);
kill(0, 0);
return (gsh_exit_status(st, cmd));
}
write(2, "|< Alarma!! Can't make fork!\n", 29);
return (1);
}
int gsh_os_cmd(char **cmd, char *path)
{
char name[NAMESIZE];
char *str;
int i;
extern uint32_t g_opt_n;
if ((g_opt_n & USE_HASH) && (str = gsh_hash_search(*cmd)))
return (gsh_launch_cmnd(str, cmd));
while ((i = gsh_get_path_name(name, path, *cmd)))
{
if (!access(name, F_OK))
return (gsh_launch_cmnd(name, cmd));
path += i;
if (*path)
path++;
}
ft_dprintf(2, "|< command not found: %s\n", *cmd);
return (127);
}