mirror of
https://github.com/Savapitech/42sh.git
synced 2026-01-18 16:57:28 +01:00
[ADD] repeat from that branches
This commit is contained in:
@@ -26,4 +26,5 @@ int builtins_cd(ef_t *ef, char **args);
|
||||
int builtins_builtins(ef_t *ef, char **args);
|
||||
int builtins_funny_double_dot(ef_t *ef, char **args);
|
||||
int builtins_history(ef_t *ef, char **args);
|
||||
int builtins_repeat(ef_t *ef, char **args);
|
||||
#endif /* BUILTIND_H */
|
||||
|
||||
46
src/builtins/repeat.c
Normal file
46
src/builtins/repeat.c
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
** EPITECH PROJECT, 2025
|
||||
** 42sh
|
||||
** File description:
|
||||
** repeat
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/wait.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include "builtins.h"
|
||||
#include "common.h"
|
||||
#include "exec.h"
|
||||
#include "u_str.h"
|
||||
|
||||
static
|
||||
bool checking_error(ef_t *ef, char **args, long *nb_loop)
|
||||
{
|
||||
char *end;
|
||||
|
||||
if (my_array_len(args) < 3)
|
||||
return (WRITE_CONST(STDERR_FILENO, "repeat: Too few arguments.\n"),
|
||||
true);
|
||||
*nb_loop = strtol(args[1], &end, 10);
|
||||
if (end[0] != '\0'){
|
||||
return (WRITE_CONST(STDERR_FILENO, "repeat: Badly formed number.\n"),
|
||||
true);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int builtins_repeat(ef_t *ef, char **args)
|
||||
{
|
||||
long nb_loop = 0;
|
||||
int status = 0;
|
||||
|
||||
if (checking_error(ef, args, &nb_loop))
|
||||
return RETURN_FAILURE;
|
||||
for (int i = 0; i < nb_loop; i++)
|
||||
status = exec_the_args(ef, &args[2]);
|
||||
return status;
|
||||
}
|
||||
19
src/exec.c
19
src/exec.c
@@ -32,6 +32,7 @@ const builtins_funcs_t BUILTINS[] = {
|
||||
{ "setenv", &builtins_setenv },
|
||||
{ "unsetenv", &builtins_unsetenv },
|
||||
{ ":", &builtins_funny_double_dot },
|
||||
{ "repeat", &builtins_repeat },
|
||||
{ "exit", &builtins_exit },
|
||||
{ "history", &builtins_history}
|
||||
};
|
||||
@@ -180,15 +181,11 @@ bool builtins_launcher(ef_t *ef, char **args)
|
||||
return false;
|
||||
}
|
||||
|
||||
int execute(ef_t *ef)
|
||||
int exec_the_args(ef_t *ef, char **args)
|
||||
{
|
||||
char *full_bin_path;
|
||||
char **args;
|
||||
int status;
|
||||
|
||||
args = parse_args(ef, ef->act_node, ef->env);
|
||||
if (!args)
|
||||
return RETURN_FAILURE;
|
||||
if (builtins_launcher(ef, args))
|
||||
return RETURN_SUCCESS;
|
||||
full_bin_path = parse_full_bin_path(ef->env, args[0]);
|
||||
@@ -199,6 +196,18 @@ int execute(ef_t *ef)
|
||||
status_handler(status);
|
||||
U_DEBUG("Exit code [%d]\n", ef->history->last_exit_code);
|
||||
free(full_bin_path);
|
||||
return RETURN_SUCCESS;
|
||||
}
|
||||
|
||||
int execute(ef_t *ef)
|
||||
{
|
||||
char **args;
|
||||
|
||||
args = parse_args(ef, ef->act_node, ef->env);
|
||||
if (!args)
|
||||
return RETURN_FAILURE;
|
||||
if (exec_the_args(ef, args))
|
||||
return RETURN_FAILURE;
|
||||
free((void *)args);
|
||||
return ef->exec_ctx->history->last_exit_code
|
||||
!= 0 ? RETURN_FAILURE : RETURN_SUCCESS;
|
||||
|
||||
@@ -48,6 +48,7 @@ typedef struct {
|
||||
|
||||
__attribute__((nonnull))
|
||||
int execute(ef_t *ef);
|
||||
int exec_the_args(ef_t *ef, char **args);
|
||||
int visit_loop(ef_t *ef, ast_t *node);
|
||||
char *handle_var_case(ast_t *node, env_t *env, size_t *i);
|
||||
#endif /* EXEC_H */
|
||||
|
||||
24
ulib/str/arraydup.c
Normal file
24
ulib/str/arraydup.c
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
** EPITECH PROJECT, 2025
|
||||
** minish
|
||||
** File description:
|
||||
** arraydup
|
||||
*/
|
||||
#include "u_str.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
char **arraydup(char **array)
|
||||
{
|
||||
char **dup_array = NULL;
|
||||
int i = 0;
|
||||
|
||||
if (array == NULL)
|
||||
return NULL;
|
||||
dup_array = malloc(sizeof(char *) * (my_array_len(array) + 1));
|
||||
if (dup_array == NULL)
|
||||
return NULL;
|
||||
for (; array[i] != NULL; i++)
|
||||
dup_array[i] = u_strdup(array[i]);
|
||||
dup_array[i] = NULL;
|
||||
return dup_array;
|
||||
}
|
||||
17
ulib/str/my_array_len.c
Normal file
17
ulib/str/my_array_len.c
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
** EPITECH PROJECT, 2024
|
||||
** Semester_1
|
||||
** File description:
|
||||
** my_array_len
|
||||
*/
|
||||
#include <unistd.h>
|
||||
|
||||
int my_array_len(char **tab)
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
if (tab == NULL)
|
||||
return count;
|
||||
for (; tab[count] != NULL; count++);
|
||||
return count;
|
||||
}
|
||||
@@ -35,5 +35,7 @@ bool u_str_is_alnum(char *str);
|
||||
bool u_str_is_only_alnum(char *str);
|
||||
void free_array(char **array);
|
||||
void puterror(char const *prefix);
|
||||
int my_array_len(char **tab);
|
||||
char **arraydup(char **array);
|
||||
|
||||
#endif /* STRING_H */
|
||||
|
||||
Reference in New Issue
Block a user