signaux fonctionnent sur test

This commit is contained in:
hugogogo
2021-11-15 16:09:19 +01:00
parent 47ae67ed14
commit 57c271b9e2
4 changed files with 65 additions and 9 deletions

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
/* Updated: 2021/11/11 09:58:01 by hulamy ### ########.fr */
/* Updated: 2021/11/12 10:45:33 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -40,6 +40,7 @@ int builtin_unset(int argc, char *argv[], t_all *c);
int builtin_exit(int argc, char *argv[], t_all *c);
int builtin_env(int argc, char *argv[], t_all *c);
int builtin_echo(int argc, char *argv[], t_all *c);
int getenv_position(char **envp, char *name);
// Free
int free_exit(t_all *c, int exit_status);

View File

@@ -22,10 +22,14 @@ int builtin_export(int argc, char *argv[], t_all *c)
var = ft_split(argv[1], '=');
position = getenv_position(c->envp, var[0]);
if (position == -1 || !ft_strchr(argv[1], '='))
{
ft_free_2d_arr(var);
return (0);
}
c->envp[position] = ft_strjoin(var[0], "=");
if (var[1]) // parce que ft_strjoin return NULL si var[1] est null, pourquoi ?
c->envp[position] = ft_strjoin(c->envp[position], var[1]);
c->envp[position] = ft_strjoinfree_s1(c->envp[position], var[1]);
ft_free_2d_arr(var);
return (0);
}
@@ -41,7 +45,6 @@ int builtin_export(int argc, char *argv[], t_all *c)
** > 'export VARIABLE' n'exporte rien
** > 'export VARIABLE=' exporte une variable vide
** 4. ordre d'insertion d'une nouvelle variable :
**
**
** > aucune idee
*/

View File

@@ -3,8 +3,21 @@
int builtin_unset(int argc, char *argv[], t_all *c)
{
int position;
(void)argc;
(void)argv;
(void)c;
position = getenv_position(c->envp, argv[1]);
if (position == -1)
return (0);
while (c->envp[position])
{
write(1, "1", 1);
free(c->envp[position]);
c->envp[position] = ft_strdup(c->envp[position + 1]);
position++;
}
write(1, "2", 1);
free(c->envp[position]);
return (0);
}

View File

@@ -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)
{