void init_lines(t_area *ar, struct termios *oldline) { struct winsize w; int li; int co; if (ioctl(0, TIOCGWINSZ, &w) < 0) { restore_mode(oldline, ar); my_put_error(ERR_IOCTL); free_struct(ar); exit(EXIT_FAILURE); } li = w.ws_row; co = w.ws_col; if (li < 4 || co < ar->size) { restore_mode(oldline, ar); my_put_error(ERR_SIZE); free_struct(ar); exit(EXIT_FAILURE); } if (li != ar->li || co != ar->co) aff_list_resize(ar, li, co); }
void free(void *ptr) { t_block_descriptor *temp; if (ptr == NULL) return ; temp = (t_block_descriptor *) ptr - 1; if (check_chunk(temp) == -1) { my_put_error("in free(): warning: modified (chunk-) pointer\n"); return ; } if (is_already_free(buckets[temp->p - 1], temp) == -1) { my_put_error("in free(): warning: page is already free\n"); return ; } if (is_malloc(temp, 1) == -1) { my_put_error("in free(): warning: malloc() has never been called\n"); return ; } ret_from_list(temp); put_in_freelist(temp); return ; }
static void afferror(int state) { if (state < 0) { if (state == -1) my_put_error("Unmatched \".\n"); else if (state == -2) my_put_error("Unmatched '.\n"); else if (state == -3) my_put_error("Unmatched `.\n"); } }
static int my_read_header(header_t *header, int fd) { if (read(fd, header, sizeof(header_t)) != sizeof(header_t)) return (my_put_error("wrong header\n")); my_rev_oct(&(header->magic), sizeof(header->magic)); my_rev_oct(&(header->prog_size), sizeof(header->prog_size)); header->prog_name[PROG_NAME_LENGTH] = '\0'; header->comment[COMMENT_LENGTH] = '\0'; if (header->magic != COREWAR_EXEC_MAGIC) return (my_put_error("wrong magic number")); return (0); }
int main(int argc, char **argv) { struct termios oldline; t_area ar; if (argc > 1) { non_canonical_mode(&oldline); init_term(&ar, argv, argc); display_arguments(&ar); while ((ar.len = read(0, ar.buff, sizeof(ar.buff)))) { x_read(ar.len); init_lines(&ar, &oldline); check_ctrl(&ar); check_keys(&ar, ar.len); check_select(&ar); if (my_exit(&oldline, &ar) == 1 || void_exit(&ar, &oldline) == 1) return (EXIT_SUCCESS); } restore_mode(&oldline, &ar); free_struct(&ar); } else my_put_error(ERR_ARGV); return (EXIT_SUCCESS); }
t_tube *copy_pile(t_tube *pile) { t_tube *cur; t_tube *tmp; t_tube *first; t_tube *elem; cur = pile; first = NULL; if (!(first = copy_maillon(cur, &tmp))) return (NULL); cur = cur->next; while (cur) { if (!(elem = malloc(sizeof(t_tube)))) return (my_put_error(MALLOC_ERR), NULL); elem->next = NULL; elem->room = cur->room; elem->ants = cur->ants; tmp->next = elem; tmp = elem; cur = cur->next; } return (first); }
int save_hist(t_hist *hist, t_env *env) { t_hist *temp; int fd; int i; char *file; i = 0; temp = hist->prev; if ((file = hist_file(env)) == NULL) return (-1); if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) return (my_put_error("Open of .history failed\n")); free(file); while (temp != hist && i < 1000) { if (is_empty(temp->data) == 0) { write(fd, temp->data, my_strlen(temp->data)); write(fd, "\n", 1); } temp = temp->prev; i = i + 1; } close (fd); return (0); }
int init_champs(t_data *data) { int i; i = 0; while (i < 4) { if (!(data->champ[i] = malloc(sizeof(t_champion))) || !(data->champ[i]->pc = malloc(sizeof(t_pc))) || !(data->champ[i]->pc->next = malloc(sizeof(t_pc)))) return (my_put_error(MALLOC_ERROR, 1)); my_bzero(data->champ[i]->pc->next->reg, sizeof(int) * 16, 0); data->champ[i]->pc->next->champ = data->champ[i]; data->champ[i]->pc->next->reg[0] = -1; data->champ[i]->pc->next->cycle = 0; data->champ[i]->pc->next->carry = 0; data->champ[i]->pc->next->run = 0; data->champ[i]->pc->next->next = NULL; data->champ[i]->pc->next->prev = data->champ[i]->pc; data->champ[i]->pc->prev = NULL; data->champ[i]->valid = -1; data->champ[i]->alive = -1; data->champ[i]->order = -1; data->champ[i]->cur = data->champ[i]->pc->next; my_bzero(data->champ[i]->name, PROG_NAME_LENGTH + 1, 0), i += 1; } return (0); }
void init_term(t_area *ar, char **argv, int argc) { extern char **environ; ar->term = cpy_from_env(environ, "TERM="); if (ar->term == NULL) { my_put_error(ERR_TERM); exit(EXIT_FAILURE); } x_tgetent(tgetent(ar->bp, ar->term)); ar->area = ar->t; ar->clstr = xtgetstr("cl", &(ar->area)); ar->cmstr = xtgetstr("cm", &(ar->area)); ar->sostr = xtgetstr("so", &(ar->area)); ar->sestr = xtgetstr("se", &(ar->area)); ar->usstr = xtgetstr("us", &(ar->area)); ar->uestr = xtgetstr("ue", &(ar->area)); ar->li = tgetnum("li"); ar->co = tgetnum("co"); tputs(ar->clstr, 1, my_outc); argc = make_argc(ar, argc); ar->ac = argc - 1; ar->res = malloc(argc * sizeof(*(ar->res))); x_malloc(ar->res, argc); init_size(ar, argv); my_arg_cpy(ar, argv, argc); }
static int my_home(t_env *env) { t_env *home; if ((home = my_found_env(env, "HOME")) == NULL) return (my_put_error("no HOME set\n")); return (my_chdir(home->value)); }
static int my_old(t_env *env) { t_env *oldpwd; if ((oldpwd = my_found_env(env, "OLDPWD")) == NULL) return (my_put_error("no OLDPWD set\n")); return (my_chdir(oldpwd->value)); }
static int my_change_env(t_shell *shell, char *buf) { shell->env = my_add_env(shell->env, "OLDPWD", buf); if (getcwd(buf, 2048) != buf) return (my_put_error("fail getcwd\n")); shell->env = my_add_env(shell->env, "PWD", buf); shell->cd = my_strdup(buf); return (0); }
int chk_start_end(t_case *cas) { t_case *elem; elem = cas; if (elem->prev->pass || elem->next->pass) return (my_put_error(LAB_ERR), 1); return (0); }
int add_this_link(t_link **last, t_link *link) { if (!((*last)->next = malloc(sizeof(t_link)))) return (my_put_error(MALLOC_ERR), 1); (*last)->next->cas = link->cas; (*last)->next->next = NULL; (*last)->next->prev = *last; *last = (*last)->next; return (0); }
int solve_by_length(t_case *root, int width) { int ret; if ((ret = launch_solve_by_length(root)) == 1) return (1); else if (ret == 2) return (my_put_error(NO_PATH), 0); return (write_graph(root, width)); }
t_link *init_list_root() { t_link *root; if (!(root = malloc(sizeof(t_link)))) return (my_put_error(MALLOC_ERR), NULL); root->next = root; root->prev = root; return (root); }
void *xmalloc(size_t size) { void *ptr; ptr = malloc(size); if (ptr) return (ptr); my_put_error("Error: Malloc failed\n"); return (NULL); }
t_link *init_pile_root(t_case *first) { t_link *root; if (!(root = malloc(sizeof(t_link)))) return (my_put_error(MALLOC_ERR), NULL); root->cas = first; root->next = NULL; root->prev = NULL; first->path = 1; return (root); }
char *x_tgoto(const char *cap, int col, int row) { char *tmp; tmp = tgoto(cap, col, row); if (tmp == NULL) { my_put_error(ERR_TGOTO); exit(EXIT_FAILURE); } return (tmp); }
t_tube *copy_maillon(t_tube *cur, t_tube **tmp) { t_tube *first; if (!(first = malloc(sizeof(t_tube)))) return (my_put_error(MALLOC_ERR), NULL); first->next = NULL; first->room = cur->room; first->ants = cur->ants; *tmp = first; return (first); }
int height_len(char *line, int i) { int j; j = i; while (line[i] != '\0') { if (line[i] < '0' || line[i] > '9') my_put_error("MAP ERROR\n"); ++i; } return (i - j + 1); }
int width_len(char *line) { int i; i = 0; while (line[i] != 'x') { if (line[i] < '0' || line[i] > '9') my_put_error("MAP ERROR\n"); ++i; } return (i); }
void x_malloc_2d(char **tab, int size) { int i; i = 0; if (tab == NULL) { my_put_error(ERR_MALLOC); exit(EXIT_FAILURE); } while (i != size) tab[i++] = NULL; }
int my_write_normal(t_ins *ins, int fd) { int i; if (ins == NULL) return (my_put_error("internal error\n")); if (write(fd, &ins->octect_codage, 1) != 1) return (my_put_error("can't write in the file\n")); i = 0; while (i < MAX_ARGS_NUMBER) { if (ins->arg[i].type == T_REG && my_write_registre(ins, i, fd)) return (1); else if (ins->arg[i].type == T_IND && my_write_indirect(ins, i, fd)) return (1); else if (ins->arg[i].type == T_DIR && my_write_direct(ins, i, fd)) return (1); else if (ins->arg[i].type & T_LAB && my_write_label(ins, i, fd, 0)) return (1); i++; } return (0); }
char *x_malloc(char *str, int size) { int i; i = 0; str = malloc(size * sizeof(*str)); if (str == NULL) { my_put_error("malloc", 0); exit(EXIT_FAILURE); } while (i != size) str[i++] = 0; return (str); }
int my_cd(t_shell *shell, char **argv) { static char buf[2048 + 1]; int len; int ret; if (shell == NULL || argv == NULL) return (1); if ((len = my_len_tab(argv)) == 0) return (1); else if (len > 2) return (my_put_error("cd : too many arguments\n")); if (getcwd(buf, 2048) != buf) return (my_put_error("fail getcwd\n")); if (len == 1) ret = my_home(shell->env); else if (my_strcmp(argv[1], "-") == 0) ret = my_old(shell->env); else ret = my_chdir(argv[1]); if (ret != 0) return (1); return (my_change_env(shell, buf)); }
int parsing_error(t_exec *list) { t_exec *it; it = list->next; while (it != list) { if (it->tab[0] == NULL) { my_put_error("Bad command line\n"); return (1); } it = it->next; } return (0); }
int my_define_id_champ(t_champ *champ, int id) { int id_auto; champ = my_first_elem_champ(champ); id_auto = 1; if (id == -1) { while (my_id_is_not_dispo(champ, id_auto)) id_auto++; return (id_auto); } else if (my_id_is_not_dispo(champ, id)) return (my_put_error("You have put some same id in option\n") * -1); return (id); }
int main(int ac, char **av) { t_pars *parser; if (ac < 2) return (my_put_error(USAGE), 1); if (!(parser = recup_graph(av[1]))) return (1); if (!solve_by_length(parser->cas, parser->width)) { free_graph(parser); return (0); } free(parser); return (1); }
int my_unalias(t_cmd *cmd) { t_alias *tmp; tmp = cmd->alias->next; while (tmp != cmd->alias) { if (my_strcmp(tmp->alias, cmd->cmdtab[1]) == 0) { tmp->prev->next = tmp->next; tmp->next->prev = tmp->prev; free(tmp); return (0); } tmp = tmp->next; } return (my_put_error("Alias not found\n")); }