Skip to content

Commit

Permalink
Merge pull request #22 from Ofacy/exec-folder
Browse files Browse the repository at this point in the history
Add 'is a directory' error
  • Loading branch information
UnRenardQuiDab authored Apr 4, 2024
2 parents 4db6e0e + 4e4f485 commit b4c9609
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: bwisniew <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/02/14 13:24:31 by bwisniew #+# #+# #
# Updated: 2024/04/03 18:20:54 by bwisniew ### ########.fr #
# Updated: 2024/04/03 23:15:19 by lcottet ### ########.fr #
# #
# **************************************************************************** #

Expand All @@ -21,7 +21,7 @@ SRCS = main.c env.c prompt.c error.c env_utils.c mshell_utils.c signal.c
PARSER_SRCS = lexer.c syntax.c token_utils.c

EXEC_SRCS = path.c exec.c exec_fd.c here_doc.c fork.c exec_utils.c wait.c \
close.c exec_builtins.c
close.c exec_builtins.c filetype.c

EXPAND_SRCS = expander_token.c expander_len.c expander_split.c expander.c \
expander_join.c expander_file.c
Expand Down
3 changes: 2 additions & 1 deletion includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bwisniew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/13 17:55:03 by lcottet #+# #+# */
/* Updated: 2024/04/03 18:21:30 by bwisniew ### ########.fr */
/* Updated: 2024/04/03 23:29:22 by lcottet ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -100,6 +100,7 @@ int exec_builtins(t_execute *exec, t_mshell *sh);
void exec_cmd(t_execute *exec, t_mshell *sh, char **envp);
void exec_fail(t_execute *exec, t_mshell *sh, char **envp);
void choose_fork_exec(t_mshell *sh, t_execute *exec, char **envp);
int regular_file_check(t_mshell *sh, char *path);

char *expander_join(t_token *t1, t_token *t2);

Expand Down
2 changes: 1 addition & 1 deletion srcs/exec/close.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bwisniew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/10 20:34:14 by bwisniew #+# #+# */
/* Updated: 2024/03/19 19:08:58 by bwisniew ### ########.fr */
/* Updated: 2024/04/03 23:51:04 by lcottet ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
12 changes: 9 additions & 3 deletions srcs/exec/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: lcottet <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/10 17:18:34 by lcottet #+# #+# */
/* Updated: 2024/03/21 19:02:00 by lcottet ### ########.fr */
/* Updated: 2024/04/03 23:52:33 by lcottet ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -77,8 +77,9 @@ void exec_cmd(t_execute *exec, t_mshell *sh, char **envp)

pid_t exec_txt(t_execute *exec, t_mshell *sh)
{
char *null;
pid_t pid;
char *null;
pid_t pid;
int filetype;

if (exec->args.len == 0)
return (-4);
Expand All @@ -93,6 +94,11 @@ pid_t exec_txt(t_execute *exec, t_mshell *sh)
return (-3);
return (-4);
}
filetype = regular_file_check(sh, exec->cmd);
if (filetype == -1)
return (free(exec->cmd), -1);
else if (!filetype)
return (free(exec->cmd), -4);
pid = exec_fork(exec, sh);
free(exec->cmd);
return (pid);
Expand Down
36 changes: 36 additions & 0 deletions srcs/exec/filetype.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* filetype.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lcottet <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 23:12:15 by lcottet #+# #+# */
/* Updated: 2024/04/03 23:39:57 by lcottet ### ########.fr */
/* */
/* ************************************************************************** */

#include <sys/types.h>
#include <sys/stat.h>

#include "minishell.h"

int regular_file_check(t_mshell *sh, char *path)
{
struct stat sb;

if (!path)
return (1);
if (stat(path, &sb) == -1)
{
error(path);
return (-1);
}
if (!S_ISREG(sb.st_mode))
{
custom_error(path, "Is a directory");
if (set_env_return(sh, 127))
return (-1);
}
return (S_ISREG(sb.st_mode));
}

0 comments on commit b4c9609

Please sign in to comment.