float ft_get_coef(char *str, t_magic *magic) { float coef; float b; char **tab; int pow; pow = -1; coef = 0; if (ft_check_coef(str) == 1) { tab = ft_strsplit(str, '.'); (ft_tablen(tab) == 1) ? coef = atoi(tab[0]) : ft_putstr(""); if (ft_tablen(tab) == 2) { magic->flo = 1; b = atoi(tab[1]); while (++pow < ft_strlen(tab[1])) b = (float)b / 10; coef = atoi(tab[0]) + b; } else if (ft_tablen(tab) != 1) magic->error = 1; } else magic->error = 1; return (coef); }
static void ft_realloc_alias(char *value, char ***tab) { char **cmd; char **tmp; int len; int i; int j; tmp = ft_tabdup(*tab); ft_tabdel(tab); cmd = ft_strsplit_space(value); i = ft_tablen(tmp) - 1; j = ft_tablen(cmd); len = i + j; *tab = (char **)malloc(sizeof(char *) * (len + 1)); (*tab)[len] = NULL; while (--len >= 0) { if (i > 0) (*tab)[len] = ft_strdup(tmp[i--]); else (*tab)[len] = ft_strdup(cmd[--j]); } ft_tabdel(&cmd); ft_tabdel(&tmp); }
void delete_liaison_empty(t_control *control) { t_archi *tmp; t_archi *tmp2; tmp = control->archi; while (tmp) { if (ft_tablen(tmp->liaisons) == 0) { if (tmp->prev == NULL && tmp->next) { control->archi = tmp->next; free(tmp); } else if (tmp->prev != NULL && tmp->next != NULL) { tmp2 = tmp->prev; tmp2->next = tmp->next; tmp->next->prev = tmp2; free(tmp); } else if (tmp->prev && tmp->next == NULL) { tmp2 = tmp->prev; tmp2->next = NULL; free(tmp); } } tmp = tmp->next; } }
t_raw read_file(int fd) { char *text; t_raw raw; int er; int len; int size; len = 0; size = 100000; raw.lines = xmalloc(size * sizeof(t_rawline*)); raw.width = 0; while ((er = get_next_line(fd, &text)) > 0) { raw.lines[len].data = ft_strsplit(text, ' '); if (!raw.lines[len].data) error_memory(); raw.lines[len].len = ft_tablen(raw.lines[len].data); raw.width = MAX(raw.width, raw.lines[len].len); if (++len == size) error_file(); free(text); } if (er < 0) error_file(); raw.height = len; return (raw); }
int builtin_unsetenv(char **av) { int i; int j; int index; char **tab; i = 0; j = 0; if (!av[1]) return (-1); if ((index = find_env(av[1])) == -1) return (0); tab = (char **)ft_memalloc(sizeof(char *) * ft_tablen(g_handler.env)); if (!tab) return (-42); while (g_handler.env[i]) { if (index != i) tab[j++] = ft_strdup(g_handler.env[i]); i++; } ft_free_tab(&g_handler.env); g_handler.env = tab; return (0); }
int builtin_setenv(char **av, char ***env) { int x; int ret; x = 0; if (ft_tablen(av) == 1) { ft_putendl_fd("Error : not enough arguments to setenv", 2); return (1); } while (av[++x]) if (ft_strchr(av[x], '=') && *(ft_strchr(av[x], '=') + 1) && av[x][0] && (av[x] = change_into_number(av[x])) && av[x][0] != '=') { if ((ret = check_if_in_env(av[x], *env)) >= 0) { free((*env)[ret]); (*env)[ret] = ft_strdup(av[x]); } else if ((add_variable_to_env(av[x], env)) == -1) { ft_putendl_fd("Error : could'nt allocate enough memory", 2); return (1); } } return (0); }
static void ft_completion(t_data *d, char **result, char *name) { int save; if (!result) return ; if (ft_tablen(result) == 1) ft_tab_replace(d, result[0], name); else { if (ft_prefix_completion(d, result, name)) { save = d->line->len; ft_print_tab(result); ft_putchar('\n'); ft_prompt(0); ft_putstr(d->line->str); while (save > d->line->index) { tputs(tgetstr("le", NULL), 1, ft_int_putchar); save--; } } } }
int ft_setenv(char *line, t_env *env) { char **tmp; int ret; int len; ret = 0; tmp = ft_strsplit(line, ' '); if (!tmp[1]) { ft_env(env, tmp); return (0); } len = ft_tablen(tmp); if (!tmp[2]) tmp[2] = ""; ret = ft_check_line_env(env, tmp[1]); if (ft_strcmp(tmp[1], "PATH") == 0) ft_error_setenv(2); else if (ret > 0 && len <= 3) ft_replace_line_env(env, tmp[1], tmp[2]); else if (ret == 0 && len <= 3) ft_add_line(env, tmp[1], tmp[2]); else ft_error_setenv(1); return (0); }
void ft_action_inventaire(t_player *player, t_env *e) { char **tab; int i; int len; tab = ft_strsplit(player->read, ','); i = 0; len = ft_tablen((void **)tab); printf("ref : %d %d fin\n", player->y, player->x); while (tab[i]) { st_check_tab(player, tab[i]); i++; } player->inventory.status = 1; player->cur_action = player->action[0]; if (player->cur_action != NULL) { ft_destroy_action(player, 0); dprintf(player->sock, "%s", player->cur_action); printf("current : %s", player->cur_action); } ft_delfulltab((void ***)&tab); e->graph.modif = 0; }
static int exec_op(char *op, char *cmd, char **env) { char **tab; int ret; if (!(tab = ft_strsplit(cmd, ' '))) return (0); if (ft_tablen(tab) != 3) { ft_destroysplit(tab); return (ft_error("Format : <cmd|file> <operator> <cmd|file>", NULL)); } if (ft_strequ(op, "|")) { ft_pipe(env, tab[0], tab[2]); ft_destroysplit(tab); return (1); } else { ret = ft_redir(env, tab[0], tab[1], tab[2]); ft_destroysplit(tab); return (ret); } }
void ft_parse_input(t_shun **shell) { if (*(*shell)->input) { (*shell)->argv = ft_parse_get_args((*shell)->input); (*shell)->argc = ft_tablen((*shell)->argv); if ((*shell)->argc != 0) { if (ft_strcmp(ft_strtolower((*shell)->argv[0]), "exit") == 0) ft_exit(&(*shell)); else if (ft_strcmp(ft_strtolower((*shell)->argv[0]), "env") == 0) ft_print_environ(*shell); else if (ft_strcmp(ft_strtolower((*shell)->argv[0]), "setenv") == 0) ft_setenv(&(*shell)); else if (ft_strcmp(ft_strtolower((*shell)->argv[0]), "unsetenv") == 0) ft_unsetenv(&(*shell)); else if (ft_strcmp(ft_strtolower((*shell)->argv[0]), "cd") == 0) ft_cd(&(*shell)); else if (ft_strcmp(ft_strtolower((*shell)->argv[0]), "prompt") == 0) ft_chg_prompt(&(*shell)); else if (ft_strcmp(ft_strtolower((*shell)->argv[0]), "pwd") == 0) ft_putendl(ft_get_pwd()); else ft_exec_bin(&(*shell)); } } }
static char *get_destination(char **av, char ***envp) { int len; char *path; len = ft_tablen(av); path = NULL; if (len == 1) { if (!(path = get_in_env(*envp, "HOME"))) ft_putendl_fd("Error : could'nt find HOME", 2); } else if (len == 2) { if (!strcmp(av[1], "-")) { if (!(path = get_in_env(*envp, "OLDPWD"))) ft_putendl_fd("Error : could'nt find OLDPWD", 2); } else path = av[1]; } else ft_putendl_fd("Error : too many arguments", 2); return (path); }
int ft_echo(char **tab) { int i; int option; option = 0; if ((ft_tablen((void **)tab)) > 1) { i = 1; while (tab[i] && tab[i][0] == '-') { if (ft_check_option_echo(tab[i], &option) == -1) break ; i++; } if (!tab[i] && (option == 1 || option == 3)) return (1); while (tab[i]) { st_check_str_echo(tab[i], option); i++; if (tab[i]) ft_putchar(' '); } } return (ft_echo_end(option)); }
t_map *map_parse(t_list *list) { t_map *map; char **tab; int x; int y; map = (t_map*)malloc(sizeof(t_map)); if (list && map && !(y = 0)) { map->y = ft_lstlen(list); map->wall = (int**)malloc(sizeof(int*) * map->y); while (list && (x = -1)) { tab = ft_strsplit(list->content, ' '); free(list->content); map->x = ft_tablen(tab); map->wall[y] = (int*)malloc(sizeof(int) * map->x); while (++x < map->x) map->wall[y][x] = atoi_free(tab[x]); list = list->next; y++; } } return (map); }
t_args *setenv_errors_controller(t_cmd *cmd) { t_args *args; t_error *error; if (!cmd || !(args = args_put(cmd, ""))) return (0); error = NULL; if (ft_tablen((void **)args->other) > 2) error = errors_init(ERR_MANY, ""); else if (ft_tablen((void **)args->other) >= 1 && ft_strchr(args->other[0], '=')) error = errors_init(ERR_FORMAT, ""); if (error) return (errors_controller(error, &setenv_errors_printer), NULL); return (args); }
static int build_cd_tris(char **s, char ***env) { char *buf; buf = NULL; if (ft_tablen(s) == 2 && access(s[1], F_OK) == -1) return (ft_putstr_fd("Path doesn't exist\n", 2), 1); if (ft_tablen(s) == 2 && access(s[1], X_OK) == 0) { set_env(env, "OLDPWD", getcwd(buf, 1024)); chdir(s[1]); set_env(env, "PWD", getcwd(buf, 1024)); return (1); } else if (ft_tablen(s) == 2 && access(s[1], X_OK) == -1) return (ft_putstr_fd("Access can not be granted\n", 2), 1); return (1); }
static void build_setenv_bis(char **s, char ***env) { char *tmp; if (ft_tablen(s) == 2 && ft_strchr(s[1], '=') == 0) { tmp = ft_strjoin(s[1], "="); ft_add_str_to_tab(env, tmp); free(tmp); } else if (ft_tablen(s) == 3 && ft_strchr(s[1], '=') == 0 && ft_strchr(s[2], '=') == 0) { tmp = ft_strjoin(s[1], "="); tmp = ft_strjoin(tmp, s[2]); ft_add_str_to_tab(env, tmp); free(tmp); } }
int ft_cd(char **tab) { if (ft_tablen((void **)tab) == 1) return (ft_cd_home()); else if (tab[1][0] == '~') return (ft_cd_tild(tab[1])); else if (tab[1][0] == '-') { if (tab[1][1] != '\0') return (st_check_option(&tab[1])); else return (ft_cd_least()); } else if (ft_tablen((void **)tab) > 2) return (ft_error_cd("cd", 4, NULL)); else return (ft_other_cd(tab[1], 0)); return (0); }
void set_setenv(char **av, char ***env) { char *str; if (ft_tablen(av) == 3) { if (ft_getenv(*env, av[1]) == NULL) { str = ft_strjoin(av[1], "="); str = ft_strjoin(str, av[2]); *env = ft_tabjoin(*env, str); } else change_env(*env, av[1], av[2]); } else if (ft_tablen(av) > 3) error(TOOMUCHARG, NULL); else error(TOOFEWARG, NULL); }
int build_cd(char **s, char ***env) { char *buf; buf = NULL; if (ft_tablen(s) >= 3) { ft_putstr_fd("cd: Too many argmuments\n", 2); return (1); } if (ft_tablen(s) == 1) { set_env(env, "OLDPWD", getcwd(buf, 1024)); chdir(get_env("HOME", *env)); set_env(env, "PWD", getcwd(buf, 1024)); return (1); } build_cd_bis(s, *env); return (build_cd_tris(s, env)); }
int build_setenv(char **s, char ***env) { char *tmp; if (ft_tablen(s) == 1) return (display_setenv(*env), 1); if (ft_tablen(s) > 3) return (ft_putendl_fd("setenv: Too many arguments", 2), 1); if ((s[1] && ft_strchr(s[1], '=')) || (s[2] && ft_strchr(s[2], '='))) return (ft_putendl_fd("setenv: Synthax error", 2), 1); if (get_env(s[1], *env) == NULL) build_setenv_bis(s, env); else if (get_env(s[1], *env)) { tmp = ft_strjoin(s[1], "="); tmp = ft_strjoin(tmp, s[2]); ft_replace_str_in_tab(env, ft_string_position(*env, s[1]), tmp); free(tmp); } return (1); }
t_dot *map_max(t_list *list, t_env *e) { t_dot *max; char **tab; max = dot(0, 0, 0); while (list) { max->y++; tab = ft_strsplit(list->content, ' '); if (ft_tablen(tab) > max->x) max->x = ft_tablen(tab); while (*tab) { map_min_max(e, ft_atoi(*tab)); tab++; } list = list->next; } return (max); }
void split_op_label(t_hero *hero) { char **tmpopts; int op; int line; line = 0; op = 0; tmpopts = (char **)malloc(sizeof(char *) * (ft_tablen(hero->opts) + 1)); hero->label_pos = ft_memalloc(sizeof(int) * (ft_tablen(hero->opts) + 1)); hero->label_name = ft_memalloc(sizeof(char*) * (ft_tablen(hero->opts) + 1)); hero->countlabel = 0; while (hero->opts[line]) { hero->opts[line] = ft_strtrimfree(hero->opts[line]); split_op_label_line(hero, tmpopts, &op, line); line++; } tmpopts[op] = NULL; ft_tabfree(hero->opts); hero->opts = tmpopts; }
int count_char(char **table) { int lengh; int i; i = 0; lengh = ft_tablen(table) - 1; while (table[i]) { lengh += ft_strlen(table[i]); i++; } return (lengh); }
void ft_update_env(char *path, char **args, t_env *e) { char *tmp; int i; i = ft_tablen(e->env); if (path) { tmp = args[0]; args[0] = path; path = tmp; free(path); } while (--i >= 0) { if (!ft_strncmp("_=", e->env[i], 2)) { free(e->env[i]); e->env[i] = ft_strjoin("_=", args[ft_tablen(args) - 1]); return ; } } }
int build_unsetenv(char **s, char ***env) { int i; i = 0; if (ft_tablen(s) == 1) return (ft_putendl("unsetenv: Too few arguments"), 1); while (s[++i]) { if (get_env(s[i], *env)) ft_del_tab_str(env, ft_string_position(*env, s[i])); } return (1); }
static int add_dir(char ***path_tab, char *path) { char **new_tab; int size; size = ft_tablen(*path_tab); new_tab = ft_tabdup(*path_tab, size + 1); new_tab[size] = ft_strdup_to_char(path, '/'); del_tab(*path_tab); *path_tab = new_tab; size = 0; while (path[size] && path[size] != '/') ++size; return (size); }
static void build_cd_bis(char **s, char **env) { char *tmp; if (ft_tablen(s) == 2) { if (s[1][0] == '~') { tmp = ft_strsub(s[1], 1, ft_strlen(s[1])); s[1] = ft_strjoin(get_env("HOME", env), tmp); free(tmp); } if (s[1][0] == '-') s[1] = get_env("OLDPWD", env); } }
void ft_free_tab(char **tab) { int i; int len; i = 0; len = ft_tablen(tab); while (i < len) { free(tab[i]); tab[i] = NULL; i++; } free(tab); tab = NULL; }
char *ft_itoa(int nbr) { char *r; int l; int s; s = 0; l = ft_tablen(n); r = (char*)malloc(sizeof(r) * (l + 1)); if (!r) return (NULL); if (n < 0) s = 1; if (n > 0) }