Skip to content

Commit

Permalink
norminating
Browse files Browse the repository at this point in the history
  • Loading branch information
hgoncalv committed Dec 29, 2022
1 parent 57626c7 commit 8908fa4
Show file tree
Hide file tree
Showing 38 changed files with 543 additions and 133 deletions.
Binary file removed .DS_Store
Binary file not shown.
5 changes: 0 additions & 5 deletions TODO

This file was deleted.

7 changes: 0 additions & 7 deletions out

This file was deleted.

Binary file removed srcs/.DS_Store
Binary file not shown.
113 changes: 113 additions & 0 deletions srcs/builtins.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* built_ins.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hgoncalv <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/22 15:19:25 by hgoncalv #+# #+# */
/* Updated: 2022/12/22 15:34:22 by hgoncalv ### ########.fr */
/* */
/* ************************************************************************** */

#include "../include/minishell.h"

int ft_cd(char **argv)
{
char *folder;
int to_free;
char pwd[256];
int ret;

if (argv[1] && argv[2])
{
printf("cd: too many arguments\n");
return (1);
}
if (getcwd(pwd, sizeof(pwd)) == NULL)
perror("getcwd() error");
to_free = set_cd_folder_return_if_free_folder_or_not(argv, &folder);
ret = chdir(folder);
if (ret != 0)
perror("minishell");
ft_setenv("OLDPWD", pwd, 1);
if (getcwd(pwd, sizeof(pwd)) == NULL)
perror("getcwd() error");
ft_setenv("PWD", pwd, 1);
if (to_free)
free(folder);
return (ret * (-1));
}

int ft_echo(char **argv)
{
int i;
int flag;

flag = 0;
i = 1;
while (argv[i] != NULL && ft_strexact(argv[i], "-n"))
i++;
if (ft_strexact(argv[i - 1], "-n"))
flag = 1;
while (argv[i] != NULL)
{
ft_putstr_fd(argv[i], 1);
i++;
if (argv[i] != NULL)
ft_putchar_fd(' ', 1);
}
if (!flag)
ft_putchar_fd('\n', 1);
return (0);
}

int ft_pwd(char **argv)
{
char cwd[256];

(void)argv;
if (getcwd(cwd, sizeof(cwd)) == NULL)
perror("getcwd() error");
else
printf("%s\n", cwd);
return (0);
}

void ft_export_loop(char **argv)
{
char **name_value;
char *tmpstr;

name_value = ft_strsplit(*argv, '=');
if (name_value[1])
{
tmpstr = ft_concat_multi(name_value + 1, "=");
ft_setenv(name_value[0], tmpstr, 1);
free(tmpstr);
}
else
{
if (name_value[0] && (*argv)[ft_strlen(name_value[0])] == '=')
ft_setenv(name_value[0], NULL, 1);
}
ft_matrix_free(name_value);
}

int ft_export(char **argv, int index)
{
if (*argv && ft_strlen(*argv) == 0)
return (0);
argv = argv + index;
if (*argv)
{
while (*argv != NULL)
{
ft_export_loop(argv);
argv++;
}
}
else
ft_export_no_args();
return (1);
}
104 changes: 104 additions & 0 deletions srcs/builtins_background.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* built_ins_background.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hgoncalv <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/22 15:19:22 by hgoncalv #+# #+# */
/* Updated: 2022/12/29 19:50:11 by hgoncalv ### ########.fr */
/* */
/* ************************************************************************** */

#include "../include/minishell.h"

char *ft_setenv_str(char *name, char *value, char *str)
{
if (value == NULL)
str = ft_strjoin(name, "=");
else
str = ft_concat3(name, "=", value);
return (str);
}

int set_cd_folder_return_if_free_folder_or_not(char **argv,
char **ptr2folder)
{
char *tmp_str;
int to_free;

to_free = 1;
if (argv[1] == NULL)
*ptr2folder = ft_getenv("HOME", 1);
else if (argv[1][0] == '-')
*ptr2folder = ft_getenv("OLDPWD", 1);
else if (argv[1][0] == '~')
{
tmp_str = ft_getenv("HOME", 1);
*ptr2folder = ft_strjoin(tmp_str, argv[1] + 1);
free(tmp_str);
}
else
{
*ptr2folder = argv[1];
to_free = 0;
}
return (to_free);
}

