summaryrefslogtreecommitdiff
path: root/distrib/common
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>2008-12-02 03:20:39 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>2008-12-02 03:20:39 +0000
commit0dd28ad0baf2f725525aa741d0b78f8686a14200 (patch)
treeb668bb78305b50fd874a4c2a003935c773f4edf7 /distrib/common
parent4e9f2fdd95ee369f210c8d4d2e5e77f2c244c2ef (diff)
permit passing in the filesystem (a file) as an argument
Diffstat (limited to 'distrib/common')
-rw-r--r--distrib/common/elfrdsetroot.c37
-rw-r--r--distrib/common/rdsetroot.c37
2 files changed, 58 insertions, 16 deletions
diff --git a/distrib/common/elfrdsetroot.c b/distrib/common/elfrdsetroot.c
index 82c44105bf9..f42010dc437 100644
--- a/distrib/common/elfrdsetroot.c
+++ b/distrib/common/elfrdsetroot.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: elfrdsetroot.c,v 1.18 2008/12/02 01:54:55 weerd Exp $ */
+/* $OpenBSD: elfrdsetroot.c,v 1.19 2008/12/02 03:20:38 deraadt Exp $ */
/* $NetBSD: rdsetroot.c,v 1.2 1995/10/13 16:38:39 gwr Exp $ */
/*
@@ -79,7 +79,8 @@ int debug;
int
main(int argc, char *argv[])
{
- int ch, fd, n, xflag = 0, found = 0, phsize;
+ int ch, fd, n, xflag = 0, found = 0, phsize, fsd;
+ char *fs = NULL;
u_int32_t *ip;
Elf_Ehdr eh;
Elf_Phdr *ph;
@@ -99,9 +100,13 @@ main(int argc, char *argv[])
argc -= optind;
argv += optind;
- if (argc != 1)
+ if (argc == 1)
+ file = argv[0];
+ else if (argc == 2) {
+ file = argv[0];
+ fs = argv[1];
+ } else
usage();
- file = argv[0];
fd = open(file, xflag ? O_RDONLY : O_RDWR, 0644);
if (fd < 0) {
@@ -109,6 +114,22 @@ main(int argc, char *argv[])
exit(1);
}
+ if (fs) {
+ if (xflag)
+ fsd = open(fs, O_RDWR | O_CREAT | O_TRUNC, 0644);
+ else
+ fsd = open(fs, O_RDONLY, 0644);
+ } else {
+ if (xflag)
+ fsd = dup(STDOUT_FILENO);
+ else
+ fsd = dup(STDIN_FILENO);
+ }
+ if (fsd < 0) {
+ perror(fs);
+ exit(1);
+ }
+
n = read(fd, &eh, sizeof(eh));
if (n < sizeof(eh)) {
fprintf(stderr, "%s: reading header\n", file);
@@ -167,7 +188,7 @@ main(int argc, char *argv[])
if (debug)
fprintf(stderr, "copying root image...\n");
if (xflag) {
- n = write(STDOUT_FILENO, dataseg + rd_root_image_off,
+ n = write(fsd, dataseg + rd_root_image_off,
rd_root_size_val);
if (n != rd_root_size_val) {
perror("write");
@@ -176,7 +197,7 @@ main(int argc, char *argv[])
} else {
struct stat sstat;
- if (fstat(STDIN_FILENO, &sstat) == -1) {
+ if (fstat(fsd, &sstat) == -1) {
perror("fstat");
exit(1);
}
@@ -186,7 +207,7 @@ main(int argc, char *argv[])
(long)sstat.st_size, rd_root_size_val);
exit(1);
}
- n = read(STDIN_FILENO, dataseg + rd_root_image_off,
+ n = read(fsd, dataseg + rd_root_image_off,
rd_root_size_val);
if (n < 0) {
perror("read");
@@ -259,6 +280,6 @@ usage(void)
{
extern char *__progname;
- fprintf(stderr, "usage: %s [-dx] file_name\n", __progname);
+ fprintf(stderr, "usage: %s [-dx] bsd [fs]\n", __progname);
exit(1);
}
diff --git a/distrib/common/rdsetroot.c b/distrib/common/rdsetroot.c
index 2650d15eb59..8a62cbfe9de 100644
--- a/distrib/common/rdsetroot.c
+++ b/distrib/common/rdsetroot.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rdsetroot.c,v 1.17 2008/12/02 01:54:55 weerd Exp $ */
+/* $OpenBSD: rdsetroot.c,v 1.18 2008/12/02 03:20:38 deraadt Exp $ */
/* $NetBSD: rdsetroot.c,v 1.2 1995/10/13 16:38:39 gwr Exp $ */
/*
@@ -71,7 +71,8 @@ int debug;
int
main(int argc, char *argv[])
{
- int ch, fd, n, xflag = 0;
+ int ch, fd, n, xflag = 0, fsd;
+ char *fs = NULL;
int *ip;
while ((ch = getopt(argc, argv, "dx")) != -1) {
@@ -89,9 +90,13 @@ main(int argc, char *argv[])
argc -= optind;
argv += optind;
- if (argc != 1)
+ if (argc == 1)
+ file = argv[0];
+ else if (argc == 2) {
+ file = argv[0];
+ fs = argv[1];
+ } else
usage();
- file = argv[0];
fd = open(file, xflag ? O_RDONLY : O_RDWR, 0644);
if (fd < 0) {
@@ -99,6 +104,22 @@ main(int argc, char *argv[])
exit(1);
}
+ if (fs) {
+ if (xflag)
+ fsd = open(fs, O_RDWR | O_CREAT | O_TRUNC, 0644);
+ else
+ fsd = open(fs, O_RDONLY, 0644);
+ } else {
+ if (xflag)
+ fsd = dup(STDOUT_FILENO);
+ else
+ fsd = dup(STDIN_FILENO);
+ }
+ if (fsd < 0) {
+ perror(fs);
+ exit(1);
+ }
+
n = read(fd, &head, sizeof(head));
if (n < sizeof(head)) {
fprintf(stderr, "%s: reading header\n", file);
@@ -179,7 +200,7 @@ main(int argc, char *argv[])
fprintf(stderr, "copying root image...\n");
if (xflag) {
- n = write(STDOUT_FILENO, dataseg + rd_root_image_off,
+ n = write(fsd, dataseg + rd_root_image_off,
rd_root_size_val);
if (n != rd_root_size_val) {
perror("write");
@@ -188,7 +209,7 @@ main(int argc, char *argv[])
} else {
struct stat sstat;
- if (fstat(STDIN_FILENO, &sstat) == -1) {
+ if (fstat(fsd, &sstat) == -1) {
perror("fstat");
exit(1);
}
@@ -197,7 +218,7 @@ main(int argc, char *argv[])
fprintf(stderr, "ramdisk too small\n");
exit(1);
}
- n = read(STDIN_FILENO, dataseg + rd_root_image_off,
+ n = read(fsd, dataseg + rd_root_image_off,
rd_root_size_val);
if (n < 0) {
perror("read");
@@ -270,6 +291,6 @@ usage(void)
{
extern char *__progname;
- fprintf(stderr, "usage: %s [-dx] file_name\n", __progname);
+ fprintf(stderr, "usage: %s [-dx] bsd [fs]\n", __progname);
exit(1);
}