Browse Source

save

master
narnaud 3 years ago
parent
commit
375c9103ca
  1. 10
      Makefile
  2. 22
      built-in.c
  3. 21
      caller.c
  4. 14
      env.c
  5. 13
      minishell.c
  6. 17
      minishell.h

10
Makefile

@ -1,10 +1,12 @@
NAME = minishell NAME = minishell
LIBFT = libft.a LIBFT = libft.a
SRCS = minishell.c lexer.c parser.c utils.c caller.c SRCS = minishell.c lexer.c parser.c utils.c caller.c built-in.c env.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
READLINE_INC = ~/.brew/opt/readline/include //READLINE_INC = ~/.brew/opt/readline/include
LIB = -L ~/.brew/lib -lreadline -L ~/.brew/lib -lhistory -L. -lft //LIB = -L ~/.brew/lib -lreadline -L ~/.brew/lib -lhistory -L. -lft
LIB = -lreadline -lhistory -L. -lft
$(NAME): $(OBJS) $(NAME): $(OBJS)
gcc ${OBJS} ${LIB} -o ${NAME} gcc ${OBJS} ${LIB} -o ${NAME}
@ -16,7 +18,7 @@ $(LIBFT):
all: $(LIBFT) $(NAME) all: $(LIBFT) $(NAME)
%.o: %.c %.o: %.c
gcc -Werror -Wextra -Wall -g -c $< -I ${READLINE_INC} #for rl_replace_line gcc -Werror -Wextra -Wall -g -c $< # -I ${READLINE_INC} #for rl_replace_line
clean: clean:
rm -rf ${OBJS} rm -rf ${OBJS}

22
old_built-in.c → built-in.c

