int exec_pipe(t_tab *tab, t_btree **tree, int status, t_pid **pid) { int n; n = 0; (void)status; fill_pid(pid, 1); if (pipe(tab->pipefd[tab->pipe % 2]) == -1) return (-1); if (!((*pid)->nbr = fork())) return (fork_pipe(tab, tree, pid, n)); if ((close(tab->pipefd[tab->pipe % 2][1])) == -1) return (-1); fd_is_on(tab, CLOSE); if (tab) { tab->sid = (tab->sid == 0) ? (*pid)->nbr : tab->sid; tab->pipe++; } relink_tree(tree); return (0); }
int library_import(struct library *li, const char *scan, const char *path) { int fd, status; char *cratename, *pathname; pid_t pid; FILE *fp; struct crate *crate; fprintf(stderr, "Scanning '%s'...\n", path); pathname = strdupa(path); cratename = basename(pathname); /* POSIX version, see basename(3) */ assert(cratename != NULL); crate = use_crate(li, cratename); if (crate == NULL) return -1; pid = fork_pipe(&fd, scan, "scan", path, NULL); if (pid == -1) return -1; fp = fdopen(fd, "r"); if (fp == NULL) { perror("fdopen"); abort(); /* recovery not implemented */ } for (;;) { struct record *d, *x; if (get_record(fp, &d) == -1) return -1; if (d == NULL) break; /* Add to the crate of all records */ x = crate_add(&li->all, d); if (x == NULL) return -1; /* If there is an existing entry, use it instead */ if (x != d) { record_clear(d); free(d); d = x; } /* Insert into the user's crate */ if (crate_add(crate, d) == NULL) return -1; } if (fclose(fp) == -1) { perror("close"); abort(); /* assumption fclose() can't on read-only descriptor */ } if (waitpid(pid, &status, 0) == -1) { perror("waitpid"); return -1; } if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS) { fputs("Library scan exited reporting failure.\n", stderr); return -1; } return 0; }