1 /*- 2 * Copyright (c) 2017 Baptiste Daroussin <bapt (at) FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 #include <sys/wait.h> 29 30 #include <err.h> 31 #include <paths.h> 32 #include <signal.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <unistd.h> 36 37 #include "pr.h" 38 #include "diff.h" 39 #include "xmalloc.h" 40 41 #define _PATH_PR "/usr/bin/pr" 42 43 struct pr * 44 start_pr(char *file1, char *file2) 45 { 46 int pfd[2]; 47 pid_t pid; 48 char *header; 49 struct pr *pr; 50 51 pr = xcalloc(1, sizeof(*pr)); 52 53 xasprintf(&header, "%s %s %s", diffargs, file1, file2); 54 signal(SIGPIPE, SIG_IGN); 55 fflush(stdout); 56 rewind(stdout); 57 if (pipe(pfd) == -1) 58 err(2, "pipe"); 59 switch ((pid = fork())) { 60 case -1: 61 status |= 2; 62 free(header); 63 err(2, "No more processes"); 64 case 0: 65 /* child */ 66 if (pfd[0] != STDIN_FILENO) { 67 dup2(pfd[0], STDIN_FILENO); 68 close(pfd[0]); 69 } 70 close(pfd[1]); 71 execl(_PATH_PR, _PATH_PR, "-h", header, (char *)0); 72 _exit(127); 73 default: 74 75 /* parent */ 76 if (pfd[1] != STDOUT_FILENO) { 77 pr->ostdout = dup(STDOUT_FILENO); 78 dup2(pfd[1], STDOUT_FILENO); 79 close(pfd[1]); 80 } 81 close(pfd[0]); 82 rewind(stdout); 83 free(header); 84 pr->pid = pid; 85 } 86 return (pr); 87 } 88 89 /* close the pipe to pr and restore stdout */ 90 void 91 stop_pr(struct pr *pr) 92 { 93 int wstatus; 94 95 if (pr == NULL) 96 return; 97 98 fflush(stdout); 99 if (pr->ostdout != STDOUT_FILENO) { 100 close(STDOUT_FILENO); 101 dup2(pr->ostdout, STDOUT_FILENO); 102 close(pr->ostdout); 103 } 104 if (waitpid(pr->pid, &wstatus, 0) == -1) 105 err(2, "waitpid"); 106 free(pr); 107 if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != 0) 108 errx(2, "pr exited abnormally"); 109 else if (WIFSIGNALED(wstatus)) 110 errx(2, "pr killed by signal %d", 111 WTERMSIG(wstatus)); 112 } 113