void verif_term(void) { char bp[1024]; char *term; if ((term = getenv("TERM")) == NULL) aff_error("can't determine terminal"); if (tgetent(bp, term) != 1) aff_error("problem with tgetent"); }
void start_terminal(t_caps *caps) { struct termios init; if (ioctl(0, TIOCGETA, &init) == -1) aff_error("ioctl failed"); if (ioctl(0, TIOCGETA, &(caps->save)) == -1) aff_error("ioctl failed"); init.c_lflag &= ~(ECHO | ICANON); init.c_cc[VMIN] = 0; init.c_cc[VTIME] = 0; if (ioctl(0, TIOCSETA, &init) == -1) aff_error("ioctl failed"); }
void end_terminal(t_caps *caps) { if (ioctl(0, TIOCSETA, &(caps->save)) == -1) aff_error("ioctl failed"); tputs(caps->clstr, 1, my_outc); exit(EXIT_SUCCESS); }
void *xmalloc(int size) { void *obj; if ((obj = malloc(size)) == NULL) aff_error("malloc failed"); return (obj); }
void init_caps(t_caps *caps) { char t[4096]; char *area; area = t; verif_term(); if ((caps->clstr = tgetstr("cl", &area)) == NULL) aff_error("tgetstr failed to load clstr"); if ((caps->cmstr = tgetstr("cm", &area)) == NULL) aff_error("tgetstr failed to load cmdstr"); caps->li = tgetnum("li"); caps->co = tgetnum("co"); if (caps->co < WIDTH + 1 || caps->li < HEIGHT + 1) aff_error("terminal too small"); tputs(caps->clstr, 1, my_outc); }
void print_time(void) { struct timeval now; if (gettimeofday(&now, NULL) == -1) aff_error("gettimeofday failed"); printf("-- %i:%i --\n", (int) now.tv_sec, (int) now.tv_usec); }
void cmd_put_in_list(t_cmd **cmd, char **data, int type) { t_cmd *snext; t_cmd *tmp; tmp = *cmd; if (!(snext = xmalloc(sizeof(*snext)))) aff_error("Couldn't malloc"); snext->tabl = data; snext->type = type; snext->next = *cmd; *cmd = snext; }
char *add_str_end(char *cmd, char *buff, int *len, int print) { char *new_ptr; int i; int j; j = 0; i = 0; if (buff) { if (print) my_putstr(buff); if (*len + strlen(buff) < BUFF_DEF) { while (buff[i] != '\0') { cmd[*len] = buff[i++]; *len += 1; } } else { if (!(new_ptr = malloc((strlen(cmd) + strlen(buff) + 1) * sizeof(*new_ptr)))) aff_error("Couldn't malloc\n"); while (cmd[i]) new_ptr[i] = cmd[i++]; new_ptr[i] = '\0'; j = 0; while (buff[j]) { new_ptr[i++] = buff[j++]; *len += 1; } free(cmd); cmd = new_ptr; } } return (cmd); }