static int add_line(char *line, int ***grid, int *sizeX) { char **tab; int i; int *lint; tab = parse(line, " \t"); free(line); if (tab == NULL) return (my_error(EM)); i = 0; while (tab[i] != NULL) i += 1; if (*sizeX == 0) *sizeX = i; if (*sizeX != i || (lint = malloc(sizeof(*lint) * i)) == NULL) return (my_error(EM) + free_tab(tab) - 1); i = -1; while (tab[++i] != NULL) lint[i] = my_getnbr(tab[i]); free_tab(tab); if (add_to_grid(grid, lint) == 1 || lint[0] != 0 || lint[*sizeX - 1] != 0) return (my_error(WF)); return (0); }
void check_double_names(char **line, int i) { int n; char **tmpi; char **tmpn; while (line[i] && ft_strstr(line[i], " ")) { if (line[i][0] == '#') i++; n = i + 1; while (line[n] && ft_strstr(line[n], " ")) { if (line[n][0] == '#') n++; if (!ft_strstr(line[n], " ") || !line[n]) break ; tmpi = ft_strsplit(line[i], ' '); tmpn = ft_strsplit(line[n], ' '); if (ft_strequ(tmpi[0], tmpn[0])) exit_error("same name used twice for rooms.", 2); if (ft_strequ(tmpi[1], tmpn[1]) && ft_strequ(tmpi[2], tmpn[2])) exit_error("same position for two different rooms.", 2); n++; free_tab(tmpn); free_tab(tmpi); } i++; } ft_putendl("check_double_names OK"); }
void test_expr_to_tab() { char** tab; tab = expr_to_tab(" 56 * ((6+2) / ( 8-7)* 2%3)", "0123456789", "()+-*/%"); test_str(tab[0], "56"); test_str(tab[1], "*"); test_str(tab[2], "("); test_str(tab[3], "("); test_str(tab[4], "6"); test_str(tab[8], "/"); test_str(tab[16], "%"); test(tab[19] == 0); free_tab(tab); tab = expr_to_tab("0101|[1001$10101]", "01", "[]+-*|$"); test_str(tab[0], "0101"); free_tab(tab); //wrong operator tab = expr_to_tab(" 56 * ((6+2) / ( 8-7)* 2^3)", "0123456789", "()+-*/%"); test(tab == 0); //wrong operande tab = expr_to_tab(" 56 * ((6+2) / ( E-7)* 2^3)", "0123456789", "()+-*/%"); test(tab == 0); //Wrong syntax tab = expr_to_tab(" 56 * * ((6+2) / ( E-7)* 2^3)", "0123456789", "()+-*/%"); test(tab == 0); //good syntax tab = expr_to_tab(" 56 * ((6++-2) / ( 8-7)* 2%3)", "0123456789", "()+-*/%"); test(tab != 0); test_str(tab[5], "-"); free_tab(tab); }
int op_add(t_action *action, char *str, t_pos *pos) { int i; char c; char type; char **tab; char **list_arg; if ((c = 0) || !(pos->prog_size += 2) || !(i = -1) || !(action->identifier = 0x04) || !(action->args = malloc(sizeof(t_arg *) * 4)) || !(tab = str_to_word_tab(str, ',')) || !(list_arg = str_to_word_tab("1 1 1", ' ')) || init_args(action, 3)) return (1); while (tab[++i]) { if (i > 2 || (type = check_type(tab[i], pos)) == -1 || check_args(type, list_arg[i], pos) || fill_arg(type, tab[i], action->args[i], action->pos - i - 1)) return (1); add_prog_size(type, pos); c += (type << (6 - (i * 2))); } action->coding_byte = c; return (free_tab(tab), free_tab(list_arg), (i < 3) ? 1 : 0); }
static void manage_stdin(char **env, char **path, int count) { int i; char *line; char **cmd; cmd = NULL; line = NULL; while (1) { prompt(env, count); if (get_next_line(0, &line) > 0) { i = -1; cmd = ft_strsplit(line, ';'); ft_strdel(&line); while (cmd[++i]) env = parse_cmd(env, cmd, cmd[i], path); free(cmd); cmd = NULL; free_tab(path); ft_putchar('\n'); } count++; } free_tab(env); }
static int use_execve(t_tree *tree, t_env **env, char *cmd) { char **tab_env; char **tab_lex; int pid; int exec_val; exec_val = 0; if ((tab_env = env_to_tab(*env)) == NULL || ((tab_lex = lex_to_tab(tree->lexer)) == NULL) || ((pid = fork()) == -1)) return (my_perror(QUIT_ERR, QUIT_RET)); if (pid == 0) { if (execve(cmd, tab_lex, tab_env) == -1) return (my_perror(QUIT_ERR, QUIT_RET)); } else { if (wait(&exec_val) == -1) return (my_perror(QUIT_ERR, QUIT_RET)); exec_val = WEXITSTATUS(exec_val); } free_tab(tab_env); free_tab(tab_lex); free(cmd); return (exec_val); }
static char *check_access(t_tree *tree, t_env *env) { char **tab; int i_tab; char *cmd_and_path; i_tab = -1; if ((tab = my_str_to_tab(env->value, ':')) == NULL) return (NULL); while (tab[++i_tab] != 0) { if ((cmd_and_path = my_strcat(tab[i_tab], tree->lexer->content, 0)) == NULL) return (NULL); if (access(cmd_and_path, F_OK) == -1) free(cmd_and_path); else { free_tab(tab); return (cmd_and_path); } } free_tab(tab); return (NULL); }
char mysh_quit(t_arg *targ, UNUSED char *str) { int nb; int i; i = 0; if ((targ->wtab[1] && ((targ->wtab[1][0] != '-' && targ->wtab[1][0] <= '0') || targ->wtab[1][0] >= '9')) || targ->wtab[2]) return (my_perror(ERR_SYNT)); while (targ->wtab[1] && targ->wtab[1][++i]) if (targ->wtab[1][i] >= '9' || targ->wtab[1][i] <= '0') return (my_perror(ERR_SYNT)); targ->ex = 1; nb = (targ->wtab[1] != NULL) ? my_getnbr(targ->wtab[1]) : 0; if (targ->env != NULL) free_tab(targ->env); if (targ->wtab != NULL) free_tab(targ->wtab); if (targ->ptab != NULL) free_tab(targ->ptab); if (targ->pwd != NULL) free(targ->pwd); targ->env = NULL; write(1, "exit\n", 5); return (nb); }
void free_shell(t_shell sh) { free_tab(sh.set); free_tab(sh.env); free_tab(sh.alias); free_tab(sh.history); }
int free_all(t_core *core) { t_header *tmp; if (core != NULL) { if (core->re_write != NULL) free_tab(core->re_write); /* if (core->name_h != NULL) */ /* free(core->name_h); */ if (core->name_h_maj != NULL) free(core->name_h_maj); if (core->header != NULL) { tmp = core->header; while (tmp != NULL) { free_tab(tmp->line); tmp = tmp->nxt; } } free(core); } return (1); }
static char *my_msg_loop(char **dest_list, t_fd *self, t_fd *fds, char *msg) { int i; t_fd *ptr; char *str; i = 0; while (dest_list[i]) { ptr = fds; while (ptr && (!ptr->nick || strcmp(ptr->nick, dest_list[i]))) ptr = ptr->next; if (!ptr) { str = err_serv("401 ", self->nick, dest_list[i], " :No such nick\r\n"); free_tab(dest_list); free(msg); return (str); } if (fputstr(ptr->fd, msg) == -1) return (NULL); ++i; } free(msg); free_tab(dest_list); return (strdup("\r\n")); }
static int test_path(char **tab, char **env) { int i; char *path_in_str; char **path_in_array; char *cmd; int a; if ((path_in_str = get_var_env(env, "PATH=")) == NULL) path_in_str = create_path(); path_in_array = my_str_to_wordtab_pattern(path_in_str, ":"); i = -1; while (path_in_array[++i]) { cmd = concat_str(path_in_array[i], tab[0], '/'); if ((a = execve(cmd, tab, env)) != -1) { free(cmd); free_tab(path_in_array); return (a); } free(cmd); } free_tab(path_in_array); return (cmd_not_found(tab)); }
static void perform_cmd(t_client *client, char *cmd) { char **tab; int i; int n; tab = split_str(cmd, ' '); n = 0; while (tab && tab[n]) n++; if (n == 0) { free_tab(tab); return ; } i = 0; while (i < COMMANDS) { if (!my_strcmp_case(client->commands[i].str, tab[0])) { client->commands[i].func(client, n, tab); free_tab(tab); return ; } i++; } }
void free_all(t_data *data) { free(data->builtin); free_tab(data->builtins); free_tab(data->env); free(data->pwd); free(data->user); }
int suppr(t_mysh *ptr, int *index) { if (my_strncmp(ptr->param[1], ptr->env[*index], my_strlen(ptr->param[1])) == 0) { copy_tab_minus_one(&ptr->env_tmp, ptr->env, *index); free_tab(&ptr->env); copy_tab_plus_one(&ptr->env, ptr->env_tmp, 1); free_tab(&ptr->env_tmp); return (1); } return (0); }
void part_2() { char *str; char **id_tab; char **seq_tab; str = my_read(); id_tab = create_id_tab(str); seq_tab = create_ARN_tab(str); free(str); aff_fasta(id_tab, seq_tab); free_tab(id_tab); free_tab(seq_tab); }
void exec_cmd(char *str) { char **cmd; str = ft_strdup(parse_vars(str)); if (!(cmd = split_cmd(str)) || !cmd[0]) { free_tab(cmd); return ; } if (!exec_file(cmd) && !exec_builtin(cmd)) exec_path(cmd); free(str); free_tab(cmd); }
void put_in_stdin(char *com, int fd, t_com *elem) { char **tab; char *tmp; int file; tab = get_command(com); tmp = my_strdup(elem->com); free(elem->com); elem->com = my_strdup(elem->prev->prev->com); free(elem->prev->prev->com); elem->prev->prev->com = my_strdup(tmp); free(tmp); file = open(tab[0], O_RDONLY, 0644); if (file == -1) my_putstr("Error open file\n"); if (file == -1) elem->prev = NULL; if (file == -1) return; while ((tmp = get_next_line(file))) { my_write(fd, tmp, my_strlen(tmp)); my_write(fd, "\n", 1); free(tmp); } free_tab(tab); }
void put_in_stdout(char *com, int fd[2], int type) { char *buf; char **tab; int file; tab = get_command(com); my_close(fd[1]); if (type == 3) file = open(tab[0], O_WRONLY | O_APPEND | O_CREAT, 0644); else file = open(tab[0], O_WRONLY | O_CREAT | O_TRUNC, 0644); if (file == -1) { my_putstr("Error open file\n"); return; } while ((buf = get_next_line(fd[0]))) { my_write(file, buf, my_strlen(buf)); my_write(file, "\n", 1); free(buf); } free_tab(tab); my_close(file); }
int check_builtins(t_cmd *cmds, char **env) { int cur_case; char **builtins; int found; int (*fptr[6])(t_cmd *, char ***); found = -1; cur_case = -1; if ((builtins = my_split(get_builtins(), ';')) == NULL) return (-1); fill_func_ptr(fptr); if (cmds->name != NULL) { while (builtins[++cur_case]) if (my_strncmp(builtins[cur_case], cmds->name, get_longest_string(builtins[cur_case], cmds->name)) == 0) { fptr[cur_case](cmds, &env); found = 1; } free_tab(builtins); } return (found); }
int transfer_wt_to_cells(t_maze *maze, char **wt) { int i; int j; i = 0; while (wt[i]) i++; maze->h = i; if ((maze->cells = malloc(sizeof(char *) * i)) == NULL) return (1); i = -1; while (++i < maze->h) if ((maze->cells[i] = strdup(wt[i])) == NULL) return (1); i = -1; while (++i < maze->h) { j = -1; while (++j < maze->w) maze->cells[i][j] = (maze->cells[i][j] == '*') ? (0) : (1); } free_tab(wt, maze->h); if (maze->w > 1000 || maze->h > 1000) return (my_putstr_err(TOO_MUCH)); return (0); }
void client_raytrace(char *str, int *status, SOCKET sock) { unsigned int *grille; char **tab; t_prog prog; tab = client_raytrace_beg(status, &prog, sock, str); if ((prog.opt = client_raytrace_init(&prog)) == NULL) return; grille = raytrace_threading_client(&prog, my_getnbr(tab[0]), my_getnbr(tab[1])); my_printf(1, "Envois des calculs\n"); if (grille == NULL || send(sock, grille, sizeof(unsigned int) * (my_getnbr(tab[1]) - my_getnbr(tab[0])) * prog.win_size.y, 0) < 0) *status = -1; else { my_printf(1, "Caluls envoyés, en attente d'une information serveur\n"); *status = 0; free(grille); } free(prog.opt); free(str); free_tab(tab); }
int load_obj_file_open(t_prog *prog, char *str) { char **file; t_vtx_list *vtx_list; load_obj_file_open_init(prog); if ((file = str_to_wordtab(str, "\n")) == NULL) return (-1); prog->light_list = NULL; if (((prog->light_list = add_empty_light(prog->light_list)) == NULL) || ((prog->light_list = add_empty_light2(prog->light_list)) == NULL) || ((prog->light_list = add_empty_light3(prog->light_list)) == NULL) || ((prog->light_list = add_empty_light4(prog->light_list)) == NULL) || ((prog->background = create_text_uni(prog->win_size.x, prog->win_size.x, 0xff000000)) == NULL)) return (-1); prog->anti_alias = 1; prog->mat_list = NULL; if (((prog->mat_list = add_empty_mat(prog->mat_list)) == NULL) || ((vtx_list = get_vertex(file)) == NULL) || ((prog->obj_list = parse_obj_formes(file, vtx_list)) == NULL)) return (-1); free_tab(file); return (0); }
void fin_ht (value v) { hash_table_t *ht = (hash_table_t *) Field(v,1); free_tab(ht); //printf("hashtable gc'd\n"); //fflush(stdout); }
int fill_env(t_utils *utils, char **env) { int i; char **__tab; static t_base *last; i = -1; if (env == NULL) return (-1); while (env && env[++i]) { if ((__tab = totab(env[i], "=")) == NULL) return (-1); last = add_to_list((t_base **)&(utils->env), 0); ((t_env *)last)->token = 0; ((t_env *)last)->key = NULL; ((t_env *)last)->value = b_strdup(""); if (__tab[0]) ((t_env *)last)->key = b_strdup(__tab[0]); if (__tab[1]) { free(((t_env *)last)->value); ((t_env *)last)->value = b_strdup(__tab[1]); } free_tab(__tab); } return (0); }
int search_paths(t_env *e) { t_search env; env.paths = ft_strsplit(ft_getenv("PATH", e), ':'); env.y = 0; while (env.y > -1 && env.paths[env.y] != 0) { env.dirp = opendir(env.paths[env.y]); while ((env.entry = readdir(env.dirp)) != NULL) { if (strcmp(env.entry->d_name, e->args[0]) == 0) { env.str = ft_strjoin(env.paths[env.y], "/"); run_exec(ft_strjoin(env.str, env.entry->d_name), e); free(env.str); env.str = NULL; env.y = -2; } } closedir(env.dirp); env.y++; } free_tab(env.paths); free(env.paths); env.paths = NULL; return (env.y); }
void read_write_client(t_client *c) { g_server = strdup(c->server); g_name = strdup("user"); while (42) { bzero(c->buf, 4096); g_servport = c->servport; printf("\n"); printf("\e[1;33m%s@%s:%d> \e[0m", g_name, g_server, g_servport); fflush(stdout); signal(SIGINT, handler_ctrl_c); c->rc = read(0, c->buf, 4096); if (c->buf[0] != '\n' && c->rc != 0 && c->buf[0] != EOF) { parser(c->buf, c); exec_cmd(c); if (c->username != NULL) g_name = strdup(c->username); c->rc = read(c->sd, c->buf, 4096); write(1, c->buf, c->rc); free_tab(c); } } }
void set_lights_tok_tab(t_light_tok **t, char **tab, int *index, int len) { int i; char **tmp; i = 0; tmp = NULL; if (t && tab) { while (*t) { if (i < len) { tmp = ft_strsplit(tab[index[i]], SEP_2); if (tmp && tmp[0] && tmp[1]) set_light_tok_var(*t, tmp[1]); else check_errors(NUL, "light bad format", tab[index[i]]); i++; free_tab(&tmp); } t++; } } }
static int data_line(t_objs *obj, char *str) { static t_set set[SIZE] = {{"origin", recup_origin}, {"rayon", recup_rayon}, {"color", recup_color}, {"rotate", recup_rotate}, {"damier", recup_damier}, {"limit", recup_limit}, {"reflexion", recup_reflexion}, {"commissaire", recup_commissaire}}; static char *form[8] = {"plan", "sphere", "cylindre", "cone", "hyperboloide", "light", "eye", NULL}; char **line; int i; if ((line = my_str_to_wordtab(str, " \t")) == NULL) return (-1); i = -1; while (++i < SIZE) if (my_strcmp(set[i].key, line[0]) == 0) if (set[i].ptr(obj, line) == -1) return (-1); i = -1; while (form[++i]) if (my_strcmp(form[i], line[0]) == 0) obj->id = i; free_tab(line); return (0); }
void my_free(t_utils *utils) { free_struct((t_base *)utils->env); free_struct((t_base *)utils->history); free_struct((t_base *)utils->alias); free_tab(utils->path); }