This commit is contained in:
Loic Deridder
2025-01-21 13:42:32 +01:00
parent 563349c2f8
commit 9dbc3f75e8
8 changed files with 149 additions and 79 deletions

View File

@@ -1,27 +1,20 @@
#include "../../includes/builtins.h"
int is_silentchar(char c)
{
if (c == 'n' || c == '-' || c == ' ')
return (1);
else
return (0);
}
int is_silent(char *str)
{
int i;
int i;
bool flag;
i = 4;
while (str[i] && str[i] == ' ')
i = 0;
flag = false;
if (str[i] && str[i] != '-')
return (flag);
i++;
while (str[i] && str[i] == 'n')
i++;
if (str[i] && str[i] == '-')
{
i++;
if (str[i] && str[i] == 'n')
return (1);
}
return (0);
if (!str[i])
flag = true;
return (flag);
}
char *ft_getenv(char *str, char **envp)
@@ -59,33 +52,44 @@ int extractenv(char *str, char **envp)
return (i);
}
static void echo_print(char *str, char **envp)
static void echo_print(char **arg, int j, char **envp)
{
int i;
i = 0;
while (str[i])
while (arg[j])
{
if (str[i] == '$')
i = 0;
while (arg[j][i])
{
if (!str[i + 1] || str[i + 1] == ' ')
ft_put_c(str[i++]);
if (arg[j][i] == '$')
{
if (!arg[j][i + 1] || arg[j][i + 1] == ' ')
ft_put_c(arg[j][i++]);
else
i += extractenv(&arg[j][i], envp);
}
else
i += extractenv(&str[i], envp);
ft_put_c(arg[j][i++]);
}
else
ft_put_c(str[i++]);
j++;
if (arg[j])
ft_put_c(' ');
}
}
void builtin_echo(char *arg, char **envp)
void builtin_echo(char **arg, char **envp)
{
int i;
int i;
bool flag;
i = 4;
while (arg[i] && is_silentchar(arg[i]))
flag = false;
i = 1;
while (arg[i] && is_silent(arg[i]))
{
flag = true;
i++;
echo_print(&arg[i], envp);
if (!is_silent(arg))
}
echo_print(arg, i, envp);
if (!flag)
printf("\n");
}

View File

@@ -82,6 +82,10 @@ void builtin_exit(char *str, bool depth)
char *arg;
arg = get_arg(str);
long res = ft_atol(arg);
(void)res;
if (errno == ERANGE)
ft_printf("error numeric aarg\n");
if (depth == true)
{
if (count_arg(str) >= 2)
@@ -96,12 +100,12 @@ void builtin_exit(char *str, bool depth)
ft_putendl_fd(": numeric argument required", 2);
exit(2);
}
exit(ft_atoi(arg));
exit(ft_atoi(arg) % 256);
}
if (count_arg(str) >= 2)
bash_exiterrorcount();
else if (ft_isnumeric(arg))
bash_exit(ft_atoi(arg));
bash_exit(ft_atoi(arg) % 256);
else
bash_exit_errornum(arg);
}

View File

@@ -1,47 +1,5 @@
#include "../../includes/builtins.h"
void print_arr(char **envp)
{
int i;
char *equal;
(void)equal;
i = -1;
while (envp[++i])
{
ft_printf("declare -x %s\n", envp[i]);
}
}
void print_export(char **envp)
{
int i;
int j;
char *tmp;
int len;
i = 0;
len = 0;
while (envp[len])
len++;
while (i < len - 1)
{
j = 0;
while (j < len - i - 1)
{
if (ft_strncmp(envp[j], envp[j + 1], ft_strchr(envp[j], '=') - envp[j]) > 0)
{
tmp = envp[j];
envp[j] = envp[j + 1];
envp[j + 1] = tmp;
}
j++;
}
i++;
}
print_arr(envp);
}
static void free_tmp(char **tab)
{
int i;
@@ -70,6 +28,53 @@ char **key_value(char *str)
return (tmp);
}
void print_arr(char **envp)
{
int i;
char *equal;
char **print;
(void)equal;
i = -1;
while (envp[++i])
{
print = key_value(envp[i]);
if (print[1])
ft_printf("declare -x %s=\"%s\"\n", print[0], print[1]);
else
ft_printf("declare -x %s\n", print[0]);
free_tmp(print);
}
}
void print_export(char **envp)
{
int i;
int j;
char *tmp;
int len;
i = -1;
len = 0;
while (envp[len])
len++;
while (++i < len - 1)
{
j = 0;
while (j < len - i - 1)
{
if (ft_strncmp(envp[j], envp[j + 1], ft_strchr(envp[j], '=') - envp[j]) > 0)
{
tmp = envp[j];
envp[j] = envp[j + 1];
envp[j + 1] = tmp;
}
j++;
}
}
print_arr(envp);
}
void builtin_export(char **arg, t_data *data)
{
int i;

7
srcs/env/var.c vendored
View File

@@ -100,6 +100,13 @@ int get_var_index(char *key, t_data *data)
free_null_ptr(new_key);
return (i);
}
else if (ft_strncmp(data->env[i], key, ft_strlen(key)) == 0
&& (ft_strchr(data->env[i], '=') == NULL &&
(ft_strlen(data->env[i]) == ft_strlen(key))))
{
free_null_ptr(new_key);
return (i);
}
i++;
}
free_null_ptr(new_key);

View File

@@ -67,7 +67,7 @@ int main(int ac, char **av, char **envp)
if (ft_strncmp(input, "pwd", 3) == 0)
builtin_pwd(input);
if (ft_strncmp(input, "echo", 4) == 0)
builtin_echo(input, data->env);
builtin_echo(ft_split(input, " "), data->env);
if (ft_strncmp(input, "env", 3) == 0)
builtin_env(input, data->env);
if (ft_strncmp(input, "unset", 5) == 0)