Skip to content

Commit

Permalink
fix:norminette
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Demirkylych committed Jun 17, 2024
1 parent 52e80ce commit 48399c3
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 139 deletions.
4 changes: 2 additions & 2 deletions inc/builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 16:34:46 by dmdemirk #+# #+# */
/* Updated: 2024/06/04 12:18:43 by dmdemirk ### ########.fr */
/* Updated: 2024/06/17 11:02:55 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -20,7 +20,7 @@ int builtin_cd(t_minishell_data *data);
int builtin_echo(t_minishell_data *data);
int builtin_env(t_minishell_data *data);
int builtin_exit(t_minishell_data *data);
int builtin_export(t_minishell_data *data);
int builtin_export(t_minishell_data *data);
int builtin_pwd(t_minishell_data *data);
int builtin_unset(t_minishell_data *data);

Expand Down
14 changes: 13 additions & 1 deletion inc/pipe.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipe.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/17 11:02:59 by dmdemirk #+# #+# */
/* Updated: 2024/06/17 11:03:00 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef PIPE_H
# define PIPE_H

Expand All @@ -6,4 +18,4 @@
/* pipe */
void builtin_pipe(t_minishell_data *data);

#endif
#endif
4 changes: 2 additions & 2 deletions src/builtins/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 16:31:07 by dmdemirk #+# #+# */
/* Updated: 2024/06/10 17:18:38 by dmdemirk ### ########.fr */
/* Updated: 2024/06/17 11:13:55 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -41,7 +41,7 @@ int builtin_cd(t_minishell_data *data)
return (1);
}
set_env(&data->envp, "OLDPWD", get_env(data->envp, "PWD"));
if(getcwd(cwd, sizeof(cwd)) != NULL)
if (getcwd(cwd, sizeof(cwd)) != NULL)
set_env(&data->envp, "PWD", cwd);
return (0);
}
4 changes: 2 additions & 2 deletions src/builtins/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 16:32:42 by dmdemirk #+# #+# */
/* Updated: 2024/06/11 16:47:39 by dmdemirk ### ########.fr */
/* Updated: 2024/06/17 11:13:42 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -26,7 +26,7 @@
int builtin_exit(t_minishell_data *data)
{
const char *message;

printf("\nbuiltin_exit\n");
message = "exit\n";
write(STDOUT_FILENO, message, ft_strlen(message));
Expand Down
90 changes: 50 additions & 40 deletions src/builtins/export.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* export.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/17 11:10:52 by dmdemirk #+# #+# */
/* Updated: 2024/06/17 11:15:10 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

#include "env.h"
#include "libft.h"
Expand All @@ -6,45 +17,44 @@
#include <stdio.h>

/*
Functionalities:
- If no arguments are passed or "-p", print all environment variables
- If arguments are passed, set the environment variables
*/

int builtin_export(t_minishell_data *data)
Functionalities:
- If no arguments are passed or "-p", print all environment variables
- If arguments are passed, set the environment variables
*/
/*
int builtin_export(t_minishell_data *data)
{
t_env *curr_node;
char *key;
int i;
t_env *curr_node;
char *key;
int i;
printf("\nbuiltin_export\n");
i = 0;
if ((data->args[1] == NULL && data->args[2] == NULL) || ft_strncmp(data->args[1], "-p", 2) == 0)
{
curr_node = data->envp;
while (curr_node)
{
printf("declare -x %s=\"%s\"\n", curr_node->key, curr_node->value);
curr_node = curr_node->next;
}
}
else
{
while(data->args[++i])
{
if (ft_strchr(data->args[i], '='))
set_env(&data->envp, data->args[i], ft_strchr(data->args[i], '=') + 1);
else
{
key = ft_strcdup(data->args[i], '=');
if (get_env(data->envp, key))
set_env(&data->envp, key, "");
else
set_env(&data->envp, key, NULL);
free(key);
printf("declare -x %s=\"%s\"\n", key, get_env(data->envp, key));
}
}
}
return (0);
}
i = 0;
if ((data->args[1] == NULL && data->args[2] == NULL) \
|| ft_strncmp(data->args[1], "-p", 2) == 0)
{
curr_node = data->envp;
while (curr_node)
curr_node = curr_node->next;
}
else
{
while (data->args[++i])
{
if (ft_strchr(data->args[i], '='))
set_env(&data->envp, data->args[i], \
ft_strchr(data->args[i], '=') + 1);
else
{
key = ft_strcdup(data->args[i], '=');
if (get_env(data->envp, key))
set_env(&data->envp, key, "");
else
set_env(&data->envp, key, NULL);
free(key);
}
}
}
return (0);
}
*/
35 changes: 23 additions & 12 deletions src/builtins/pwd.c
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pwd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/17 11:10:33 by dmdemirk #+# #+# */
/* Updated: 2024/06/17 11:10:42 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

#include "shell.h"
#include <unistd.h>
#include "libft.h"
#include <stdio.h>

/*
Functionality:
- Print the current working directory
*/
Functionality:
- Print the current working directory
*/

