Skip to content

Commit

Permalink
Only use argv in router
Browse files Browse the repository at this point in the history
  • Loading branch information
vfurmane committed May 28, 2021
1 parent ad73b5c commit ed67e0e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 36 deletions.
5 changes: 2 additions & 3 deletions includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: user42 <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/30 17:20:33 by vfurmane #+# #+# */
/* Updated: 2021/05/27 13:35:22 by vfurmane ### ########.fr */
/* Updated: 2021/05/28 08:58:59 by vfurmane ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -151,14 +151,13 @@ char *ft_cmd_argdup(t_config *shell_c, const char *cmd);
int ft_stderr_message(const char *str1, const char *str2,
const char *str3, int ret);

int ft_route_command(const char *command, char **args,
char **line, t_config *shell_c);
int ft_route_file_to(const char *file_name, t_config *shell_c,
int happen);
int ft_write_pipe(const char *key, const char *value1,
const char *value2, int pipefd);
int ft_route_file_from(const char *file_name,
t_config *shell_c);
int ft_route_command(t_config *shell_c, char **argv);

char *ft_skip_spaces(const char *cmd, int *i);
char *ft_skip_cmd_arg(const char *cmd, int *i);
Expand Down
4 changes: 2 additions & 2 deletions srcs/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: earnaud <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/31 18:42:16 by vfurmane #+# #+# */
/* Updated: 2021/05/27 13:36:16 by earnaud ### ########.fr */
/* Updated: 2021/05/28 09:01:23 by vfurmane ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -87,7 +87,7 @@ int ft_recursiv_command(t_cmd *cmd, t_config *shell_c, int pipe_in, int std_out)
cmd_next = cmd->next;
fd_next = cmd->fd[0];
free(cmd);
ret = ft_route_command(args[0], &args[1], args, shell_c);
ret = ft_route_command(shell_c, args);
free_neo(args);
ft_recursiv_command(cmd_next, shell_c, fd_next, std_out);
free_shell(shell_c);
Expand Down
56 changes: 25 additions & 31 deletions srcs/router.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,77 +6,71 @@
/* By: earnaud <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/04/01 11:52:44 by vfurmane #+# #+# */
/* Updated: 2021/05/27 15:17:33 by earnaud ### ########.fr */
/* Updated: 2021/05/28 09:02:46 by vfurmane ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

static int ft_is_a_builtin(t_config *shell_c, const char *command, char **args,
int *ret)
static int ft_is_a_builtin(t_config *shell_c, char **argv, int *ret)
{
int builtin_id;
const char *builtins[8] = {"echo", "cd", "pwd", "export", "unset",
"env", "exit", NULL};
const t_builtin funcs[7] = {ft_echo, ft_cd, ft_pwd, ft_export, ft_unset,
ft_env, ft_exit};

ft_strarrstr(builtins, command, &builtin_id);
ft_strarrstr(builtins, argv[0], &builtin_id);
if (builtin_id == -1)
return (0);
*ret = (funcs[builtin_id])(shell_c, args, STDOUT_FILENO);
*ret = (funcs[builtin_id])(shell_c, &argv[1], STDOUT_FILENO);
return (1);
}

static int return_free(char **args, char **line, t_config *shell_c, int ret)
static int return_free(t_config *shell_c, char **argv, int ret)
{
if (ret == 127)
ft_stderr_message(line[0], ": No such file or directory", NULL,
ft_stderr_message(argv[0], ": No such file or directory", NULL,
127);
if (ret == 126)
ft_stderr_message(line[0], ": Is a directory", NULL,
ft_stderr_message(argv[0], ": Is a directory", NULL,
126);
free_neo_content(args); //maybe leaks here?
free_neo(line);
free_neo(argv);
free_shell(shell_c);
return (ret);
}

static int ft_search_and_execute_file(t_config *shell_c,
char **args, char **line)
static int ft_search_and_execute_file(t_config *shell_c, char **argv)
{
struct stat file_stat;

if (stat(line[0], &file_stat) == -1)
exit(return_free(args, line, shell_c, 127));
if (stat(argv[0], &file_stat) == -1)
exit(return_free(shell_c, argv, 127));
if (file_stat.st_mode & 040000)
exit(return_free(args, line, shell_c, 126));
exit(return_free(shell_c, argv, 126));
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
ft_execve(line[0], line, shell_c->envp);
return_free(args, line, shell_c, 0);
ft_execve(argv[0], argv, shell_c->envp);
return_free(shell_c, argv, 0);
return (127);
}

static int ft_search_and_execute_command(t_config *shell_c,
const char *command, char **args, char **line)
static int ft_search_and_execute_command(t_config *shell_c, char **argv)
{
if (ft_getenv(shell_c->envp_list, "PATH") == NULL)
exit(ft_stderr_message(command, ": No such file or directory", NULL,
exit(ft_stderr_message(argv[0], ": No such file or directory", NULL,
127));
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
if (command[0] == '\0' || ft_strcmp(".", command) == 0
|| !ft_exec(shell_c, line))
ft_stderr_message(command, ": command not found", NULL, 0);
free_neo_content(args);
free_neo(line);
if (argv[0][0] == '\0' || ft_strcmp(".", argv[0]) == 0
|| !ft_exec(shell_c, argv))
ft_stderr_message(argv[0], ": command not found", NULL, 0);
free_neo(argv);
free_shell(shell_c);
return (127);
}

int ft_route_command(const char *command, char **args, char **line,
t_config *shell_c)
int ft_route_command(t_config *shell_c, char **argv)
{
int ret;
int id;
Expand All @@ -91,12 +85,12 @@ int ft_route_command(const char *command, char **args, char **line,
wait(&status);
else
{
if (ft_is_a_builtin(shell_c, command, args, &ret))
if (ft_is_a_builtin(shell_c, argv, &ret))
exit(ret);
else if (ft_strchr(command, '/'))
exit(ft_search_and_execute_file(shell_c, args, line));
else if (ft_strchr(argv[0], '/'))
exit(ft_search_and_execute_file(shell_c, argv));
else
exit(ft_search_and_execute_command(shell_c, command, args, line));
exit(ft_search_and_execute_command(shell_c, argv));
}
if (WIFSIGNALED(status))
ret += 128 + WTERMSIG(status);
Expand Down
29 changes: 29 additions & 0 deletions tools/argv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* argv.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vfurmane <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/28 08:52:02 by vfurmane #+# #+# */
/* Updated: 2021/05/28 08:56:41 by vfurmane ### ########.fr */
/* */
/* ************************************************************************** */

#include <stdio.h>

int main(int argc, char **argv, char **envp)
{
int i;

printf("argc -> %d\n\n", argc);
printf("argv:\n");
i = -1;
while (argv[++i])
printf(" %02d. %s\n", i, argv[i]);
printf("\nenvp:\n");
i = -1;
while (envp[++i])
printf(" %02d. %s\n", i, envp[i]);
return (0);
}

0 comments on commit ed67e0e

Please sign in to comment.