signaux fonctionnent sur test
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
# include <unistd.h>
|
||||
# include <readline/readline.h>
|
||||
# include <signal.h>
|
||||
# include <sys/wait.h>
|
||||
|
||||
void handler_sigint(int id)
|
||||
{
|
||||
@@ -12,16 +13,24 @@ void handler_sigint(int id)
|
||||
rl_redisplay();
|
||||
}
|
||||
|
||||
void handler_sigint_execution(int id)
|
||||
{
|
||||
(void)id;
|
||||
write(1, "\n", 1);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[], char *envp[])
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
char *line_input;
|
||||
char *prompt;
|
||||
pid_t pid;
|
||||
|
||||
signal(SIGINT, handler_sigint);
|
||||
signal(SIGINT, handler_sigint); // signal handling for SINGINT while interactive mode
|
||||
line_input = NULL;
|
||||
prompt = strdup("\e[0;31mtest>\e[0m ");
|
||||
prompt = strdup("\e[0;31m>\e[0m ");
|
||||
while (1)
|
||||
{
|
||||
if (line_input)
|
||||
@@ -29,8 +38,38 @@ int main(int argc, char *argv[], char *envp[])
|
||||
line_input = readline(prompt);
|
||||
if (line_input && *line_input)
|
||||
{
|
||||
write(1, line_input, strlen(line_input));
|
||||
write(1, "\n", 1);
|
||||
signal(SIGINT, SIG_IGN); // stop signal handling for SIGINT because end of interactive mode
|
||||
pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
signal(SIGINT, handler_sigint_execution); // start signal handling for SIGINT in child process
|
||||
if (!strncmp("echo", line_input, 4))
|
||||
{
|
||||
write(1, "3", 2);
|
||||
sleep(1);
|
||||
write(1, "\b2", 2);
|
||||
sleep(1);
|
||||
write(1, "\b1", 2);
|
||||
sleep(1);
|
||||
write(1, "\b0\n", 3);
|
||||
execve("/bin/echo", argv, envp);
|
||||
}
|
||||
else
|
||||
{
|
||||
write(1, line_input, strlen(line_input));
|
||||
write(1, " 3", 2);
|
||||
sleep(1);
|
||||
write(1, "\b2", 2);
|
||||
sleep(1);
|
||||
write(1, "\b1", 2);
|
||||
sleep(1);
|
||||
write(1, "\b0\n", 3);
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
else
|
||||
wait(0);
|
||||
signal(SIGINT, handler_sigint); // restart signal handling for SIGINT because re-entring in interactive mode
|
||||
}
|
||||
else if (!line_input)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user