builtins
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
# include <string.h>
|
||||
|
||||
// void echo(char *msg, int flag);
|
||||
void builtin_echo(char *arg, char **envp);
|
||||
void builtin_echo(char **arg, char **envp);
|
||||
void builtin_exit(char *arg, bool depth);
|
||||
void builtin_pwd(char *arg);
|
||||
void builtin_env(char *str, char **envp);
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
# include <unistd.h>
|
||||
# include <limits.h>
|
||||
# include <stdarg.h>
|
||||
# include <errno.h>
|
||||
|
||||
# include "colors.h"
|
||||
# include "general.h"
|
||||
@@ -53,6 +54,7 @@ int ft_toupper(int c);
|
||||
int ft_tolower(int c);
|
||||
|
||||
int ft_atoi(const char *str);
|
||||
long ft_atol(const char *str);
|
||||
char *ft_itoa(int n);
|
||||
|
||||
void *ft_calloc(size_t count, size_t size);
|
||||
|
||||
48
lib/libft/srcs/format/ft_atol.c
Normal file
48
lib/libft/srcs/format/ft_atol.c
Normal file
@@ -0,0 +1,48 @@
|
||||
#include "../../libft.h"
|
||||
|
||||
static int is_signed(char c, int *sign)
|
||||
{
|
||||
if (c == '+' || c == '-')
|
||||
{
|
||||
if (c == '-')
|
||||
*sign *= -1;
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
long ft_atol(const char *str)
|
||||
{
|
||||
long res;
|
||||
int sign;
|
||||
|
||||
res = 0;
|
||||
sign = 1;
|
||||
errno = 0;
|
||||
while (*str && ((*str >= 9 && *str <= 13) || *str == 32))
|
||||
str++;
|
||||
if (is_signed(*str, &sign))
|
||||
str++;
|
||||
while (*str >= '0' && *str <= '9')
|
||||
{
|
||||
if (((sign > 0) && res >= (LONG_MAX / 10) &&
|
||||
(*str - '0' > LONG_MAX % 10))
|
||||
|| (sign == -1 && res >= LONG_MAX / 10 && *str - '0' > LONG_MAX % 10 + 1))
|
||||
{
|
||||
errno = ERANGE;
|
||||
break;
|
||||
}
|
||||
res = (res * 10) + (*str - '0');
|
||||
str++;
|
||||
}
|
||||
return (res * sign);
|
||||
}
|
||||
//
|
||||
// #include <stdio.h>
|
||||
//
|
||||
// int main(int ac, char **av)
|
||||
// {
|
||||
// printf("res: %ld\n", ft_atol(av[1]));
|
||||
// printf("errno: %d\n", errno);
|
||||
//
|
||||
// }
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
7
srcs/env/var.c
vendored
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user