Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/head/head.c
Show All 37 Lines | |||||
#ifndef lint | #ifndef lint | ||||
#if 0 | #if 0 | ||||
static char sccsid[] = "@(#)head.c 8.2 (Berkeley) 5/4/95"; | static char sccsid[] = "@(#)head.c 8.2 (Berkeley) 5/4/95"; | ||||
#endif | #endif | ||||
#endif /* not lint */ | #endif /* not lint */ | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <sys/types.h> | #include <sys/types.h> | ||||
#include <sys/capsicum.h> | |||||
emaste: > If either <sys/types.h> or <sys/param.h> is needed, include it before other include files. | |||||
#include <capsicum_helpers.h> | |||||
#include <ctype.h> | #include <ctype.h> | ||||
#include <err.h> | #include <err.h> | ||||
#include <errno.h> | |||||
#include <getopt.h> | #include <getopt.h> | ||||
#include <inttypes.h> | #include <inttypes.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> | ||||
#include <libcasper.h> | |||||
#include <casper/cap_fileargs.h> | |||||
/* | /* | ||||
* head - give the first few lines of a stream or of each of a set of files | * head - give the first few lines of a stream or of each of a set of files | ||||
* | * | ||||
* Bill Joy UCB August 24, 1977 | * Bill Joy UCB August 24, 1977 | ||||
*/ | */ | ||||
static void head(FILE *, int); | static void head(FILE *, int); | ||||
static void head_bytes(FILE *, off_t); | static void head_bytes(FILE *, off_t); | ||||
Show All 10 Lines | |||||
int | int | ||||
main(int argc, char *argv[]) | main(int argc, char *argv[]) | ||||
{ | { | ||||
int ch; | int ch; | ||||
FILE *fp; | FILE *fp; | ||||
int first, linecnt = -1, eval = 0; | int first, linecnt = -1, eval = 0; | ||||
off_t bytecnt = -1; | off_t bytecnt = -1; | ||||
char *ep; | char *ep; | ||||
fileargs_t *fa; | |||||
cap_rights_t rights; | |||||
Not Done Inline ActionsThese additions maintain non-style(9) of variable declarations. It's a pedantic comment, but as this change will likely be used as a reference for future fileargs additions we may want to address it; if we do, we should probably style(9) head.c first in a separate commit. emaste: These additions maintain non-style(9) of variable declarations.
It's a pedantic comment, but… | |||||
Not Done Inline ActionsI created another review for making it more style(9) https://reviews.freebsd.org/D14498 oshogbo: I created another review for making it more style(9) https://reviews.freebsd.org/D14498 | |||||
obsolete(argv); | obsolete(argv); | ||||
while ((ch = getopt_long(argc, argv, "+n:c:", long_opts, NULL)) != -1) | while ((ch = getopt_long(argc, argv, "+n:c:", long_opts, NULL)) != -1) | ||||
switch(ch) { | switch(ch) { | ||||
case 'c': | case 'c': | ||||
bytecnt = strtoimax(optarg, &ep, 10); | bytecnt = strtoimax(optarg, &ep, 10); | ||||
if (*ep || bytecnt <= 0) | if (*ep || bytecnt <= 0) | ||||
errx(1, "illegal byte count -- %s", optarg); | errx(1, "illegal byte count -- %s", optarg); | ||||
break; | break; | ||||
case 'n': | case 'n': | ||||
linecnt = strtol(optarg, &ep, 10); | linecnt = strtol(optarg, &ep, 10); | ||||
if (*ep || linecnt <= 0) | if (*ep || linecnt <= 0) | ||||
errx(1, "illegal line count -- %s", optarg); | errx(1, "illegal line count -- %s", optarg); | ||||
break; | break; | ||||
case '?': | case '?': | ||||
default: | default: | ||||
usage(); | usage(); | ||||
} | } | ||||
argc -= optind; | argc -= optind; | ||||
argv += optind; | argv += optind; | ||||
fa = fileargs_init(argc, argv, O_RDONLY, 0, | |||||
cap_rights_init(&rights, CAP_READ, CAP_FSTAT, CAP_FCNTL)); | |||||
if (fa == NULL) | |||||
errx(1, "unable to init casper"); | |||||
caph_cache_catpages(); | |||||
if (caph_limit_stdio() < 0 || caph_enter_casper() < 0) | |||||
err(1, "unable to enter capability mode"); | |||||
if (linecnt != -1 && bytecnt != -1) | if (linecnt != -1 && bytecnt != -1) | ||||
errx(1, "can't combine line and byte counts"); | errx(1, "can't combine line and byte counts"); | ||||
if (linecnt == -1 ) | if (linecnt == -1 ) | ||||
linecnt = 10; | linecnt = 10; | ||||
if (*argv) { | if (*argv) { | ||||
for (first = 1; *argv; ++argv) { | for (first = 1; *argv; ++argv) { | ||||
if ((fp = fopen(*argv, "r")) == NULL) { | if ((fp = fileargs_fopen(fa, *argv, "r")) == NULL) { | ||||
warn("%s", *argv); | warn("%s", *argv); | ||||
eval = 1; | eval = 1; | ||||
continue; | continue; | ||||
} | } | ||||
if (argc > 1) { | if (argc > 1) { | ||||
(void)printf("%s==> %s <==\n", | (void)printf("%s==> %s <==\n", | ||||
first ? "" : "\n", *argv); | first ? "" : "\n", *argv); | ||||
first = 0; | first = 0; | ||||
} | } | ||||
if (bytecnt == -1) | if (bytecnt == -1) | ||||
head(fp, linecnt); | head(fp, linecnt); | ||||
else | else | ||||
head_bytes(fp, bytecnt); | head_bytes(fp, bytecnt); | ||||
(void)fclose(fp); | (void)fclose(fp); | ||||
} | } | ||||
Done Inline ActionsShould we call fileargs_free? emaste: Should we call `fileargs_free`? | |||||
} else if (bytecnt == -1) | } else if (bytecnt == -1) | ||||
head(stdin, linecnt); | head(stdin, linecnt); | ||||
else | else | ||||
head_bytes(stdin, bytecnt); | head_bytes(stdin, bytecnt); | ||||
fileargs_free(fa); | |||||
exit(eval); | exit(eval); | ||||
} | } | ||||
static void | static void | ||||
head(FILE *fp, int cnt) | head(FILE *fp, int cnt) | ||||
{ | { | ||||
char *cp; | char *cp; | ||||
size_t error, readlen; | size_t error, readlen; | ||||
▲ Show 20 Lines • Show All 54 Lines • Show Last 20 Lines |
from style(9)
Still sys/cdefs.h first.