diff --git a/env.o b/env.o new file mode 100644 index 0000000..0330aff Binary files /dev/null and b/env.o differ diff --git a/ex.c b/ex.c new file mode 100644 index 0000000..86d7ae4 --- /dev/null +++ b/ex.c @@ -0,0 +1,363 @@ +#include "minishell.h" + +int lstsize(t_command *lst) +{ + int i; + + i = 0; + while (lst) + { + lst = lst->next; + i++; + } + return (i); +} + +void ft_echo(t_command *cmd) +{ + int i = 1; + int n = 0; + if (cmd->args[i] && ft_strcmp(cmd->args[i], "-n") == 0) + { + n = 1; + i++; + } + while (cmd->args[i]) + { + ft_putstr_fd(cmd->args[i], 1); + if (cmd->args[i + 1]) + ft_putstr_fd(" ", 1); + i++; + } + if (!n) + ft_putstr_fd("\n", 1); +} + +void ft_export(t_command *cmd, t_env **p_env) +{ + int i = 1; + char *key; + char *value; + t_env *tmp = *p_env; + + while (cmd->args[i] != NULL && ft_strchr(cmd->args[i], '=')) + { + int j = 0; + while (cmd->args[i][j] && cmd->args[i][j] != '=') + j++; + + key = ft_substr(cmd->args[i], 0, j); + value = NULL; + if (cmd->args[i][j] == '=') + value = ft_substr(cmd->args[i], j + 1, ft_strlen(cmd->args[i]) - j - 1); + while (tmp) + { + if (ft_strcmp(tmp->key, key) == 0) + { + if (value) + { + free(tmp->value); + tmp->value = ft_strdup(value); + } + break; + } + tmp = tmp->next; + } + if (tmp == NULL) + { + tmp = new_env(key, value); + lstadd_back(p_env, tmp); + } + + i++; + } +} + +void ft_cd(t_command *cmd) +{ + char *path; + // char *old = getcwd(NULL, 0); + + path = cmd->args[1]; + if (chdir(path) == -1) + { + ft_putstr_fd("minishell: cd: ", 1); + ft_putstr_fd(path, 1); + ft_putstr_fd(": No such file or directory\n", 1); + } +} + +void ft_pwd() +{ + char cwd[1024]; + + if (getcwd(cwd, sizeof(cwd)) != NULL) + printf("%s\n", cwd); + else + perror("getcwd() error"); +} + +void ft_env(t_command *cmd, t_env **g_env) +{ + (void)cmd; + t_env *tmp = *g_env; + while (tmp) + { + ft_putstr_fd(tmp->key, 1); + ft_putstr_fd("=", 1); + ft_putstr_fd(tmp->value, 1); + ft_putstr_fd("\n", 1); + tmp = tmp->next; + } +} + +void ft_exit() +{ + exit(0); +} + + +void ft_unset(t_command *cmd, t_env **g_env) +{ + int i = 1; + t_env *tmp; + t_env *prev; + while (cmd->args[i]) + { + tmp = *g_env; + prev = NULL; + while (tmp) + { + if (ft_strcmp(tmp->key, cmd->args[i]) == 0) + { + if (prev == NULL) + *g_env = tmp->next; + else + prev->next = tmp->next; + free(tmp->key); + free(tmp->value); + free(tmp); + break ; + } + prev = tmp; + tmp = tmp->next; + } + i++; + } +} + +int execute_builtins(t_command *cmd ,char **envi) +{ + char *path; + char *lwa; + extern char **environ; + int i = 0; + while(environ[i]) + { + if(ft_strncmp(environ[i], "PATH=", 5) == 0) + path = ft_strdup(environ[i] + 5); + i++; + } + i =0; + char **paths ; + paths = ft_split(path, ':'); + while(paths[i] ) + { + + int file_existance = access(cmd->args[0], F_OK); + int file_permission = access(cmd->args[0], X_OK); + if((cmd->args[0][0] == '.' || cmd->args[0][0] == '/')) + { + int j = 0; + if(file_permission == 0 && file_existance == 0) + { + int fd = open(cmd->args[0], O_RDONLY); + char *str = get_next_line(fd); + printf("%s\n", str); + } + while(cmd->args[0][j]) + { + if(!file_existance && file_permission == -1) + { + printf("minishell: %s: Permission denied\n", cmd->args[0]); + return (1); + exit(0); + } + else if(cmd->args[0][j] == '/') + { + if(file_existance == -1 ) + { + printf("minishell: %s: No such file or directory\n", cmd->args[0]); + return(10); + + } + if(cmd->args[0][j + 1] == '/') + { + printf("minishell : %s is a directory\n", cmd->args[0]); + return (1); + } + } + + else if(cmd->args[0][j] == '.') + { + if(file_existance == -1) + { + printf("minishell: %s: No such file or directory\n", cmd->args[0]); + return(10); + + } + if(cmd->args[0][j + 1] == '/') + { + printf("minishell : %s is a directory\n", cmd->args[0]); + return (1); + } + } + + j++; + + } + + + if(execve(cmd->args[0], cmd->args,envi) == -1) + { + printf("minishell: %s: command not found\n", cmd->args[0]); + // printf("cmd->args[0] = %s\n", cmd->args[0]); + return (1); + exit(0); + } + } + char *tmp = ft_strjoin(paths[i],"/"); + tmp = ft_strjoin(tmp, cmd->args[0]); + if (access(tmp, F_OK | X_OK) == 0) + { + lwa = tmp; + break; + } + i++; + } + if(execve(lwa, cmd->args,envi) == -1) + { + printf("minishell: %s: command not found\n", cmd->args[0]); + printf("cmd->args[0] = %s\n", cmd->args[0]); + return (1); + exit(0); + } + return (0); + + +} +void execute_built_ins(t_command *cmd, t_env *envp) +{ + if (ft_strcmp(cmd->args[0], "echo") == 0) + ft_echo(cmd); + else if (ft_strcmp(cmd->args[0], "cd") == 0) + ft_cd(cmd); + else if (ft_strcmp(cmd->args[0], "pwd") == 0) + ft_pwd(); + else if (ft_strcmp(cmd->args[0], "export") == 0) + ft_export(cmd, &envp); + else if (ft_strcmp(cmd->args[0], "unset") == 0) + ft_unset(cmd, &envp); + else if (ft_strcmp(cmd->args[0], "env") == 0) + ft_env(cmd, &envp); + else if (ft_strcmp(cmd->args[0], "exit") == 0) + ft_exit(); +} +int ft_lst_size(t_command *cmd) +{ + int i =0; + while(cmd) + { + i++; + cmd = cmd->next; + } + return i; +} + +void signal_handler(int sig) +{ + if (sig == SIGINT) + { + ft_putstr_fd("\n", 1); + rl_on_new_line(); + //This function call is likely used to move the cursor to a new line. + //It ensures that the next output or user input will start on a new line + // rl_replace_line("", 0); + //eplaces the current input line with an empty string + rl_redisplay(); + //After modifying the input line, this function is called to refresh the display + } + + if(sig == SIGQUIT) + ; +} + + +int execute_the_shOt(t_command* cmd,t_env *g_env, char **envp) +{ + int old; + t_command *tmp =NULL; + tmp = cmd; + + int fd[2] = {-1,-1}; + if(!cmd) + return (77); + while (cmd) + { + execute_built_ins(cmd, g_env); + old = fd[0]; // -1; + if (pipe(fd) != 0) + { + perror("failed to create a pipe"); + return(2); + } + cmd->pid = fork(); + if (cmd->pid == 0) + { + close(fd[0]); + if (cmd->next) + { + dup2(fd[1],1); + close(fd[1]); + } + if(cmd->fd.fd_out != 1) + { + dup2(cmd->fd.fd_out,1); + close(cmd->fd.fd_out); + } + if (old != -1) + { + dup2(old, 0); + close(old); + } + if(cmd->fd.fd_in != 0) + { + dup2(cmd->fd.fd_in,0); + close(cmd->fd.fd_in); + } + execute_builtins(cmd, envp); + + } + else if (cmd->pid > 0) + { + close(fd[1]); + if (old != -1) + close(old); + } + else + { + + perror("fork() failed"); + return (1); + } + + cmd = cmd->next; + } + close(fd[1]); + while(tmp) + { + waitpid(tmp->pid,NULL,0); + tmp = tmp->next; + } + return (0); +} \ No newline at end of file diff --git a/execute.c b/execute.c index 6c83c4f..ead49b6 100644 --- a/execute.c +++ b/execute.c @@ -124,7 +124,7 @@ int check_key(char *str, char *arg) ft_putstr_fd("minishell: export: `", 2); ft_putstr_fd(str, 2); ft_putendl_fd("': not a valid identifier", 2); - exit_status = 1; + exit_st.status = 1; return(45); } p = ft_strdup(str); @@ -135,7 +135,7 @@ int check_key(char *str, char *arg) ft_putstr_fd("minishell: export: `", 2); ft_putstr_fd(arg, 2); ft_putendl_fd("': not a valid identifier", 2); - exit_status = 1; + exit_st.status = 1; free(p); return (45); } @@ -167,7 +167,7 @@ void create_key_value(char *arg, t_env **g_env) { ft_putstr_fd("minishell: export: `", 2); ft_putstr_fd(arg, 2); - exit_status = 1; + exit_st.status = 1; ft_putendl_fd("': not a valid identifier", 2); return ; } @@ -175,7 +175,10 @@ void create_key_value(char *arg, t_env **g_env) j++; key = ft_substr(arg, 0, j); if(check_key(key,arg) == 45) + { + exit_st.status = 1; return ; + } value = NULL; if (arg[j] == '+') @@ -233,7 +236,7 @@ void ft_export(t_command *cmd, t_env **p_env) { if(check_key(cmd->args[1], cmd->args[1]) == 45) { - exit_status = 1; + exit_st.status = 1; return ; } } @@ -248,13 +251,13 @@ void ft_export(t_command *cmd, t_env **p_env) if(cmd->args[1] == NULL) print_export(p_env); } -void ft_cd(t_command *cmd) +void ft_cd(t_command *cmd , t_env **g_env) { - const char *home; - home = getenv("HOME"); - - - char *path; + const char *home = getenv("HOME"); + char *path = getenv("PWD"); + t_env *tmp; + tmp = *g_env; + if (cmd->args[1] == NULL) { if (home == NULL) @@ -262,7 +265,7 @@ void ft_cd(t_command *cmd) ft_putstr_fd("minishell: cd: HOME not set\n", 2); return ; } - path = ft_strdup(home); + path = ft_strdup(home); } else if (ft_strcmp(cmd->args[1], "~") == 0) { @@ -271,7 +274,7 @@ void ft_cd(t_command *cmd) if (home == NULL) { - ft_putstr_fd("minishell: cd: HOME not set\n", 2); + home = ft_strdup(""); return ; } if(cmd->args[2] == NULL) @@ -284,7 +287,6 @@ void ft_cd(t_command *cmd) ft_putstr_fd("minishell: cd: enviroment is unset \n", 2); return ; } - path = ft_strdup(getenv("OLDPWD")); if (path == NULL) { ft_putstr_fd("minishell: cd: OLDPWD not set\n", 2); @@ -302,11 +304,23 @@ void ft_cd(t_command *cmd) ft_putstr_fd("minishell: cd: ", 2); ft_putstr_fd(cmd->args[1], 2); ft_putstr_fd(": No such file or directory\n", 2); - exit_status = 1; + exit_st.status = 1; return ; } - if(path) - free(path); + tmp = *g_env; + if (path) + { + while (tmp) + { + if(ft_strcmp(tmp->key, "PWD") == 0) + { + free(tmp->value); + tmp->value = ft_strdup(path); + } + tmp = tmp->next; + } + } + } void ft_pwd() @@ -353,7 +367,7 @@ void ft_exit(t_command *cmd) ft_putstr_fd("exit\n", 1); ft_putstr_fd("minishell: exit: ", 2); ft_putstr_fd(cmd->args[1], 2); - exit_status = 255; + exit_st.status = 255; ft_putstr_fd(": numeric argument required\n", 2); exit(255); } @@ -362,7 +376,7 @@ void ft_exit(t_command *cmd) if(cmd->args[2] != NULL) { ft_putstr_fd("exit\n", 1); - exit_status = 1; + exit_st.status = 1; ft_putstr_fd("minishell: exit: too many arguments\n", 2); exit(1); @@ -370,40 +384,28 @@ void ft_exit(t_command *cmd) else { ft_putstr_fd("exit\n", 1); - exit_status = atoi(cmd->args[1]); + exit_st.status = atoi(cmd->args[1]); exit(atoi(cmd->args[1])); } } } } -int parsing_unset(char *str) -{ - int i = 0; - while(str[i]) - { - if(ft_isalnum(str[i]) == 0 && str[i] != '_') - { - ft_putstr_fd("minishell: unset: `", 2); - ft_putstr_fd(str, 2); - ft_putendl_fd("': not a valid identifier", 2); - exit_status = 1; - return (1); - } - i++; - } - return (0); -} void ft_unset(t_command *cmd, t_env **g_env) { t_env *tmp = *g_env; + extern char **environ; t_env *prev = NULL; int i = 1; + + if(cmd->args[1] && !ft_strcmp(cmd->args[1], "PATH")) + { + exit_st.path = ft_strdup(""); + exit_st.is_unset = 1; + } while (cmd->args[i]) { - if(parsing_unset(cmd->args[i]) == 1) - return ; tmp = *g_env; while (tmp) { @@ -424,23 +426,27 @@ void ft_unset(t_command *cmd, t_env **g_env) i++; } } -char **path_splitted() +char **path_splitted(t_env **g_env) { extern char **environ; int i; - char *path; char **paths; + t_env *tmp; + tmp = *g_env; i = 0; - if(!*environ) - path = ft_strdup("/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:"); - while (environ[i]) + if(!*environ && !exit_st.is_unset) + exit_st.path =ft_strdup("/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:."); + while(tmp) { - if (ft_strncmp(environ[i], "PATH=", 5) == 0) - path = ft_strdup(environ[i] + 5); - i++; + if(ft_strcmp(tmp->key, "PATH") == 0) + exit_st.path = ft_strdup(tmp->value); + tmp = tmp->next; } - paths = ft_split(path, ':'); + if(!exit_st.path) + exit_st.path = ft_strdup(" "); + // printf("%s\n", exit_st.path); + paths = ft_split(exit_st.path, ':'); return(paths); } @@ -453,13 +459,10 @@ void norm_help(t_command *cmd , int j ,int existance ,char c) { if(existance == -1) { - printf("minishell: %s: No such file or directory\n", cmd->args[0]); - exit(1); - } - else - { - printf("minishell : %s is a directory\n", cmd->args[0]); - exit(1); + ft_putstr_fd("minishell: ", 2); + ft_putstr_fd(cmd->args[0], 2); + ft_putstr_fd(": No such file or directory\n", 2); + exit(127); } } } @@ -482,7 +485,7 @@ void _manipulate_files(int permission, int existance , t_command *cmd) } } -void check_filepermission_ndexistance(t_command *cmd) +int check_filepermission_ndexistance(t_command *cmd) { int file_existance; int file_permission; @@ -491,46 +494,62 @@ void check_filepermission_ndexistance(t_command *cmd) file_permission = access(cmd->args[0], X_OK); if((cmd->args[0][0] == '.' || cmd->args[0][0] == '/')) + { _manipulate_files(file_permission,file_existance,cmd); + return (1); + } + return (0); } -void execute_builtins(t_command *cmd ,char **envi) +void execute_bin(t_command *cmd ,char **envi, t_env **g_env) { - char *lwa; + char *lwa = ft_strdup(""); + extern char **environ; int i; char **paths; i = 0; - paths = path_splitted(); - while(paths[i] ) + paths = path_splitted(g_env); + while(paths[i]) { - check_filepermission_ndexistance(cmd); + // printf("number %d of %s\n", i, paths[i]); char *tmp = ft_strjoin(paths[i],"/"); tmp = ft_strjoin(tmp, cmd->args[0]); - - if (access(tmp, F_OK ) == 0) - lwa = tmp; + if (access(tmp, F_OK| X_OK) == 0) + lwa = ft_strdup(tmp); + if(check_filepermission_ndexistance(cmd)) + lwa = ft_strdup(cmd->args[0]); i++; } + if(execve(lwa, cmd->args,envi) == -1) { + exit_st.status = 127; + if(!*environ || !ft_strcmp(exit_st.path, " ")) + { + ft_putstr_fd("minishell: ", 2); + ft_putstr_fd(cmd->args[0], 2); + ft_putstr_fd(": No such file or directory\n", 2); + exit(127); + } + if(check_filepermission_ndexistance(cmd)) + { + ft_putstr_fd("minishell: ", 2); + ft_putstr_fd(cmd->args[0], 2); + ft_putstr_fd(": is a directory \n", 2); + exit(126); + } lwa = cmd->args[0]; ft_putstr_fd("minishell: ", 2); - exit_status = 127; ft_putstr_fd(lwa, 2); ft_putstr_fd(": command not found\n", 2); - exit(exit_status); - } - else - { - exit_status = 0; - exit(0); + exit(exit_st.status); } } int checkfor_builtins(t_command *cmd) { if(ft_strcmp(cmd->args[0], "echo") == 0) - return(44); + return(1); else if(ft_strcmp(cmd->args[0], "cd") == 0) return(1); else if(ft_strcmp(cmd->args[0], "export") == 0) @@ -549,22 +568,22 @@ int checkfor_builtins(t_command *cmd) int execute_built_ins(t_command *cmd, t_env **envp) { int in, out; - if(cmd->fd.fd_in != 0) - { - in = dup(0); - dup2(cmd->fd.fd_in, 0); - close(cmd->fd.fd_in); - } - if(cmd->fd.fd_out != 1) - { - out = dup(1); - dup2(cmd->fd.fd_out, 1); - close(cmd->fd.fd_out); - } + if(cmd->fd.fd_in != 0) + { + in = dup(0); + dup2(cmd->fd.fd_in, 0); + close(cmd->fd.fd_in); + } + if(cmd->fd.fd_out != 1) + { + out = dup(1); + dup2(cmd->fd.fd_out, 1); + close(cmd->fd.fd_out); + } if(ft_strcmp(cmd->args[0], "echo") == 0) ft_echo(cmd); else if(ft_strcmp(cmd->args[0], "cd") == 0 && lstsize(cmd) == 1) - ft_cd(cmd); + ft_cd(cmd, envp); else if(ft_strcmp(cmd->args[0], "pwd") == 0) ft_pwd(); else if(ft_strcmp(cmd->args[0], "export") == 0) @@ -575,20 +594,20 @@ int execute_built_ins(t_command *cmd, t_env **envp) ft_unset(cmd, envp); else if(ft_strcmp(cmd->args[0], "exit") == 0) ft_exit(cmd); - else - return (0); - if(cmd->fd.fd_in != 0) - { - dup2(in, 0); - close(in); - } - if(cmd->fd.fd_out != 1) - { - dup2(out, 1); - close(out); - } - + if (cmd->fd.fd_in != 0) + { + dup2(in, 0); + close(in); + } + if (cmd->fd.fd_out != 1) + { + dup2(out, 1); + close(out); + } + return (1); + + } int ft_lst_size(t_command *cmd) @@ -613,36 +632,37 @@ void signal_handler(int signal) { void handle_child_process(t_command *cmd, int *fd ,int old) { - if (cmd->next) - { - dup2(fd[1],1); - close(fd[1]); - } - if(cmd->fd.fd_out != 1) - { - if(cmd->fd.fd_out == -1) - { - exit_status = 1; - exit(1); - } - dup2(cmd->fd.fd_out,1); - close(cmd->fd.fd_out); - } - if (old != -1) + close(fd[0]); + if (cmd->next) + { + dup2(fd[1],1); + close(fd[1]); + } + if(cmd->fd.fd_out != 1) + { + if(cmd->fd.fd_out == -1) { - dup2(old, 0); - close(old); + exit_st.status = 1; + exit(1); } - if(cmd->fd.fd_in != 0) + dup2(cmd->fd.fd_out,1); + close(cmd->fd.fd_out); + } + if (old != -1) + { + dup2(old, 0); + close(old); + } + if(cmd->fd.fd_in != 0) + { + if(cmd->fd.fd_in == -1) { - if(cmd->fd.fd_in == -1) - { - exit_status = 1; - exit(1); - } - dup2(cmd->fd.fd_in,0); - close(cmd->fd.fd_in); + exit_st.status = 1; + exit(1); } + dup2(cmd->fd.fd_in,0); + close(cmd->fd.fd_in); + } } @@ -663,36 +683,78 @@ int execute_the_shOt(t_command* cmd,t_env **g_env, char **envp) t_command *tmp = NULL; tmp = cmd; int fd[2] = {-1,-1}; - - if(!cmd) - return (77); - int done = 0; - while (cmd && cmd->args[0]) + if(!cmd->args[0]) + { + if(!cmd->next) + return (0); + } + if (!cmd->next) { a = checkfor_builtins(cmd); - old = fd[0]; // -1; - protection(fd); - if(a) - { + if(a == 1) execute_built_ins(cmd,g_env); - done = 1; + if(a == 0) + { + cmd->pid = fork(); + if(cmd->pid == 0) + { + if(cmd->fd.fd_out != 1) + { + if(cmd->fd.fd_out == -1) + { + exit_st.status = 1; + exit(1); + } + dup2(cmd->fd.fd_out,1); + close(cmd->fd.fd_out); + } + if(cmd->fd.fd_in != 0) + { + if(cmd->fd.fd_in == -1) + { + exit_st.status = 1; + exit(1); + } + dup2(cmd->fd.fd_in,0); + close(cmd->fd.fd_in); + } + execute_bin(cmd, envp, g_env); + } + else if(cmd->pid > 0) + { + int a = 0; + waitpid(cmd->pid, &a, 0); + if(a != 0) + exit_st.status = a / 256; + else + exit_st.status = 0; + } + else + { + perror("fork() failed"); + return (1); + } } + return (0); + } + while (cmd) + { + if(cmd->args[0]) + a = checkfor_builtins(cmd); + old = fd[0]; // -1; + protection(fd); cmd->pid = fork(); if (cmd->pid == 0) { handle_child_process(cmd,fd,old); - if(a == 0 && done == 0) - execute_builtins(cmd, envp); - exit(exit_status); + if(a == 1) + execute_built_ins(cmd,g_env); + if(a == 0 ) + execute_bin(cmd, envp, g_env); + exit(exit_st.status); } else if (cmd->pid > 0) { - waitpid(cmd->pid, &exit_status, 0); - int a = 0; - if(a != 0) - exit_status = a / 256; - else - exit_status = 0; close(fd[1]); if (old != -1) close(old); @@ -706,11 +768,16 @@ int execute_the_shOt(t_command* cmd,t_env **g_env, char **envp) cmd = cmd->next; } close(fd[1]); + int s = 0; while(tmp) { - waitpid(tmp->pid, &exit_status, 0); + waitpid(tmp->pid, &s, 0); tmp = tmp->next; } + if(s != 0) + exit_st.status =s / 256; + else + exit_st.status = 0; return (0); } diff --git a/execute.o b/execute.o new file mode 100644 index 0000000..6230ef4 Binary files /dev/null and b/execute.o differ diff --git a/exit.o b/exit.o new file mode 100644 index 0000000..90009be Binary files /dev/null and b/exit.o differ diff --git a/expander.c b/expander.c index 29d5eb9..009a60f 100644 --- a/expander.c +++ b/expander.c @@ -259,7 +259,7 @@ char *ft_expand(char *str, t_env **g_env) i = hehe(str, i); if (str[i] == '$' && str[i + 1] == '?') { - p = ft_itoa(exit_status); + p = ft_itoa(exit_st.status); str = handler_value(str, &i, 1, p); free(p); continue ; diff --git a/expander.o b/expander.o new file mode 100644 index 0000000..4bf289c Binary files /dev/null and b/expander.o differ diff --git a/lexing.c b/lexing.c index e75b454..4d225e4 100644 --- a/lexing.c +++ b/lexing.c @@ -221,11 +221,13 @@ int pipe_line_syntax(t_lexer **list) if (tmp->prev == NULL || tmp->next == NULL) { printf("syntax error near unexpected token `|'\n"); + exit_st.status = 2; return (1); } if (help_token(&tmp->prev) == 0 || help_token(&tmp->next) == 0) { printf("syntax error near unexpected token `|'\n"); + exit_st.status = 2; return (1); } } @@ -252,6 +254,7 @@ int redir_syntax(t_lexer **list) if (tmp->next == NULL || tmp->next->token != WORD) { printf("syntax error near unexpected token `newline'\n"); + exit_st.status = 2; return (1); } } @@ -277,6 +280,7 @@ int quote_error(char *str) if (str[i] == '\0') { printf("syntax error near unexpected token `%c'\n", str[i - 1]); + exit_st.status = 2; return (1); } } diff --git a/lexing.o b/lexing.o new file mode 100644 index 0000000..1575f80 Binary files /dev/null and b/lexing.o differ diff --git a/libft.o b/libft.o new file mode 100644 index 0000000..6ddddaf Binary files /dev/null and b/libft.o differ diff --git a/list.o b/list.o new file mode 100644 index 0000000..b6d88cb Binary files /dev/null and b/list.o differ diff --git a/ls b/ls new file mode 100644 index 0000000..7d9c73b --- /dev/null +++ b/ls @@ -0,0 +1,26 @@ +Makefile +env.c +env.o +execute.c +execute.o +exit.c +exit.o +expander.c +expander.o +lexing.c +lexing.o +libft.c +libft.o +list.c +list.o +ls +main.c +main.o +minishell +minishell.h +parsing.c +parsing.o +t.c +t.o +test.c +test.o diff --git a/main.c b/main.c index 8e9bbe1..221f1c9 100644 --- a/main.c +++ b/main.c @@ -79,8 +79,10 @@ int main(int ac ,char **av , char **env) // sa.sa_handler = signal_handler; sigemptyset(&sa.sa_mask); + exit_st.status = 0; + exit_st.path = NULL; + exit_st.is_unset = 0; sa.sa_flags = 0; - exit_status = 0; cmd = NULL; (void)ac; (void)av; @@ -112,7 +114,7 @@ int main(int ac ,char **av , char **env) parse_args(&lexer, &cmd, &p_env); execute_the_shOt(cmd,&p_env,env); } - + // while(lexer != NULL) // { diff --git a/main.o b/main.o new file mode 100644 index 0000000..a62bb48 Binary files /dev/null and b/main.o differ diff --git a/minishell b/minishell new file mode 100755 index 0000000..e33ea79 Binary files /dev/null and b/minishell differ diff --git a/minishell.h b/minishell.h index e8445e3..c83f41a 100644 --- a/minishell.h +++ b/minishell.h @@ -6,7 +6,7 @@ /* By: heddahbi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/07/18 16:47:00 by abel-hid #+# #+# */ -/* Updated: 2023/08/01 15:06:30 by heddahbi ### ########.fr */ +/* Updated: 2023/08/03 09:12:14 by heddahbi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,13 +61,14 @@ typedef struct s_env struct s_env *next; } t_env; -// typedef struct s_exit -// { -// int status; -// } t_exit; + typedef struct s_exit +{ + int status; + char *path; + int is_unset; +} t_exit; -// t_exit *exit_st; -int exit_status; +t_exit exit_st; int lexing(t_lexer **list, char *line); void craete_env(char **env_list, t_env **g_env); char **ft_split(char const *s, char c); diff --git a/parsing.c b/parsing.c index 2f37442..b13ab17 100644 --- a/parsing.c +++ b/parsing.c @@ -300,13 +300,17 @@ int handel_redirout(char *str_next, int fd, t_env **g_env, int a) fd = open(str, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd == -1) { - printf("minishell: : No such file or directory\n"); + ft_putstr_fd("minishell: ", 2); + ft_putstr_fd(str, 2); + ft_putstr_fd(": No such file or directory\n", 2); return (free(str), -1); } } else { - printf("minishell: %s: ambiguous redirect\n", str_next); + ft_putstr_fd("minishell: ", 2); + ft_putstr_fd(str_next, 2); + ft_putstr_fd(": ambiguous redirect\n", 2); return (-1); } free(str); @@ -328,13 +332,17 @@ int handel_redirin(char *str_next, int fd, t_env **g_env, int a) fd = open(str, O_RDONLY, 0644); if (fd == -1) { - printf("minishell: : No such file or directory\n"); + ft_putstr_fd("minishell: ", 2); + ft_putstr_fd(str, 2); + ft_putstr_fd(": No such file or directory\n", 2); return (free(str), -1); } } else { - printf("minishell: %s: ambiguous redirect\n", str_next); + ft_putstr_fd("minishell: ", 2); + ft_putstr_fd(str_next, 2); + ft_putstr_fd(": ambiguous redirect\n", 2); return (-1); } free(str); @@ -572,4 +580,4 @@ void heredoc(t_lexer **lexer, t_env **g_env) } tmp = tmp->next; } -} +} \ No newline at end of file diff --git a/parsing.o b/parsing.o new file mode 100644 index 0000000..af99fd9 Binary files /dev/null and b/parsing.o differ diff --git a/s b/s new file mode 100644 index 0000000..2a03cbd --- /dev/null +++ b/s @@ -0,0 +1,27 @@ +Makefile +env.c +env.o +execute.c +execute.o +exit.c +exit.o +expander.c +expander.o +lexing.c +lexing.o +libft.c +libft.o +list.c +list.o +ls +main.c +main.o +minishell +minishell.h +parsing.c +parsing.o +s +t.c +t.o +test.c +test.o diff --git a/t.o b/t.o new file mode 100644 index 0000000..c534e5f Binary files /dev/null and b/t.o differ diff --git a/test.o b/test.o new file mode 100644 index 0000000..2d7543e Binary files /dev/null and b/test.o differ