int sh_search_builtin(char *av, char ***env) { int state; char **option; if (!av || !env || !(*av)) return (0); state = 0; option = ft_strsplit(av, ' '); if (option[0]) { if (ft_strcmp(option[0], "cd") == 0) sh_builtin_cd(option, env), state = 1; else if (ft_strcmp(option[0], "exit") == 0) sh_builtin_exit(option), state = 1; else if (ft_strcmp(option[0], "env") == 0) sh_builtin_env(option, env), state = 1; else if (ft_strcmp(option[0], "setenv") == 0) sh_builtin_setenv(option, env), state = 1; else if (ft_strcmp(option[0], "unsetenv") == 0) sh_builtin_unsetenv(option, env), state = 1; else if (ft_strcmp(option[0], "clear") == 0) ft_putstr("\033[1;1H\033[2J"), state = 1; } ft_arrfree(&option); return (state); }
char *autocomp_command(char *str, char *path) { struct dirent *rd; DIR *rep; int i; int j; char **tb; char **sp; i = -1; j = -1; sp = ft_strsplit(path, ':'); tb = autocomp_size(str, path); if (!tb) return (str); while (sp[++j]) { if (!(rep = opendir(sp[j]))) return (NULL); while ((rd = readdir(rep))) if (!(ft_strncmp(str, rd->d_name, ft_strlen(str)))) tb[++i] = ft_strdup(rd->d_name); closedir(rep); } tb[++i] = NULL; ft_arrfree(&sp); return (ft_select(tb)); }
char *autocomp_word(char *str, char *path) { struct dirent *rd; DIR *rep; int i; char **tb; i = -1; if (!str) return (ft_select(autocomp_all(path))); if ((i = get_tab_size(path, str) == 0)) return (str); if (!(tb = (char**)malloc(sizeof(char*) * i + 1))) ft_exit("Malloc error"); if (!(rep = opendir(path))) ft_exit("Opendir failed"); i = -1; while ((rd = readdir(rep))) if (!(ft_strncmp(str, rd->d_name, ft_strlen(str)))) tb[++i] = ft_strdup(rd->d_name); if (i == -1) { ft_arrfree(&tb); return (str); } tb[i + 1] = NULL; closedir(rep); if (tb[1] && tb[1][0] == '\0') return (tb[0]); return (ft_select(tb)); }
int ft_traitement(char *str) { int i; char *path; char **parsed; str = ft_formatting(str); i = ft_opera_handler(str); if (i >= 0) { ft_strdel(&str); return (i); } if (!(parsed = parse_command(str))) { ft_strdel(&str); return (-1); } i = ft_check_builtin(parsed); path = get_val("PATH"); if (i >= 0) g_status = g_listefonctions[i](parsed); else if (!ft_check_exe(parsed, path)) return (ft_not_found(&parsed, &str)); ft_arrfree(&parsed); ft_strdel(&str); return (1); }
int ft_not_found(char ***parsed, char **str) { g_status = 0; ft_putstr_fd("42sh"": Command not found: ", 2); ft_putendl_fd(*parsed[0], 2); ft_strdel(str); ft_arrfree(parsed); return (0); }
static int ft_truc(char **tb) { pid_t pid; pid = fork(); save_cpid(&pid); if (pid == 0) ft_trafic_regulator(tb); else wait(NULL); pid = 0; save_cpid(&pid); ft_arrfree(&tb); return (1); }