static int exec_redout_get_fd(t_tree *root) { char *path; int open_fd; int opt; if ((path = cd_get_path2(root->cmd[1])) == NULL) return (sh_reset_std_fd() - 1); if (access(path, F_OK) == 0 && access(path, W_OK) < 0) { sh_reset_std_fd(); ft_dprintf(STDERR_FILENO, "shell: %s: permission denied\n", root->cmd[1]); free(path); return (-1); } opt = O_WRONLY | O_CREAT | (root->types == D_GREAT ? O_APPEND : O_TRUNC); if ((open_fd = open(path, opt, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) { sh_reset_std_fd(); ft_dprintf(STDERR_FILENO, "shell: %s: cannot write on this\n", root->cmd[1]); } free(path); return (open_fd); }
int io_redir_aggregate(struct s_sh_io_redirect const *io_redir, t_lst **const fd_backups_loc) { int io_number; int target_fd; if (!io_redir || !fd_backups_loc || !io_redir->ionum || !io_redir->target) ft_dprintf(STDERR_FILENO, "42sh: invalid args in %s.\n", __FUNCTION__); else if ((io_number = str_to_fd(io_redir->ionum)) == -1) ft_dprintf(STDERR_FILENO, "42sh: %s: bad file descriptor.\n", io_redir->ionum); else if ((target_fd = ft_strcmp(io_redir->target, "-") ? 0 : -1) != -1 && ((target_fd = str_to_fd(io_redir->target)) == -1 || !fd_is_active(target_fd) || filedescriptor_is_a_backup(target_fd, *fd_backups_loc))) ft_dprintf(STDERR_FILENO, "42sh: %s: bad file descriptor.\n", io_redir->target); else if (backup_filedescriptor(io_number, fd_backups_loc)) ft_dprintf(STDERR_FILENO, "42sh: failed to backup filedescriptor.\n"); else if (0 < target_fd && dup2(target_fd, io_number) == -1) ft_dprintf(STDERR_FILENO, "42sh: error in aggregate redirection.\n"); else { if (target_fd == -1) close(io_number); return (0); } return (1); }
int ft_puterr(const char *s1, const char *s2, const char *issu) { if (s1 && s2 && issu) ft_dprintf(FTL_OUT_ERR, "%s: [%s] %s\n", s1, s2, issu); else if (s1 && issu) ft_dprintf(FTL_OUT_ERR, "%s: %s\n", s1, issu); else if (issu) ft_dprintf(FTL_OUT_ERR, "%s\n", issu); return (FTL_ERROR); }
void msh_print_redirs(t_list *lst) { int i; t_red *red; i = 0; while (lst != NULL) { red = (t_red*)lst->content; ft_dprintf(2, "Redir#%Nd: type:%N#x ", ++i, red->type); ft_dprintf(2, "err:%.6b ", red->error); ft_dprintf(2, "lfd:%-N3d ", red->lhsfd); if (red->type == MTK_HERE) { ft_dprintf(2, "delim:%!N $zr ", red->file); ft_dprintf(2, "result:%!N $zr ", red->hdoc); } else if (red->file != NULL) ft_dprintf(2, "rfl:%!N $zr ", red->file); else ft_dprintf(2, "rfd:%-N3d ", red->rhsfd); ft_dprintf(2, "\":und:%! $.*zr", red->len[0], red->ptr[0]); ft_dprintf(2, "%! $.*zr:eou:\"", red->len[1], red->ptr[1]); ft_putchar_fd('\n', 2); lst = lst->next; } }
in_addr_t get_addr(char *ip) { struct hostent *host; ft_dprintf(1, "Solve proxy\n"); host = gethostbyname(ip); if (host) ip = inet_ntoa(*((struct in_addr *)host->h_addr)); else return (-1); ft_dprintf(1, "%s\n", ip); return (inet_addr(ip)); }
static int get_colors(char *str) { struct stat *lol; if ((lol = (struct stat*)malloc(sizeof(struct stat))) == NULL) return (0); if (lstat(str, lol) == -1) return (0); if (lol->st_mode & S_IFDIR) ft_dprintf(get_fd(0), "\x1b[31;01m"); else if (lol->st_mode & S_IXUSR) ft_dprintf(get_fd(0), "\x1b[34;01m"); free(lol); return (0); }
int completion_search_path(t_comp *comp) { t_env *env; t_env_list *path; char **paths; env = &get_shell_data()->env; if (!(path = ft_getenv(env->env_list, "PATH"))) return (0); if (!(paths = ft_strsplit(path->value, ':'))) return (MALLOC_FAIL); if (ft_tab_len(paths) > MAX_PATH_COMPONENTS) { ft_dprintf(STDERR_FILENO, "42sh: Max path components reached"); reset_buffer(&get_shell_data()->input); ft_free_string_tab(&paths); return (1); } if ((comp->matches = open_path_dirs(comp, paths)) == NULL && comp->nb_matches == -1) { ft_free_string_tab(&paths); return (MALLOC_FAIL); } ft_free_string_tab(&paths); return (0); }
int builtin_export(char **av, t_env *env) { char const *export_options = "n"; char options[2]; char wrong_opt; int i; if (!av[1]) return (display_global_env(env->env_list)); i = 0; while (av[++i] && av[i][0] == '-' && ft_strcmp("--", av[i])) { if (!av[i][1]) { ft_putendl_fd("42sh: export: `-': not a valid identifier", 2); return (1); } if ((wrong_opt = check_arg_opt(av[i] + 1, export_options, options))) return (export_usage(wrong_opt)); } if (ft_strchr(options, 'n')) return (export_option_n(env, av + i)); else if (is_valid_variable_name(av[1])) return (set_to_global(env, av[1], av[2])); return (ft_dprintf(STDERR_FILENO, "%s: not a valid variable name\n", av[1])); }
int launch_utility(char **argv, t_bool is_child) { t_bsh *const bsh = get_shell_data(); t_builtin builtin; char *external; int ret; if (bsh && argv != NULL) { if (!argv[0]) return (0); else if (ft_strchr(argv[0], '/')) return (launch_external(argv[0], argv, &bsh->env, is_child)); else if ((builtin = find_builtin(argv[0]))) return (builtin(argv, &bsh->env)); else if ((external = find_external(argv[0], bsh))) { ret = launch_external(external, argv, &bsh->env, is_child); free(external); return (ret); } else ft_dprintf(2, "42sh: %s: command not found\n", argv[0]); } return (-1); }
void ft_enter(t_list **list, t_env *envir) { restore_term(envir); while (*list && (*list)->rev != 1) *list = (*list)->next; if (*list) { ft_dprintf(EXIT, "%s", (*list)->arg); *list = (*list)->next; } while (*list) { if ((*list)->rev == 1) ft_dprintf(EXIT, " %s", (*list)->arg); *list = (*list)->next; } }
static int sh_check_rights(char *path_to_bin, char *com) { struct stat buf; if (lstat(path_to_bin, &buf) == -1) return (126); else if (access(path_to_bin, X_OK) < 0) { ft_dprintf(STDERR_FILENO, "shell: permission denied: %s\n", com); return (126); } else if (S_ISREG(buf.st_mode) == 0 && S_ISLNK(buf.st_mode) == 0) { ft_dprintf(STDERR_FILENO, "shell: exec format error: %s\n", com); return (126); } return (0); }
void msh_print_tokens(t_list *tkn) { int i; t_tkn *c; const char tktypes[][5] = MTKNAMES; i = 0; while (tkn != NULL) { c = (t_tkn*)tkn->content; ft_dprintf(2, "Token%H-2d: size%H0_2u %H4s \":BLU:", ++i, c->len, tktypes[c->type]); ft_dprintf(2, "% $.*zr", (int)c->len, c->ptr); ft_dprintf(2, ":eof::inv:%! $.*zr:eof:\"\n", 10, c->ptr + (int)c->len); tkn = tkn->next; } return ; }
char *cd_var_env(char *name) { int i; char *tmp; if ((i = sh_is_new_var(name)) < 0) { ft_dprintf(STDERR_FILENO, "cd: %s not set\n", name); return (NULL); } tmp = ft_strdup(&(ft_strchr(g_env[i], '=')[1])); if (access(tmp, F_OK) < 0) { ft_dprintf(STDERR_FILENO, "cd: %s invalid path: %s\n", name, tmp); return (NULL); } return (tmp); }
t_sock connect_server_ipv6(char *ip, int port, char *proto_name) { t_sock client; t_protoent *proto; t_sockaddr_in6 sin; ft_dprintf(1, "{yellow}Try connecting to '%s:%d' on ipv6\n{eoc}", ip, port); client = 0; proto = getprotobyname(proto_name); if (proto) if ((client = socket(PF_INET6, SOCK_STREAM, proto->p_proto)) < 0) return (connect_server_ipv4(ip, port, proto_name)); sin.sin6_family = AF_INET6; inet_pton(AF_INET6, ip, &sin.sin6_addr); sin.sin6_port = htons(port); if ((connect(client, (const t_sockaddr*)&sin, sizeof(sin))) < 0) return (connect_server_ipv4(ip, port, proto_name)); ft_dprintf(1, "{yellow}Success\n{eoc}"); return (client); }
void print_option_error(const char *prog_name, const char option, enum e_opt_return error) { const char *error_strings[] = { "%s: illegal option -- %c\n", "%s: option requires an argument -- %c\n", "%s: option encountederd an error -- %c\n" }; ft_dprintf(STDERR_FILENO, error_strings[error], prog_name, option); }
t_sock connect_server_ipv4(char *ip, int port, char *proto_name) { t_sock client; t_protoent *proto; t_sockaddr_in sin; ft_dprintf(1, "{yellow}Try connecting to '%s:%d' on ipv4\n{eoc}", ip, port); client = 0; proto = getprotobyname(proto_name); if (proto) if ((client = socket(PF_INET, SOCK_STREAM, proto->p_proto)) == -1) exit(ft_dprintf(2, "can't create socket\n")); sin.sin_family = AF_INET; sin.sin_port = htons(port); sin.sin_addr.s_addr = get_addr(ip); if ((connect(client, (const t_sockaddr*)&sin, sizeof(sin))) == -1) exit(ft_dprintf(1, "\033[22;31mError : init client on %d\033[22;37m\n", port)); ft_dprintf(1, "{yellow}Success\n{eoc}"); return (client); }
int nm_acv_handle(t_env const e[1], t_bininfo bi[1]) { t_acvinfo ai[1]; ai->hdr = bi->addr + 8; while (ai->hdr < bi->addrend) { if (PRINT_DEBUG) ft_dprintf(2, "60BYTES: %$M.60r \n", ai->hdr); if (nm_acv_read_header(bi, ai)) return (0); if (PRINT_DEBUG) ft_dprintf(2, "(%.*r) (%u)\n" , ai->filename.len, ai->filename.str , ai->filesize); if (sub_binary(e, bi, ai)) return (1); ai->hdr = ai->hdr + ai->filesize + 60; } return (0); }
static int ft_put_elem(t_arg_list *tmp, int fd) { if (tmp->op == 1 || tmp->op == 3) ft_tputs("us", 1, 0, fd); if (tmp->op == 2 || tmp->op == 3) ft_tputs("mr", 1, 0, fd); ft_dprintf(fd, "%s", tmp->name); if (tmp->op == 2 || tmp->op == 3) ft_tputs("me", 1, 0, fd); if (tmp->op == 1 || tmp->op == 3) ft_tputs("ue", 1, 0, fd); return (0); }
int nm_env_make(int ac, char const *const *av, t_env e[1]) { unsigned int opt[1]; t_ftvector paths[1]; *opt = 0; ftv_init_instance(paths, sizeof(char *)); if (args((t_arg_parser[1]){ft_arg_create(ac, av)}, opt, paths)) return (1); if (paths->size == 0) ft_dprintf(2, "%s\n%s\n", NM_NOFILE, NM_USAGE); *e = (t_env){*opt & ARG_FIELD, *paths, get_sym_insert_fn(*opt)}; return (0); }
static void print_node_intern(int const fd, t_trie const *trie, t_print const *print, int depth) { char const tab[] = " "; ft_dprintf(fd, "%.*s", depth, tab); if (trie->children == NULL) { ft_putstr_fd("content:", fd); print[0](fd, trie->content.data); } else { print[1](fd, trie->content.ref); ft_dprintf(fd, "(%d\n", depth); f_lstiter_va(trie->children, print_intern, fd, print, depth + 1); ft_dprintf(fd, "%.*s )%d", depth, tab, depth); } ft_putstr_fd("\n", fd); }
void msh_print_lvar(t_list *lst) { int i; char *lvar; i = 0; while (lst != NULL) { lvar = lst->content; ft_dprintf(2, "Local Var#%i: \"%! $zr\"\n", ++i, lvar); lst = lst->next; } return ; }
void ft_echo(t_process *p) { int i; int n; i = 1; n = 0; if (p->av[i] != NULL) { if (!ft_strcmp(p->av[i], "-n")) n = i++; if (p->av[i + 1] != NULL) while (p->av[i] && p->av[i + 1]) { ft_dprintf(p->FD_OUT, "%s ", p->av[i]); i++; } if (p->av[i]) ft_dprintf(p->FD_OUT, "%s", p->av[i]); if (n == 0) write(p->FD_OUT, "\n", 1); } }
int main(int argc, char **argv) { int i; t_main main; ft_logf_set_enabled(LOG_DEBUG, true); ft_logf_set_enabled(LOG_VERBOSE, true); main = (t_main){ NULL, NULL, {}, VECTOR(t_scene), -1, -1, false, }; if ((main.thread_pool = ft_thread_pool_create(THREAD_COUNT)) == NULL) return (ft_dprintf(2, "Failed to create threads"), 1); if ((main.mlx = mlx_init()) == NULL || !ft_mlx_open(&main.win, main.mlx, VEC2U(WIN_WIDTH, WIN_HEIGHT), SUBC(WIN_TITLE))) return (1); i = 0; while (++i < argc) { ft_logf(LOG_VERBOSE, "Loading scene file: %s", argv[i]); if (!load_scenes(argv[i], &main.scenes)) return (1); } if (main.scenes.length == 0) { ft_logf(LOG_ERROR, "No scene loaded"); return (1); } ft_logf(LOG_SUCCESS, "%d scene(s) loaded", main.scenes.length); mlx_loop_hook(main.mlx, &loop_hook, &main); mlx_key_hook(main.win.win_id, &key_hook, &main); mlx_expose_hook(main.win.win_id, &expose_hook, &main); render(&main, 0, 0); mlx_loop(main.mlx); ASSERT(false); main_destroy(&main); return (0); }
bool load_scenes(char const *file, t_vector *dst) { t_file_in *in; t_xml_parser xml_parser; bool ret; if (file[0] == '-' && file[1] == '\0') in = ft_in_fdopen(0); else if ((in = ft_in_open(ft_sub(file, 0, -1))) == NULL) { ft_dprintf(2, "%s[Error]%s %s: Invalid file%n", C_RED, C_RESET, file); return (false); } xml_parser = XML_PARSER(V(in)); if (!(ret = parse_scenes(&xml_parser, dst))) { ft_dprintf(2, "%s[Error]%s %s:%u: %ts%n", C_RED, C_RESET, file, xml_parser.line, ft_xml_value(&xml_parser)); // TODO: destroy scenes data on error } ft_xml_clear(&xml_parser); ft_in_close(in); return (ret); }
void history_save(t_server *server) { t_list *tmp; t_entry *entry; server->history.file = open("history.csv", O_WRONLY | O_APPEND); tmp = server->history.last_save->next; while (tmp) { entry = (t_entry*)tmp->content; ft_dprintf(server->history.file, "%s, %s\n", entry->speech, \ entry->command); tmp = tmp->next; } close(server->history.file); }
int print_line(t_line *line) { t_line *buf; buf = line; while (buf) { if (buf->select == 1) ft_notputs("us", 1); ft_dprintf(STDOUT_FILENO, "%c", buf->c); if (buf->select == 1) ft_notputs("ue", 1); buf = buf->next; } return (0); }
static int save_option(unsigned int opt[1], char c) { unsigned int i; i = 0; while (i < ARG_NUM_OPTIONS) { if (ARG_OPTIONS[i] == c) { *opt |= 1 << i; return (0); } i++; } ft_dprintf(2, "%s%c\n%s\n", NM_INVARG, c, NM_USAGE); return (1); }
static char *find_external(char *const name, t_bsh *const bsh) { char *external; external = get_path_table(&bsh->hashtable, name); if (external != NULL) { if ((external = ft_strdup(external)) == NULL) ft_putstr_fd("42sh: allocation error\n", 2); } else external = sh_path_search(&bsh->env, name); if (external && bsh && add_hash_table(&bsh->hashtable, name, external)) ft_dprintf(STDERR_FILENO, "42sh: failed to add %s to hashtable\n", external); return (external); }
int ft_select(t_param **debut) { char buf[10]; int ret; ft_tputs("vi", 1); if (print_args(*debut) == -1) return (-1); if (print_args(*debut) == -2) { ft_tputs("cl", 1); ft_dprintf(STDERR_FILENO, "\x1b[31;01mSCREEN SIZE TOO SMALL\x1b[0;m"); } ret = read(0, buf, 10); ft_bzero(&buf[ret], 10 - ret); return (read_key(buf, debut)); }
void init_env(t_env *e, int ac, char **av) { if (ac < 2) exit(ft_dprintf(2, "usage : %s <map>\n", av[0])); e->ac = ac; e->av = av; e->img = ft_new_img(e->mlx, WIN_WIDTH, WIN_HEIGHT); e->dir.x = 0; e->dir.y = 0; e->dir.z = 1; e->pos.x = 0; e->pos.y = 0; e->pos.z = -5; e->screen = ft_memalloc(sizeof(t_obj)); init_tab_obj(e, av[1]); raytracer(e); }