Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/diff3/diff3.c
| Show First 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | |||||
| #include <sys/capsicum.h> | #include <sys/capsicum.h> | ||||
| #include <sys/procdesc.h> | #include <sys/procdesc.h> | ||||
| #include <sys/wait.h> | #include <sys/wait.h> | ||||
| #include <assert.h> | #include <assert.h> | ||||
| #include <capsicum_helpers.h> | #include <capsicum_helpers.h> | ||||
| #include <ctype.h> | #include <ctype.h> | ||||
| #include <err.h> | #include <err.h> | ||||
| #include <fcntl.h> | |||||
| #include <getopt.h> | #include <getopt.h> | ||||
| #include <inttypes.h> | #include <inttypes.h> | ||||
| #include <limits.h> | #include <limits.h> | ||||
| #include <spawn.h> | |||||
| #include <stdio.h> | #include <stdio.h> | ||||
| #include <stdlib.h> | #include <stdlib.h> | ||||
| #include <string.h> | #include <string.h> | ||||
| #include <unistd.h> | #include <unistd.h> | ||||
| extern char **environ; | |||||
| /* | /* | ||||
| * "from" is first in range of changed lines; "to" is last+1 | * "from" is first in range of changed lines; "to" is last+1 | ||||
| * from=to=line after point of insertion for added lines. | * from=to=line after point of insertion for added lines. | ||||
| */ | */ | ||||
| struct range { | struct range { | ||||
| int from; | int from; | ||||
| int to; | int to; | ||||
| }; | }; | ||||
| ▲ Show 20 Lines • Show All 204 Lines • ▼ Show 20 Lines | if (i > 0) { | ||||
| (*dd)[i].old.from = (*dd)[i].old.to = (*dd)[i - 1].old.to; | (*dd)[i].old.from = (*dd)[i].old.to = (*dd)[i - 1].old.to; | ||||
| (*dd)[i].new.from = (*dd)[i].new.to = (*dd)[i - 1].new.to; | (*dd)[i].new.from = (*dd)[i].new.to = (*dd)[i - 1].new.to; | ||||
| } | } | ||||
| fclose(f); | fclose(f); | ||||
| return (i); | return (i); | ||||
| } | } | ||||
| static int | static int | ||||
| diffexec(const char *diffprog, char **diffargv, int fd[]) | diffexec(char **diffargv, int fd[]) | ||||
| { | { | ||||
| int pd; | posix_spawnattr_t sa; | ||||
| posix_spawn_file_actions_t fa; | |||||
| pid_t pid; | |||||
| int pd, error; | |||||
| switch (pdfork(&pd, PD_CLOEXEC)) { | if ((error = posix_spawnattr_init(&sa)) != 0) | ||||
| case 0: | errc(2, error, "posix_spawnattr_init"); | ||||
| close(fd[0]); | if ((error = posix_spawn_file_actions_init(&fa)) != 0) | ||||
| if (dup2(fd[1], STDOUT_FILENO) == -1) | errc(2, error, "posix_spawn_file_actions_init"); | ||||
| err(2, "child could not duplicate descriptor"); | |||||
| posix_spawnattr_setprocdescp_np(&sa, &pd, 0); | |||||
| posix_spawn_file_actions_addclose(&fa, fd[0]); | |||||
| posix_spawn_file_actions_adddup2(&fa, fd[1], STDOUT_FILENO); | |||||
| posix_spawn_file_actions_addclose(&fa, fd[1]); | |||||
| error = posix_spawn(&pid, diffargv[0], &fa, &sa, diffargv, environ); | |||||
| if (error != 0) | |||||
| errc(2, error, "could not spawn diff"); | |||||
| posix_spawn_file_actions_destroy(&fa); | |||||
| posix_spawnattr_destroy(&sa); | |||||
| close(fd[1]); | close(fd[1]); | ||||
| execvp(diffprog, diffargv); | |||||
| err(2, "could not execute diff: %s", diffprog); | |||||
| break; | |||||
| case -1: | |||||
| err(2, "could not fork"); | |||||
| break; | |||||
| } | |||||
| close(fd[1]); | |||||
| return (pd); | return (pd); | ||||
| } | } | ||||
| static char * | static char * | ||||
| getchange(FILE *b) | getchange(FILE *b) | ||||
| { | { | ||||
| char *line; | char *line; | ||||
| ▲ Show 20 Lines • Show All 669 Lines • ▼ Show 20 Lines | while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) { | ||||
| case 'x': | case 'x': | ||||
| eflag = EFLAG_OVERLAP; | eflag = EFLAG_OVERLAP; | ||||
| break; | break; | ||||
| case 'X': | case 'X': | ||||
| oflag = 1; | oflag = 1; | ||||
| eflag = EFLAG_OVERLAP; | eflag = EFLAG_OVERLAP; | ||||
| break; | break; | ||||
| case DIFFPROG_OPT: | case DIFFPROG_OPT: | ||||
| diffprog = optarg; | diffargv[0] = optarg; | ||||
| break; | break; | ||||
| case STRIPCR_OPT: | case STRIPCR_OPT: | ||||
| strip_cr = 1; | strip_cr = 1; | ||||
| diffargv[diffargc++] = __DECONST(char *, "--strip-trailing-cr"); | diffargv[diffargc++] = __DECONST(char *, "--strip-trailing-cr"); | ||||
| break; | break; | ||||
| case HELP_OPT: | case HELP_OPT: | ||||
| usage(); | usage(); | ||||
| exit(0); | exit(0); | ||||
| ▲ Show 20 Lines • Show All 58 Lines • ▼ Show 20 Lines | main(int argc, char **argv) | ||||
| if (caph_rights_limit(fileno(fp[2]), &rights_ro) < 0) | if (caph_rights_limit(fileno(fp[2]), &rights_ro) < 0) | ||||
| err(2, "unable to limit rights on: %s", file3); | err(2, "unable to limit rights on: %s", file3); | ||||
| if (pipe(fd13)) | if (pipe(fd13)) | ||||
| err(2, "pipe"); | err(2, "pipe"); | ||||
| if (pipe(fd23)) | if (pipe(fd23)) | ||||
| err(2, "pipe"); | err(2, "pipe"); | ||||
kib: Spurious blank line | |||||
| diffargv[diffargc] = file1; | diffargv[diffargc] = file1; | ||||
| diffargv[diffargc + 1] = file3; | diffargv[diffargc + 1] = file3; | ||||
| diffargv[diffargc + 2] = NULL; | diffargv[diffargc + 2] = NULL; | ||||
| pd13 = diffexec(diffprog, diffargv, fd13); | pd13 = diffexec(diffargv, fd13); | ||||
| diffargv[diffargc] = file2; | diffargv[diffargc] = file2; | ||||
| pd23 = diffexec(diffprog, diffargv, fd23); | pd23 = diffexec(diffargv, fd23); | ||||
| caph_cache_catpages(); | caph_cache_catpages(); | ||||
| if (caph_enter() < 0) | if (caph_enter() < 0) | ||||
| err(2, "unable to enter capability mode"); | err(2, "unable to enter capability mode"); | ||||
| /* parse diffs */ | /* parse diffs */ | ||||
| increase(); | increase(); | ||||
| m = readin(fd13[0], &d13); | m = readin(fd13[0], &d13); | ||||
| Show All 9 Lines | |||||
Spurious blank line