diff options
Diffstat (limited to 'usr.bin/paste/paste.c')
| -rw-r--r-- | usr.bin/paste/paste.c | 61 |
1 files changed, 39 insertions, 22 deletions
diff --git a/usr.bin/paste/paste.c b/usr.bin/paste/paste.c index 8114a85a869a..cc029e20ea9c 100644 --- a/usr.bin/paste/paste.c +++ b/usr.bin/paste/paste.c @@ -33,9 +33,13 @@ */ #include <sys/types.h> +#include <sys/queue.h> +#include <sys/capsicum.h> +#include <capsicum_helpers.h> #include <err.h> #include <errno.h> +#include <fcntl.h> #include <limits.h> #include <locale.h> #include <stdio.h> @@ -44,11 +48,14 @@ #include <unistd.h> #include <wchar.h> +#include <libcasper.h> +#include <casper/cap_fileargs.h> + static wchar_t *delim; static int delimcnt; -static int parallel(char **); -static int sequential(char **); +static int parallel(char **, fileargs_t *); +static int sequential(char **, fileargs_t *); static int tr(wchar_t *); static void usage(void) __dead2; @@ -61,6 +68,8 @@ main(int argc, char *argv[]) wchar_t *warg; const char *arg; size_t len; + fileargs_t *fa; + cap_rights_t rights; setlocale(LC_CTYPE, ""); @@ -98,51 +107,58 @@ main(int argc, char *argv[]) delim = tab; } - if (seq) - rval = sequential(argv); - else - rval = parallel(argv); + fa = fileargs_init(argc, argv, O_RDONLY, 0, + cap_rights_init(&rights, CAP_READ, CAP_FSTAT, CAP_FCNTL), FA_OPEN); + if (fa == NULL) + err(1, "unable to open system.fileargs service"); + + caph_cache_catpages(); + if (caph_enter_casper() < 0) + err(1, "unable to enter capability mode"); + + rval = seq ? sequential(argv, fa) : parallel(argv, fa); + + fileargs_free(fa); exit(rval); } typedef struct _list { - struct _list *next; + STAILQ_ENTRY(_list) entries; FILE *fp; int cnt; char *name; } LIST; +static STAILQ_HEAD(head, _list) lh; + static int -parallel(char **argv) +parallel(char **argv, fileargs_t *fa) { LIST *lp; int cnt; wint_t ich; wchar_t ch; char *p; - LIST *head, *tmp; int opencnt, output; - for (cnt = 0, head = tmp = NULL; (p = *argv); ++argv, ++cnt) { + STAILQ_INIT(&lh); + + for (cnt = 0; (p = *argv); ++argv, ++cnt) { if ((lp = malloc(sizeof(LIST))) == NULL) err(1, NULL); if (p[0] == '-' && !p[1]) lp->fp = stdin; - else if (!(lp->fp = fopen(p, "r"))) + else if (!(lp->fp = fileargs_fopen(fa, p, "r"))) err(1, "%s", p); - lp->next = NULL; lp->cnt = cnt; lp->name = p; - if (!head) - head = tmp = lp; - else { - tmp->next = lp; - tmp = lp; - } + + STAILQ_INSERT_TAIL(&lh, lp, entries); } for (opencnt = cnt; opencnt;) { - for (output = 0, lp = head; lp; lp = lp->next) { + output = 0; + STAILQ_FOREACH(lp, &lh, entries) { if (!lp->fp) { if (output && lp->cnt && (ch = delim[(lp->cnt - 1) % delimcnt])) @@ -183,7 +199,7 @@ parallel(char **argv) } static int -sequential(char **argv) +sequential(char **argv, fileargs_t *fa) { FILE *fp; int cnt, failed, needdelim; @@ -194,7 +210,7 @@ sequential(char **argv) for (; (p = *argv); ++argv) { if (p[0] == '-' && !p[1]) fp = stdin; - else if (!(fp = fopen(p, "r"))) { + else if (!(fp = fileargs_fopen(fa, p, "r"))) { warn("%s", p); failed = 1; continue; @@ -243,7 +259,8 @@ tr(wchar_t *arg) default: *arg = ch; break; - } else + } + else *arg = ch; if (!cnt) |
