static void s_load_map(char *file_name) { int ret_gnl; int fd; char *line; if ((fd = open(file_name, O_RDONLY)) == -1) return ((void)ERR("%s", ft_strerror())); line = NULL; while ((ret_gnl = ft_get_next_line(fd, &line)) > 0) ft_lstd_push_front(&sgt_fdf()->l_line, line); if (ret_gnl == -1) ERR("%s", ft_strerror()); ft_lstd_rmap(&sgt_fdf()->l_line, &s_split); }
void ft_perror(const char *s) { char *err_str; if (s && *s != '\0') { write(2, s, ft_strlen(s)); write(2, ": ", 2); } err_str = ft_strerror(errno); write(2, err_str, ft_strlen(err_str)); write(2, "\n", 1); }
static void s_check_file_without_id(char *arg) { t_file *file; file = file_read(arg); if (file == NULL) { ERROR("%s", ft_strerror()); exit(EXIT_FAILURE); } file->id = s_find_free_id(); ft_lstd_push_front(&sgt_corewar()->files, file); }
int ft_cd(char **args, char ***env) { int i; i = 1; while (args[i] && args[i][0] == '-' && args[i][1] && args[i][1] != '-') { if (ft_strchr(args[i], 'P') == NULL && ft_strchr(args[i], 'L') == NULL) { ft_strerror(ft_strjoin("42sh: cd: invalid option: ", args[i])); ft_putendl("42sh: usage: cd [-L|-P] [dir]"); return (1); } i++; } if (!args[i] || !args[i + 1]) return (cd_write_in_pwd(args + i - 1, env)); if (!args[i + 2]) return (cd_search_in_pwd(args + i - 1, env)); ft_strerror(ft_strjoin("42sh: cd: too much arguments: ", args[1])); return (1); }
int ft_check_size(t_ctx *ctx) { struct winsize w; int fd; if (ERR == (fd = ft_get_fd())) return (ERR); if (-1 == ioctl(fd, TIOCGWINSZ, &w)) return (ft_strerror()); if (ctx->cols * 2 + 6 > w.ws_col) return (ft_error("The window is too small to display this grid")); return (OK); }
void ft_exit(char *errmsg, int errnum) { if (errmsg != NULL) { ft_putstr_fd(errmsg, 2); if (errnum > -1) ft_putstr_fd(": ", 2); else ft_putstr_fd("\n", 2); } if (errnum > -1) { ft_putstr_fd(ft_strerror(errnum), 2); ft_putstr_fd("\n", 2); } exit(errnum); }
static int cd_search_in_pwd(char **args, char ***env) { char *pwd; char *tmp; pwd = ft_getenv("PWD", *env); if ((tmp = ft_strstr(pwd, args[1])) == NULL) { ft_strerror(ft_strjoin("42sh: cd: string not in pwd: ", args[1])); return (1); } pwd = cd_change_in_pwd(pwd, tmp, args); if (chdir(pwd) != -1) { ft_change_pwds(pwd, env); ft_putendl(pwd); free(pwd); return (0); } return (cd_error(pwd, pwd)); }