void parse_file(char *name) { int fd; void *buf; struct stat s; if ((fd = open(name, O_RDONLY)) == -1) { my_putstr_error("Error open \n"); return ; } if (fstat(fd, &s) == -1) { my_putstr_error("Error fstats \n"); return ; } if ((buf = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { my_putstr_error("Error mmap \n"); return ; } choose_type(buf, name, s.st_size); if (munmap(buf, s.st_size) == -1) my_putstr_error("Error munmap \n"); if (close(fd) == -1) my_putstr_error("Error close \n"); }
char *error_cd(char *str) { my_putstr_error("cd: "); my_putstr_error(str); my_putstr_error(": No files or folders of this type\n"); return (NULL); }
int get_option(int argc, char **argv) { int i; int flag_c; i = 1; flag_c = 0; while (i < argc) { if (argv[i][0] == '-') { if (argv[i][1] == 'c') flag_c = 1; else if (argv[i][1] == 'g') return (gen_map(argc, argv)); else { my_putstr_error("./bsq: invalid option -- '"); my_putchar_error(argv[i][1]); my_putstr_error("\'\n"); return (-1); } } i = i + 1; } return (flag_c); }
/* ** fork et execeute une commande */ void exec_cmd(char **cmd, char **my_env) { int status; char *binpath; pid_t my_pid; signal(SIGINT, kill_fils); binpath = find_binpath(cmd, my_env); if (binpath == NULL) binpath = cmd[0]; if ((my_pid = fork()) < 0) exit(-1); if (my_pid != 0) { waitpid(my_pid, &status, 0); } if (my_pid == 0) { if (execve(binpath , cmd, my_env) == -1) { my_putstr_error(cmd[0]); my_putstr_error(": Command not found.\n"); } exit(1); } }
int my_setenv(char ***env, char *variable, char *value) { char **tmp_var; int i; i = 0; tmp_var = *env; if (variable == NULL || value == NULL) return (my_putstr_error("usage: setenv VARIABLE VALUE\n")); if (variable == NULL || my_strlen(variable) == 0 || my_strchr(variable, '=') != NULL) return (my_putstr_error("error : variable NULL or '=' in name\n")); if ((variable = add_egale(variable)) == NULL) return (-1); while (tmp_var && tmp_var[i] && my_strncmp(variable, tmp_var[i], my_strlen(variable)) != 0) i = i + 1; if (tmp_var == NULL || tmp_var[i] == NULL) { if ((push_env(env, variable, value)) == -1) return (-1); } else if ((mod_env(&tmp_var[i], value)) == -1) return (-1); return (0); }
int main(int ac, char **av) { t_ptr *ptr; t_col *color; t_sc *scene; if (ac > 1) my_parse(av[1]); (void)av; if (ac != 1) my_putstr_error(ARG_ERROR); if ((ptr = my_init()) == NULL) { my_putstr_error(INIT_ERROR); return (EXIT_ERROR); } if ((color = my_init_color(0, 0, 0)) == NULL) { my_putstr_error(INIT_ERROR); return (EXIT_ERROR); } if ((scene = my_init_scene()) == NULL) return (EXIT_ERROR); my_fill_image(ptr->pic, color, scene); mlx_expose_hook(ptr->win->ptr, &my_expose, ptr); mlx_key_hook(ptr->win->ptr, &my_key, ptr); mlx_loop(ptr->mlx); return (SUCCESS); }
void set_color(int bgcolor, int fgcolor) { char *afstr; char *abstr; char t[4096]; char *area; char *term; char bp[1024]; if ((term = my_get_env("TERM")) == NULL) { my_putstr_error("can't determine terminal\n"); exit(-1); } if (tgetent(bp, term) != 1) { my_putstr_error("problem with tgetent\n"); exit(-1); } area = t; afstr = xtgetstr("AF", &area); abstr = xtgetstr("AB", &area); if (tputs(tparm(afstr, fgcolor), 1, my_outc) == ERR) exit(-1); if (bgcolor != 0) if (tputs(tparm(abstr, bgcolor), 1, my_outc) == ERR) exit(-1); }
int disclaimer(glob_t *globuf, t_tab *stock, char **send) { int ret; char buff[1]; my_putchar('\n'); if (printf(DISCLAIMER, (int)globuf->gl_pathc) < 0) my_putstr_error("Printf Fail\n"); while ((ret = read(0, buff, 1)) > 0) if (buff[0] == 'y') { aff_glob(globuf); prompt(stock); if (send != NULL) my_putstr(*send); return (0); } else if (buff[0] == 'n') { my_putchar('\n'); prompt(stock); if (send != NULL) my_putstr(*send); return (0); } return (my_glob_return(globuf, NULL, ret)); }
static int fork_pipe(t_tab *tab, t_btree **tree, t_pid **pid, int n) { if (setpgid(0, tab->sid) == -1) return (exit_flag(tab, tree, -1)); if (signal(SIGINT, SIG_DFL) == SIG_ERR || signal(SIGQUIT, SIG_DFL) == SIG_ERR || signal(SIGTSTP, SIG_DFL) == SIG_ERR || signal(SIGTTIN, SIG_DFL) == SIG_ERR || signal(SIGTTOU, SIG_DFL) == SIG_ERR) my_putstr_error("Syscall signal fail\n"); if (tab->pipe != 0) if ((dup2(tab->pipefd[(tab->pipe - 1) % 2][0], 0))) return (exit_flag(tab, tree, -1)); if ((dup2(tab->pipefd[tab->pipe % 2][1], 1) == -1) || (close(tab->pipefd[tab->pipe % 2][0]))) return (exit_flag(tab, tree, -1)); if ((n = built_or_exec(tab, tree, 0, pid)) == -1) return (exit_flag(tab, tree, -1)); else if (n == 0) return (exit_flag(tab, tree, 0)); fd_is_on(tab, DUP); if (execve((*tree)->right->stock[0], (*tree)->right->stock, tab->env)) return (exit_flag(tab, tree, -1)); return (0); }
int read_arg(t_tetris *tetris, int ac, char **av) { int i; int n; int debug; i = 0; n = 0; debug = 0; while (++i < ac) { if (my_strcmp(av[i], "-d") == 0 || my_strcmp(av[i], "--debug") == 0) debug = 1; else if (my_strcmp(av[i], "--help") == 0 && ac == 2) display_help(av[0]); else if ((n = check_good_arg(tetris, av, &i)) == -1) return (-1); if (n == 1) { my_putstr_error("Wrong arguments\n\n"); display_help_error(); } } if (display_debug(tetris, debug) == -1) return (-1); return (0); }
int start_tetris(int argc, char **argv, t_tetris *tetris) { int check; int i; if ((check = my_check_arg(argc, argv, tetris)) == 1) { my_free_tetris(tetris); return (1); } else if (check == - 1) return (- 1); if (my_map_tab(tetris) == - 1) return (- 1); if (tetris->debug == true) my_print_debug(tetris); if ((i = rm_elem(&tetris->piece)) == - 1) { my_putstr_error(TET2_ERR_MSG); return (- 1); } tetris->nb_piece = tetris->nb_piece - i; my_put_pos(tetris); return (0); }
int my_check_arg(int argc, char **argv, t_tetris *tetris) { int k; int i; bool end; int check; t_opt opt[OPT_LEN]; k = 0; init_tab_opt(opt, argc); while (++k < argc) { end = false; i = - 1; while (end == false && ++i < OPT_LEN) if ((check = my_check_opt(argv, &k, opt[i], tetris)) == 2) end = true; else if (check != 0) return (check); if (i == OPT_LEN) { my_putstr_error(ARG_ERR_MSG); my_putnbr_error(k, true); return (- 1); } } return (0); }
void my_malloc(void **ptr, int size) { if (!(*ptr = malloc(size))) { my_putstr_error("Malloc error\n"); exit(1); } }
void my_exit(char *str) { my_putstr_error("exit\n"); if (str != NULL) exit(my_atoi(str)); else exit(0); }
int main(int ac, char **av) { int i; int res; i = 1; if (ac > 1) { res = create_cor(av[i]); if (res == -1) return (my_putstr_error("Error champion not OK\n")); else if (res == -2) return (my_putstr_error("Error when create outfile \n")); else if (res == -3) return (my_putstr_error("Error: .name or .comment not \ found or not good\n")); else if (res == -5) return (my_putstr_error("File file_name not accessible")); }
char *buffer(char *str_size) { int ret; int size; char *buff; size = my_getnbr(str_size); buff = malloc(size + 1); if (buff == NULL) { my_putstr_error(ERROR_MSG); } ret = read (0, buff, size); if (ret != size) { my_putstr_error(ERROR_MSG); } buff[size] = '\0'; return (buff); }
int reset_getent() { char *term; if ((term = my_getenv("TERM")) == NULL || tgetent(0, term) != 1) { my_putstr_error("Error on tgetent, only support xterm\n"); return (0); } return (1); }
int main(int ac, char **av) { char *buffer; struct stat st; int size; if (ac != 2) { my_putstr_error("You must have 2 arguments.\n"); return (1); } if (stat(av[1], &st) == -1) { my_putstr_error("Stat has failed\n"); exit(1); } size = st.st_size; buffer = malloc(size); map_open(av[1], buffer, size); return (0); }
void init_sdl(t_arena *arena) { arena->mode = M_SDL; if (SDL_Init(SDL_INIT_VIDEO) == -1) my_putstr_error("Can't init SDL\n", 1); atexit(SDL_Quit); if ((arena->graph.sdl_surface = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_HWSURFACE )) == NULL) my_putstr_error("Can't set video mode\n", 1); if (TTF_Init() == -1) my_putstr_error("Can't init TTF\n", 1); arena->graph.font_title = NULL; if ((arena->graph.font_title = TTF_OpenFont(FONT_PATH, 65)) == NULL) my_putstr_error("Can't open font\n", 1); arena->graph.font = NULL; if ((arena->graph.font = TTF_OpenFont(FONT_PATH, 20)) == NULL) my_putstr_error("Can't open font\n", 1); arena->graph.title_col.r = 200; arena->graph.title_col.g = 200; arena->graph.title_col.b = 200; arena->graph.miam = NULL; if ((arena->graph.miam = IMG_Load("sprites/coquillette_jambon.png")) == NULL) { my_printf("Can't open the coquillette jambon image.. i'm hungry"); my_putstr_error(" i will not fight until you feed me.....\n", 1); } }
int clean_term() { char *cmd; move_abs(0, 0); if ((cmd = tgetstr("cd", NULL)) == NULL) { my_putstr_error("Error, while cleaning the screen\n"); return (0); } my_putstr_fd(cmd); return (1); }
int map_open(char *filepath, char *buffer, int size) { int fd; fd = open(filepath, O_RDONLY); if (fd == -1) { my_putstr_error("Can't open the file\n"); exit(1); } map_read(fd, buffer, size); close(fd); return (fd); }
int get_nb_cols(char *file) { int fd; char tmp; int i; i = 0; if ((fd = open(file, O_RDONLY)) == -1) return (put_bad_file(file)); while (read(fd, &tmp, 1) > 0 && tmp != '\n'); while (read(fd, &tmp, 1) > 0 && tmp != '\n') i = i + 1; if ((close(fd)) == -1) return (my_putstr_error("error: could not close file\n")); return (i); }
int get_size_term(int *x, int *y) { struct winsize ws; int fd; if ((fd = open("/dev/tty", O_RDWR)) < 0 || ioctl(fd, TIOCGWINSZ, &ws) < 0) { my_putstr_error("Error while loading winsize\n"); return (0); } close(fd); *x = ws.ws_col; *y = ws.ws_row; if (*x < 0 || *y < 0) return (0); return (1); }
int my_erno(int code, char *str) { if (code == 1) { my_putstr_error("script: illegal option --"); my_putstr_error(str); my_putstr_error("\nusage: script [-akq] [-t time]"); my_putstr_error(" [file [command ...]]\n"); } if (code == 2) { my_putstr_error(str); my_putstr_error(" : Cant open file.\n"); } if (code == 3) my_putstr_error(str); exit(1); }
int get_nb_lines(int *fd, char *file) { int i; char nb_lines[15]; i = 0; if ((*fd = open(file, O_RDONLY)) == -1) return (put_bad_file(file)); while (read(*fd, &nb_lines[i], 1) > 0 && nb_lines[i] != '\n' && i < 15) i = i + 1; if (i == 14) return (my_putstr_error("error: bad number on first line\n")); nb_lines[i] = 0; if (check_nb_lines(nb_lines) == -1 || i == 0) { close(*fd); return (put_bad_nb_lines(nb_lines, file)); } return (my_atoi(nb_lines)); }
int go_first_line(char *file, int *fd) { char tmp; int i; i = 0; tmp = 0; if ((*fd = open(file, O_RDONLY)) == -1) return (put_bad_file(file)); while (read(*fd, &tmp, 1) > 0 && tmp != '\n'); tmp = 0; while (read(*fd, &tmp, 1) > 0 && tmp != '\n') i = i + 1; if ((close(*fd)) == -1) return (my_putstr_error("error: could not close file\n")); if ((*fd = open(file, O_RDONLY)) == -1) return (put_bad_file(file)); tmp = 0; while (read(*fd, &tmp, 1) > 0 && tmp != '\n'); return (i); }
int main(int argc, char **argv, char **env) { t_tetris *tetris; int check; if (env == NULL) { my_putstr_error(ENV_ERR_MSG); return (- 1); } srand(time(NULL)); if ((tetris = my_def_tetris(env)) == NULL) return (- 1); if ((check = start_tetris(argc, argv, tetris)) == 1) return (0); else if (check == - 1) return (- 1); if (the_game(tetris) == - 1) return (- 1); if (my_save_high(tetris) == - 1) return (- 1); my_free_tetris(tetris); return (0); }
void my_putstr(char *str) { if (write(1, str, strlen(str)) < 0) my_putstr_error("malloc: write error"); }
int check_size(char **av) { if (my_str_isnum(av[3]) != 1) my_putstr_error(SYNTAXE_ERROR_MSG); return (0); }
void verif_arg(int ac, char **av) { if (ac < 2) my_putstr_error("not enought parameter\n", 1); (void)av; }