summaryrefslogtreecommitdiffstats
path: root/usr.bin/dc/dc.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin/dc/dc.c')
-rw-r--r--usr.bin/dc/dc.c26
1 files changed, 21 insertions, 5 deletions
diff --git a/usr.bin/dc/dc.c b/usr.bin/dc/dc.c
index f6968a5..a2cf8c9 100644
--- a/usr.bin/dc/dc.c
+++ b/usr.bin/dc/dc.c
@@ -22,9 +22,11 @@ __FBSDID("$FreeBSD$");
#include <sys/stat.h>
+#include <capsicum_helpers.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
+#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
@@ -58,11 +60,11 @@ usage(void)
}
static void
-procfile(char *fname) {
+procfd(int fd, char *fname) {
struct stat st;
FILE *file;
- file = fopen(fname, "r");
+ file = fdopen(fd, "r");
if (file == NULL)
err(1, "cannot open file %s", fname);
if (fstat(fileno(file), &st) == -1)
@@ -80,7 +82,7 @@ procfile(char *fname) {
int
main(int argc, char *argv[])
{
- int ch;
+ int ch, fd;
bool extended_regs = false, preproc_done = false;
/* accept and ignore a single dash to be 4.4BSD dc(1) compatible */
@@ -97,7 +99,10 @@ main(int argc, char *argv[])
case 'f':
if (!preproc_done)
init_bmachine(extended_regs);
- procfile(optarg);
+ fd = open(optarg, O_RDONLY);
+ if (fd < 0)
+ err(1, "cannot open file %s", optarg);
+ procfd(fd, optarg);
preproc_done = true;
break;
case 'x':
@@ -126,12 +131,23 @@ main(int argc, char *argv[])
if (argc > 1)
usage();
if (argc == 1) {
- procfile(argv[0]);
+ fd = open(argv[0], O_RDONLY);
+ if (fd < 0)
+ err(1, "cannot open file %s", argv[0]);
+
+ if (caph_limit_stream(fd, CAPH_READ) < 0 ||
+ caph_limit_stdio() < 0 ||
+ (cap_enter() < 0 && errno != ENOSYS))
+ err(1, "capsicum");
+
+ procfd(fd, argv[0]);
preproc_done = true;
}
if (preproc_done)
return (0);
+ if (caph_limit_stdio() < 0 || (cap_enter() < 0 && errno != ENOSYS))
+ err(1, "capsicum");
src_setstream(&src, stdin);
reset_bmachine(&src);
eval();
OpenPOWER on IntegriCloud