void redirection_gt(t_main *w, t_env *env, int append) { int fd[2]; char **coms; coms = ft_strsplit(w->line, '>'); coms[1] = ft_strtrim(coms[1]); if (append == 1) fd[0] = open(ft_strrw(coms[1]), O_RDWR | O_CREAT | O_APPEND, 0666); else fd[0] = open(ft_strrw(coms[1]), O_RDWR | O_CREAT, 0666); if (fd[0] < 0) { ft_strcpy(w->line, " "); ft_minishell(env, w); } else { fd[1] = dup(STDOUT_FILENO); dup2(fd[0], STDOUT_FILENO); close(fd[0]); ft_strcpy(w->line, ft_strtrim(coms[0])); ft_minishell(env, w); dup2(fd[1], STDOUT_FILENO); close(fd[1]); } }
void ft_cd(char *line) { int i; char *tmp; char *home; i = 0; home = gethome(); tmp = (char *)malloc(sizeof(char *) * (ft_strlen(line) + 1)); ft_strcpy(tmp, line); tmp = ft_strrw(tmp); tmp = ft_strnshift(tmp, 3); i = 0; if (tmp[0] == '~') { tmp = ft_strnshift(tmp, 1); while (tmp[0] == '/') tmp = ft_strnshift(tmp, 1); home = ft_strjoin(home, "/"); home = ft_strjoin(home, tmp); if (chdir(home) == -1) ft_putstr("No such file or directory.\n"); } else if (chdir(tmp) == -1) ft_putstr("No such file or directory.\n"); }
int ft_isbuiltin(t_env *env, char *line) { char **line2; int i; t_main w; i = 0; line2 = ft_strsplit(line, ' '); if (ft_strcmp(line2[0], "echo") == 0 && (i = 1)) { line = ft_strfcut(line, 5); ft_echo(line); } else if (ft_strncmp(line, "cd", 2) == 0 && (i = 1)) ft_cd(line); else if (ft_strncmp(line, "unsetenv", 7) == 0 && (i = 1)) env = ft_unsetenv(env); else if (ft_strncmp(line, "env", 2) == 0 && (i = 1)) print_env(env); else if (ft_strncmp(line, "setenv", 5) == 0 && (i = 1)) { line = ft_strrw(line); line2 = ft_strsplit(line, ' '); env = set_env(line2, env); } if (i == 1) ft_doublecoms(env, &w, 0); return (1); }
void redirection_lt(t_main *w, t_env *env) { char **coms; int fd[2]; coms = ft_strsplit(w->line, '<'); fd[0] = open(ft_strrw(coms[1]), O_RDWR); if (fd[0] == -1) { ft_putstr("File doesn't exist or cannot be opened.\n"); ft_strcpy(w->line, " "); ft_minishell(env, w); } else { w->line = ft_strnew((size_t)(ft_strlen(w->line) + 1)); fd[1] = dup(STDIN_FILENO); dup2(fd[0], STDIN_FILENO); close(fd[0]); ft_strcpy(w->line, coms[0]); ft_minishell(env, w); dup2(fd[1], STDIN_FILENO); close(fd[1]); } }