void ft_export_no_args(void)
{
t_vars vars;

ft_inicialize_vars(&vars);
vars.matrix = ft_matrix_dup(_shell()->envp, 0);
while (vars.matrix[vars.l] != NULL)
{
while (vars.matrix[vars.k + 1] != NULL)
{
vars.i = ft_strlen(vars.matrix[vars.k]);
vars.j = ft_strlen(vars.matrix[vars.k + 1]);
if (vars.i > vars.j)
vars.i = vars.j;
if (ft_strncmp(vars.matrix[vars.k], vars.matrix[vars.k + 1],
vars.i) > 0)
ft_swap2str(&(vars.matrix[vars.k]), &(vars.matrix[vars.k + 1]));
vars.k++;
}
vars.k = 0;
vars.l++;
}
ft_print_matrix_add_str2line_start(vars.matrix, "declare -x", " ");
ft_matrix_free(vars.matrix);
}

void unset_loop(char *argv)
{
int i;
char **envp;

envp = NULL;
i = -1;
while (_shell()->envp[++i] != NULL && *argv && ft_strncmp(_shell()->envp[i],
argv, ft_strlen(argv)))
;
if (_shell()->envp[i] != NULL)
{
envp = ft_matrix_remove_col_by_index(_shell()->envp, i);
_shell()->envp = envp;
}
}

int ft_unset(char **argv)
{
argv++;
if (*argv)
{
while (*argv)
{
unset_loop(*argv);
argv++;
}
}
return (0);
}
57 changes: 57 additions & 0 deletions srcs/builtins_background2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* built_ins_background2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hgoncalv <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/29 19:50:19 by hgoncalv #+# #+# */
/* Updated: 2022/12/29 19:50:21 by hgoncalv ### ########.fr */
/* */
/* ************************************************************************** */

#include "../include/minishell.h"

int ft_exit(char **argv)
{
if (argv[1])
{
_shell()->exit_code = ft_atoi(argv[1]);
_shell()->exit = 1;
return (ft_atoi(argv[1]));
}
else
{
_shell()->exit_code = 0;
_shell()->exit = 1;
return (0);
}
}

int ft_setenv(char *name, char *value, int overwrite)
{
int i;
char *str;
char **envp;

i = -1;
str = NULL;
str = ft_setenv_str(name, value, str);
if (str == NULL)
return (0);
while (_shell()->envp[++i] != NULL && name && ft_strncmp(_shell()->envp[i],
name, ft_strlen(name)))
;
if (_shell()->envp[i] != NULL && overwrite == 1)
{
free(_shell()->envp[i]);
_shell()->envp[i] = ft_strdup(str);
}
else if (_shell()->envp[i] == NULL)
{
envp = ft_matrix_push(_shell()->envp, ft_strdup(str));
_shell()->envp = envp;
}
free(str);
return (1);
}
63 changes: 63 additions & 0 deletions srcs/builtins_checkers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtins_checkers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hgoncalv <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/22 15:19:24 by hgoncalv #+# #+# */
/* Updated: 2022/12/29 19:59:27 by hgoncalv ### ########.fr */
/* */
/* ************************************************************************** */

#include "../include/minishell.h"

int check_if_builtin(t_cmd *cmd)
{
if (ft_strchr(cmd->args[0], '='))
return (1);
if (ft_strexact("cd", cmd->args[0]))
return (1);
if (ft_strexact("export", cmd->args[0]))
return (1);
if (ft_strexact("unset", cmd->args[0]))
return (1);
if (ft_strexact("exit", cmd->args[0]))
return (1);
if (ft_strexact("pwd", cmd->args[0]))
return (1);
if (ft_strexact("echo", cmd->args[0]))
return (1);
if (ft_strexact("env", cmd->args[0]))
return (1);
return (0);
}

int check_if_builtin_not_pipe(t_cmd *cmd)
{
if (!cmd->next)
{
if (ft_strchr(cmd->args[0], '='))
return (1);
if (ft_strexact("cd", cmd->args[0]))
return (1);
if (ft_strexact("export", cmd->args[0]))
return (1);
if (ft_strexact("unset", cmd->args[0]))
return (1);
if (ft_strexact("exit", cmd->args[0]))
return (1);
}
return (0);
}

int check_if_builtin_2pipe(t_cmd *cmd)
{
if (ft_strexact("pwd", cmd->args[0]))
return (1);
if (ft_strexact("echo", cmd->args[0]))
return (1);
if (ft_strexact("env", cmd->args[0]))
return (1);
return (0);
}
Loading

0 comments on commit 8908fa4

Please sign in to comment.