Browse Source

redirection on alone built-in, file error function

master
narnaud 3 years ago
parent
commit
1b53f04629
  1. 2
      Makefile
  2. 34
      built-in.c
  3. 8
      caller.c
  4. 20
      env.c
  5. 25
      errors.c
  6. 6
      minishell.h
  7. 2
      utils.c

2
Makefile

@ -1,6 +1,6 @@
NAME = minishell NAME = minishell
LIBFT = libft.a LIBFT = libft.a
SRCS = minishell.c lexer.c parser.c utils.c caller.c built-in.c env.c SRCS = minishell.c lexer.c parser.c utils.c caller.c built-in.c env.c errors.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}

34
built-in.c

@ -6,7 +6,7 @@
/* 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/05/03 18:18:23 by narnaud ### ########.fr */ /* Updated: 2022/05/06 13:37:47 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,6 +15,12 @@
int ft_echo(t_command *cmd) int ft_echo(t_command *cmd)
{ {
int no_nl; int no_nl;
int fd_out;
if (cmd->fd[1])
fd_out = cmd->fd[1];
else
fd_out = 1;
no_nl = 0; no_nl = 0;
if (*(++cmd->argv)) if (*(++cmd->argv))
@ -23,25 +29,29 @@ int ft_echo(t_command *cmd)
no_nl = 1; no_nl = 1;
while (*(cmd->argv)) while (*(cmd->argv))
{ {
ft_putstr_fd(*(cmd->argv), 1); ft_putstr_fd(*(cmd->argv), fd_out);
if (*(++cmd->argv)) if (*(++cmd->argv))
ft_putstr_fd(" ", 1); ft_putstr_fd(" ", fd_out);
} }
} }
if (!no_nl) if (!no_nl)
ft_putstr_fd("\n", 1); ft_putstr_fd("\n", fd_out);
return (0); return (0);
} }
int ft_pwd(t_command *cmd) int ft_pwd(t_command *cmd)
{ {
char *dir; char *dir;
int fd_out;
(void)cmd; if (cmd->fd[1])
fd_out = cmd->fd[1];
else
fd_out = 1;
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, 1); ft_putstr_fd(dir, fd_out);
ft_putstr_fd("\n", 1); ft_putstr_fd("\n", fd_out);
free(dir); free(dir);
return (0); return (0);
} }
@ -51,15 +61,9 @@ int ft_cd(t_command *cmd)
if (cmd->argv[1]) if (cmd->argv[1])
{ {
if (access(cmd->argv[1], X_OK)) if (access(cmd->argv[1], X_OK))
{ return(file_error(cmd->argv[0], cmd->argv[1], "Permission denied\n"));
printf("Minishell: %s: %s: Permission denied\n", cmd->argv[0], cmd->argv[1]);
return (0);
}
else if (access(cmd->argv[1], F_OK)) else if (access(cmd->argv[1], F_OK))
{ return(file_error(cmd->argv[0], cmd->argv[1], "Not a directory\n"));
printf("Minishell: %s: %s: Not a directory\n", cmd->argv[0], cmd->argv[1]);
return (0);
}
chdir(cmd->argv[1]); chdir(cmd->argv[1]);
return (1); return (1);
} }

8
caller.c

@ -6,7 +6,7 @@
/* 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/06 12:14:27 by narnaud ### ########.fr */ /* Updated: 2022/05/06 13:32:33 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,7 +25,7 @@ int builtin_call(t_datas *datas, t_command *cmd)
else if (ft_strncmp(cmd->argv[0], "unset", 6) == 0) else if (ft_strncmp(cmd->argv[0], "unset", 6) == 0)
return (ft_unset(datas, cmd)); return (ft_unset(datas, cmd));
else if (ft_strncmp(cmd->argv[0], "env", 4) == 0) else if (ft_strncmp(cmd->argv[0], "env", 4) == 0)
return (ft_env(datas)); return (ft_env(datas, cmd));
else if (ft_strncmp(cmd->argv[0], "exit", 5) == 0) else if (ft_strncmp(cmd->argv[0], "exit", 5) == 0)
return (close_minishell(0)); return (close_minishell(0));
else else
@ -77,13 +77,15 @@ pid_t caller(t_datas *datas, t_command *cmd)
pid = fork(); pid = fork();
if (!pid) if (!pid)
{
if (builtin_call(datas, cmd) == -1)
{ {
if (cmd->fd[0]) if (cmd->fd[0])
dup2(cmd->fd[0], STDIN_FILENO); dup2(cmd->fd[0], STDIN_FILENO);
if (cmd->fd[1]) if (cmd->fd[1])
dup2(cmd->fd[1], STDOUT_FILENO); dup2(cmd->fd[1], STDOUT_FILENO);
if (builtin_call(datas, cmd) == -1)
exe(datas, cmd); exe(datas, cmd);
}
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
return pid; return pid;

20
env.c

@ -6,7 +6,7 @@
/* 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/05/05 09:30:51 by narnaud ### ########.fr */ /* Updated: 2022/05/06 13:39:03 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -38,14 +38,20 @@ int ft_unset(t_datas *datas, t_command *cmd)
return (0); return (0);
} }
int ft_env(t_datas *datas) int ft_env(t_datas *datas, t_command *cmd)
{ {
int i; int i;
int fd_out;
if (cmd->fd[1])
fd_out = cmd->fd[1];
else
fd_out = 1;
i = 0; i = 0;
while (datas->envp[i]) while (datas->envp[i])
{ {
printf("%s\n", datas->envp[i]); ft_putstr_fd(datas->envp[i], fd_out);
ft_putchar_fd('\n', fd_out);
i++; i++;
} }
return (0); return (0);
@ -61,7 +67,9 @@ int ft_export(t_datas *datas, t_command *cmd)
new = ft_split(cmd->argv[1], '='); new = ft_split(cmd->argv[1], '=');
if (!is_valid_identifier(new[0])) if (!is_valid_identifier(new[0]))
{ {
printf ("export: not an identifier: %s\n", new[0]); ft_putstr_fd("export: not an identifier: ", 2);
ft_putstr_fd(new[0], 2);
ft_putchar_fd('\n', 2);
return (0); return (0);
} }
while (datas->envp[i]) while (datas->envp[i])
@ -78,7 +86,3 @@ int ft_export(t_datas *datas, t_command *cmd)
datas->envp[i + 1] = NULL; datas->envp[i + 1] = NULL;
return (1); return (1);
} }

25
errors.c

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* errors.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/06 13:15:59 by narnaud #+# #+# */
/* Updated: 2022/05/06 13:25:32 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int file_error(char *cmd, char *file, char *msg)
{
ft_putstr_fd("Minishell: ", 2);
ft_putstr_fd(cmd, 2);
ft_putstr_fd(": ", 2);
ft_putstr_fd(file, 2);
ft_putstr_fd(": ", 2);
ft_putstr_fd(msg, 2);
ft_putchar_fd('\n', 2);
return (0);
}

6
minishell.h

@ -6,7 +6,7 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ /* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */ /* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */
/* Updated: 2022/05/06 11:49:32 by narnaud ### ########.fr */ /* Updated: 2022/05/06 13:30:59 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -46,6 +46,8 @@ typedef struct s_datas
char **envp; char **envp;
int exit_code; int exit_code;
} t_datas; } t_datas;
// ----------------------------------Errors.c
int file_error(char *cmd, char *file, char *msg);
// ----------------------------------Utils.c // ----------------------------------Utils.c
int is_empty(char *line); int is_empty(char *line);
@ -63,7 +65,7 @@ int ft_cd(t_command *cmd);
// ----------------------------------Env.c // ----------------------------------Env.c
int is_valid_identifier(char *ident); int is_valid_identifier(char *ident);
int ft_unset(t_datas *datas, t_command *cmd); int ft_unset(t_datas *datas, t_command *cmd);
int ft_env(t_datas *datas); int ft_env(t_datas *datas, t_command *cmd);
int ft_export(t_datas *datas, t_command *cmd); int ft_export(t_datas *datas, t_command *cmd);
// ----------------------------------Caller.c // ----------------------------------Caller.c

2
utils.c

@ -6,7 +6,7 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ /* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/03 08:57:53 by narnaud #+# #+# */ /* Created: 2022/05/03 08:57:53 by narnaud #+# #+# */
/* Updated: 2022/05/06 12:13:48 by narnaud ### ########.fr */ /* Updated: 2022/05/06 13:15:33 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

Loading…
Cancel
Save