Changeset View
Changeset View
Standalone View
Standalone View
head/usr.bin/cmp/cmp.c
Show First 20 Lines • Show All 110 Lines • ▼ Show 20 Lines | main(int argc, char *argv[]) | ||||
argc -= optind; | argc -= optind; | ||||
if (lflag && sflag) | if (lflag && sflag) | ||||
errx(ERR_EXIT, "specifying -s with -l or -x is not permitted"); | errx(ERR_EXIT, "specifying -s with -l or -x is not permitted"); | ||||
if (argc < 2 || argc > 4) | if (argc < 2 || argc > 4) | ||||
usage(); | usage(); | ||||
if (caph_limit_stdio() == -1) | |||||
err(ERR_EXIT, "failed to limit stdio"); | |||||
/* Backward compatibility -- handle "-" meaning stdin. */ | /* Backward compatibility -- handle "-" meaning stdin. */ | ||||
special = 0; | special = 0; | ||||
if (strcmp(file1 = argv[0], "-") == 0) { | if (strcmp(file1 = argv[0], "-") == 0) { | ||||
special = 1; | special = 1; | ||||
fd1 = 0; | fd1 = STDIN_FILENO; | ||||
file1 = "stdin"; | file1 = "stdin"; | ||||
} | } else if ((fd1 = open(file1, oflag, 0)) < 0 && errno != EMLINK) { | ||||
else if ((fd1 = open(file1, oflag, 0)) < 0 && errno != EMLINK) { | |||||
if (!sflag) | if (!sflag) | ||||
err(ERR_EXIT, "%s", file1); | err(ERR_EXIT, "%s", file1); | ||||
else | else | ||||
exit(ERR_EXIT); | exit(ERR_EXIT); | ||||
} | } | ||||
if (strcmp(file2 = argv[1], "-") == 0) { | if (strcmp(file2 = argv[1], "-") == 0) { | ||||
if (special) | if (special) | ||||
errx(ERR_EXIT, | errx(ERR_EXIT, | ||||
"standard input may only be specified once"); | "standard input may only be specified once"); | ||||
special = 1; | special = 1; | ||||
fd2 = 0; | fd2 = STDIN_FILENO; | ||||
file2 = "stdin"; | file2 = "stdin"; | ||||
} | } else if ((fd2 = open(file2, oflag, 0)) < 0 && errno != EMLINK) { | ||||
else if ((fd2 = open(file2, oflag, 0)) < 0 && errno != EMLINK) { | |||||
if (!sflag) | if (!sflag) | ||||
err(ERR_EXIT, "%s", file2); | err(ERR_EXIT, "%s", file2); | ||||
else | else | ||||
exit(ERR_EXIT); | exit(ERR_EXIT); | ||||
} | } | ||||
skip1 = argc > 2 ? strtol(argv[2], NULL, 0) : 0; | skip1 = argc > 2 ? strtol(argv[2], NULL, 0) : 0; | ||||
skip2 = argc == 4 ? strtol(argv[3], NULL, 0) : 0; | skip2 = argc == 4 ? strtol(argv[3], NULL, 0) : 0; | ||||
Show All 20 Lines | if (caph_rights_limit(fd2, &rights) < 0) | ||||
err(ERR_EXIT, "unable to limit rights for %s", file2); | err(ERR_EXIT, "unable to limit rights for %s", file2); | ||||
/* Required for fdopen(3). */ | /* Required for fdopen(3). */ | ||||
fcntls = CAP_FCNTL_GETFL; | fcntls = CAP_FCNTL_GETFL; | ||||
if (caph_fcntls_limit(fd1, fcntls) < 0) | if (caph_fcntls_limit(fd1, fcntls) < 0) | ||||
err(ERR_EXIT, "unable to limit fcntls for %s", file1); | err(ERR_EXIT, "unable to limit fcntls for %s", file1); | ||||
if (caph_fcntls_limit(fd2, fcntls) < 0) | if (caph_fcntls_limit(fd2, fcntls) < 0) | ||||
err(ERR_EXIT, "unable to limit fcntls for %s", file2); | err(ERR_EXIT, "unable to limit fcntls for %s", file2); | ||||
if (!special) { | |||||
cap_rights_init(&rights); | |||||
if (caph_rights_limit(STDIN_FILENO, &rights) < 0) { | |||||
err(ERR_EXIT, "unable to limit stdio"); | |||||
} | |||||
} | |||||
if (caph_limit_stdout() == -1 || caph_limit_stderr() == -1) | |||||
err(ERR_EXIT, "unable to limit stdio"); | |||||
caph_cache_catpages(); | caph_cache_catpages(); | ||||
if (caph_enter() < 0) | if (caph_enter() < 0) | ||||
err(ERR_EXIT, "unable to enter capability mode"); | err(ERR_EXIT, "unable to enter capability mode"); | ||||
if (!special) { | if (!special) { | ||||
if (fstat(fd1, &sb1)) { | if (fstat(fd1, &sb1)) { | ||||
▲ Show 20 Lines • Show All 42 Lines • Show Last 20 Lines |