int builtin_pwd(t_minishell_data *data)
int builtin_pwd(t_minishell_data *data)
{
char cwd[4096];
char cwd[4096];

(void)data;
if (getcwd(cwd, sizeof(cwd)) != NULL)
ft_putendl_fd(cwd, STDOUT_FILENO);
else
{
(void)data;
if (getcwd(cwd, sizeof(cwd)) != NULL)
ft_putendl_fd(cwd, STDOUT_FILENO);
else
{
ft_putstr_fd("bash: cd: ", 2);
perror("cwd");
return (1);
}
return (0);
}
return (0);
}
60 changes: 36 additions & 24 deletions src/builtins/unset.c
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* unset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/17 11:10:03 by dmdemirk #+# #+# */
/* Updated: 2024/06/17 11:10:25 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

#include "shell.h"
#include "env.h"
#include "libft.h"
#include <unistd.h>

/*
todo
set the exit status to 1 if the variable is not a valid identifier
Functionality:
- Unset the environment variable
- Return 1 if the variable is not a valid identifier
*/
todo
set the exit status to 1 if the variable is not a valid identifier
Functionality:
- Unset the environment variable
- Return 1 if the variable is not a valid identifier
*/

int builtin_unset(t_minishell_data *data)
int builtin_unset(t_minishell_data *data)
{
int i;
int ret;
int i;
int ret;

i = 0;
ret = 0;
while (data->args[++i])
{
if (unset_env(&data->envp, data->args[i]) == -1)
{
ft_putstr_fd("bash: unset: `", STDERR_FILENO);
ft_putstr_fd(data->args[i], STDERR_FILENO);
ft_putendl_fd("': not a valid identifier", STDERR_FILENO);
ret = 1;
}
}
data->exit_status = ret;
return (ret);
}
i = 0;
ret = 0;
while (data->args[++i])
{
if (unset_env(&data->envp, data->args[i]) == -1)
{
ft_putstr_fd("bash: unset: `", STDERR_FILENO);
ft_putstr_fd(data->args[i], STDERR_FILENO);
ft_putendl_fd("': not a valid identifier", STDERR_FILENO);
ret = 1;
}
}
data->exit_status = ret;
return (ret);
}
4 changes: 2 additions & 2 deletions src/env/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 17:20:11 by dmdemirk #+# #+# */
/* Updated: 2024/06/10 17:16:32 by dmdemirk ### ########.fr */
/* Updated: 2024/06/17 11:08:05 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,7 +19,7 @@ void init_env(t_env **data_envp, char **envp);
void add_env_node(t_env **data_envp, char *line);
char *get_env(t_env *envp, const char *key);
void set_env(t_env **env, const char *key, const char *value);
int unset_env(t_env **env, const char *name);
int unset_env(t_env **env, const char *name);

void init_env(t_env **data_envp, char **envp)
{
Expand Down
26 changes: 8 additions & 18 deletions src/execute/execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/31 11:02:00 by dmdemirk #+# #+# */
/* Updated: 2024/06/11 17:12:28 by dmdemirk ### ########.fr */
/* Updated: 2024/06/17 11:18:02 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -23,30 +23,27 @@ int new_process(t_minishell_data *data);
int execute(t_minishell_data *data)
{
size_t i;
char *builtin_commands[7];
int (*builtin_functions[7])(t_minishell_data *);
char *builtin_commands[6];
int (*builtin_functions[6])(t_minishell_data *);

builtin_commands[0] = "cd";
builtin_commands[1] = "echo";
builtin_commands[2] = "env";
builtin_commands[3] = "exit";
builtin_commands[4] = "export";
builtin_commands[5] = "pwd";
builtin_commands[6] = "unset";
builtin_commands[4] = "pwd";
builtin_commands[5] = "unset";
builtin_functions[0] = &builtin_cd;
builtin_functions[1] = &builtin_echo;
builtin_functions[2] = &builtin_env;
builtin_functions[3] = &builtin_exit;
builtin_functions[4] = &builtin_export;
builtin_functions[5] = &builtin_pwd;
builtin_functions[6] = &builtin_unset;
builtin_functions[4] = &builtin_pwd;
builtin_functions[5] = &builtin_unset;
if (data->args[0] == NULL)
return (1);
i = -1;
while (++i < sizeof(builtin_commands) / sizeof(char *))
while (++i < sizeof(builtin_commands) / sizeof(char *))
if (ft_strcmp(data->args[0], builtin_commands[i]) == 0)
return ((*builtin_functions[i])(data));
printf("\nexternal_func\n");
return (new_process(data));
}

Expand All @@ -57,13 +54,6 @@ int new_process(t_minishell_data *data)
char *path;
char **envp;

/*
todo
add absolute - / path handling
add relative - path handling
add home - ~ path handling
*/

envp = env_to_array(data->envp);
path = ft_find_path(data->args[0], data->envp);
pid = fork();
Expand Down
Loading

0 comments on commit 48399c3

Please sign in to comment.