@ -6,13 +6,13 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/06 09:02:57 by narnaud #+# #+# */ /* Created: 2022/01/06 09:02:57 by narnaud #+# #+# */
/* Updated: 2022/04/14 07:45:37 by narnaud ### ########.fr */ /* Updated: 2022/05/03 18:18:23 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int ft_echo(char *argv[], int output_fd) int ft_echo(char *argv[])
{ {
int no_nl; int no_nl;
@ -25,36 +25,32 @@ int ft_echo(char *argv[], int output_fd)
no_nl = 1; no_nl = 1;
while (*argv) while (*argv)
{ {
ft_putstr_fd(*argv, output_fd); ft_putstr_fd(*argv, 1);
argv++; argv++;
if (*argv) if (*argv)
ft_putstr_fd(" ", output_fd); ft_putstr_fd(" ", 1);
} }
} }
if (!no_nl) if (!no_nl)
ft_putstr_fd("\n", output_fd); ft_putstr_fd("\n", 1);
return (0); return (0);
} }
int ft_pwd(char **argv, int input_fd, int output_fd) int ft_pwd(char **argv)
{ {
char *dir; char *dir;
(void)argv; (void)argv;
(void)input_fd;
dir = ft_calloc(PATHS_MAX_SIZE, sizeof(char)); dir = ft_calloc(PATHS_MAX_SIZE, sizeof(char));
getcwd(dir, PATHS_MAX_SIZE); getcwd(dir, PATHS_MAX_SIZE);
ft_putstr_fd(dir, output_fd); ft_putstr_fd(dir, 1);
ft_putstr_fd("\n", output_fd); ft_putstr_fd("\n", 1);
free(dir); free(dir);
return (0); return (0);
} }
int ft_cd(char **argv, int input_fd, int output_fd) int ft_cd(char **argv)
{ {
(void)output_fd;
(void)input_fd;
if (argv[1]) if (argv[1])
{ {
if (access(argv[1], X_OK)) if (access(argv[1], X_OK))

21
caller.c

@ -6,26 +6,26 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */ /* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */
/* Updated: 2022/05/03 13:57:06 by narnaud ### ########.fr */ /* Updated: 2022/05/03 20:29:01 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
/*
int command_call(char *argv[], int input_fd, int output_fd) int command_call(char *argv[])
{ {
if (ft_strncmp(argv[0], "echo", 4) == 0) if (ft_strncmp(argv[0], "echo", 4) == 0)
return (ft_echo(argv, output_fd)); return (ft_echo(argv));
else if (ft_strncmp(argv[0], "pwd", 3) == 0) else if (ft_strncmp(argv[0], "pwd", 3) == 0)
return (ft_pwd(argv, input_fd, output_fd)); return (ft_pwd(argv));
else if (ft_strncmp(argv[0], "cd", 2) == 0) else if (ft_strncmp(argv[0], "cd", 2) == 0)
return (ft_cd(argv, input_fd, output_fd)); return (ft_cd(argv));
else if (ft_strncmp(argv[0], "export", 6) == 0) else if (ft_strncmp(argv[0], "export", 6) == 0)
return (ft_export(argv, input_fd, output_fd)); return (ft_export(argv));
else if (ft_strncmp(argv[0], "unset", 5) == 0) else if (ft_strncmp(argv[0], "unset", 5) == 0)
return (ft_unset(argv, input_fd, output_fd)); return (ft_unset(argv));
else if (ft_strncmp(argv[0], "env", 3) == 0) else if (ft_strncmp(argv[0], "env", 3) == 0)
return (ft_env(argv, input_fd, output_fd)); return (ft_env(argv));
else if (ft_strncmp(argv[0], "exit", 4) == 0) else if (ft_strncmp(argv[0], "exit", 4) == 0)
return (close_minishell(0)); return (close_minishell(0));
else else
@ -34,14 +34,13 @@ int command_call(char *argv[], int input_fd, int output_fd)
return (-1); return (-1);
} }
} }
*/
void exe(char *argv[], char *const envp[]) void exe(char *argv[], char *const envp[])
{ {
char **path_dirs; char **path_dirs;
char *path; char *path;
size_t path_length; size_t path_length;
execve(argv[0], argv, envp); execve(argv[0], argv, envp);
path_dirs = ft_split(getenv("PATH"), ':'); path_dirs = ft_split(getenv("PATH"), ':');
while (path_dirs) while (path_dirs)

14
old_env.c → env.c

@ -6,16 +6,14 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */ /* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */
/* Updated: 2022/04/14 07:45:21 by narnaud ### ########.fr */ /* Updated: 2022/05/03 18:17:46 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int ft_unset(char **argv, int input_fd, int output_fd) int ft_unset(char **argv)
{ {
(void)output_fd;
(void)input_fd;
if (!argv[1]) if (!argv[1])
{ {
//read(input_fd, PIPE_MAX_SIZE); //read(input_fd, PIPE_MAX_SIZE);
@ -24,10 +22,8 @@ int ft_unset(char **argv, int input_fd, int output_fd)
return (0); return (0);
} }
int ft_env(char **argv, int input_fd, int output_fd) int ft_env(char **argv)
{ {
(void)output_fd;
(void)input_fd;
(void)argv; (void)argv;
int i; int i;
@ -55,14 +51,12 @@ int is_valid_identifier(char *ident)
return (0); return (0);
} }
int ft_export(char **argv, int input_fd, int output_fd) int ft_export(char **argv)
{ {
char **new; char **new;
char **env; char **env;
int i; int i;
(void)output_fd;
(void)input_fd;
i = 0; i = 0;
new = ft_split(argv[1], '='); new = ft_split(argv[1], '=');
if (!is_valid_identifier(new[0])) if (!is_valid_identifier(new[0]))

13
minishell.c

@ -6,13 +6,14 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ /* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */
/* Updated: 2022/05/03 16:25:14 by narnaud ### ########.fr */ /* Updated: 2022/05/03 20:31:44 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
# include "minishell.h" # include "minishell.h"
// Note: Pipes are broken char **g_env;
int caller(t_command *cmd) int caller(t_command *cmd)
{ {
int cmd_ret; int cmd_ret;
@ -20,7 +21,6 @@ int caller(t_command *cmd)
int pip[2]; int pip[2];
int status; int status;
if (cmd->fd[1] == 0 && cmd->next && cmd->next->fd[0] == 0) if (cmd->fd[1] == 0 && cmd->next && cmd->next->fd[0] == 0)
{ {
pipe(pip); pipe(pip);
@ -30,8 +30,8 @@ int caller(t_command *cmd)
pid = fork(); pid = fork();
if (!pid) if (!pid)
{ {
cmd_ret = 1; //command_call(command->argv); cmd_ret = command_call(cmd->argv);
if (cmd_ret == 1) if (cmd_ret == -1)
{ {
if (cmd->fd[0]) if (cmd->fd[0])
dup2(cmd->fd[0], STDIN_FILENO); dup2(cmd->fd[0], STDIN_FILENO);
@ -48,7 +48,6 @@ int caller(t_command *cmd)
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
if (cmd->fd[0]) if (cmd->fd[0])
close(cmd->fd[0]); close(cmd->fd[0]);
return (cmd_ret); return (cmd_ret);
} }
@ -132,7 +131,7 @@ int main(int argc, char **argv, char**envp)
{ {
if (argc < 2) if (argc < 2)
(void)argv; (void)argv;
(void)envp; g_env = envp;
t_env env; t_env env;
env.null = 0; env.null = 0;
while (1) while (1)

17
minishell.h

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */ /* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */
/* Updated: 2022/05/03 15:40:23 by narnaud ### ########.fr */ /* Updated: 2022/05/03 20:22:29 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,7 +31,19 @@
# include <errno.h> # include <errno.h>
# include <termios.h> # include <termios.h>
char **g_env; extern char **g_env;
// ----------------------------------Utils.c
int ft_echo(char **argv);
int ft_pwd(char **argv);
int ft_cd(char **argv);
int close_minishell(int exit_code);
// ----------------------------------Utils.c
int ft_unset(char **argv);
int ft_env(char **argv);
int is_valid_identifier(char *ident);
int ft_export(char **argv);
// ----------------------------------Utils.c // ----------------------------------Utils.c
@ -57,6 +69,7 @@ typedef struct s_command
} t_command; } t_command;
void exe(char *argv[], char *const envp[]); void exe(char *argv[], char *const envp[]);
int command_call(char *argv[]);
// ----------------------------------Parser.c // ----------------------------------Parser.c

Loading…
Cancel
Save