aboutsummaryrefslogtreecommitdiff
path: root/toys/pending
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2016-08-04 10:16:59 -0500
committerRob Landley <rob@landley.net>2016-08-04 10:16:59 -0500
commit027a73a903af306449710ce12bc09e0e3550c6c9 (patch)
treea415cb11fa6f2b34e63b8259fc52342aaa8fec75 /toys/pending
parent145b7024b5fbb74f16d5e403fb004ff8209bc4a0 (diff)
downloadtoybox-027a73a903af306449710ce12bc09e0e3550c6c9.tar.gz
Make xopen() skip stdin/stdout/stderr, add xopen_stdio() if you want stdout,
add xopenro() that takes one argument and understands "-" means stdin, and switch over lots of users.
Diffstat (limited to 'toys/pending')
-rw-r--r--toys/pending/arp.c2
-rw-r--r--toys/pending/crond.c5
-rw-r--r--toys/pending/crontab.c9
-rw-r--r--toys/pending/dd.c17
-rw-r--r--toys/pending/dhcp.c4
-rw-r--r--toys/pending/dumpleases.c2
-rw-r--r--toys/pending/getty.c2
-rw-r--r--toys/pending/klogd.c2
-rw-r--r--toys/pending/last.c2
-rw-r--r--toys/pending/mdev.c2
-rw-r--r--toys/pending/modprobe.c4
-rw-r--r--toys/pending/openvt.c2
-rw-r--r--toys/pending/sulogin.c2
-rw-r--r--toys/pending/tftp.c2
14 files changed, 24 insertions, 33 deletions
diff --git a/toys/pending/arp.c b/toys/pending/arp.c
index e725112b..6b57c3ca 100644
--- a/toys/pending/arp.c
+++ b/toys/pending/arp.c
@@ -245,7 +245,7 @@ void arp_main(void)
if ((toys.optflags & FLAG_d) && !delete_entry()) return;
//show arp chache
- fd = xopen("/proc/net/arp", O_RDONLY);
+ fd = xopenro("/proc/net/arp");
buf = get_line(fd);
free(buf); //skip first line
diff --git a/toys/pending/crond.c b/toys/pending/crond.c
index 1b5e4194..df8b5f11 100644
--- a/toys/pending/crond.c
+++ b/toys/pending/crond.c
@@ -79,10 +79,11 @@ static void loginfo(uint8_t loglevel, char *msg, ...)
if (!TT.flagd && TT.logfile) {
int fd = open(TT.logfile, O_WRONLY | O_CREAT | O_APPEND, 0666);
- if (fd >=0 && fd != 2) {
+ if (fd==-1) perror_msg("'%s", TT.logfile);
+ else {
dup2(fd, 2);
close(fd);
- } else if (fd < 0) perror_msg("'%s", TT.logfile);
+ }
}
used = vsnprintf(NULL, 0, msg, d);
smsg = xzalloc(++used);
diff --git a/toys/pending/crontab.c b/toys/pending/crontab.c
index 80a881d7..0b1c47db 100644
--- a/toys/pending/crontab.c
+++ b/toys/pending/crontab.c
@@ -114,7 +114,7 @@ static int validate_component(int min, int max, char *src)
static int parse_crontab(char *fname)
{
char *line;
- int lno, fd = xopen(fname, O_RDONLY);
+ int lno, fd = xopenro(fname);
long plen = 0;
for (lno = 1; (line = get_rawline(fd, &plen, '\n')); lno++,free(line)) {
@@ -214,8 +214,7 @@ static void do_list(char *name)
int fdin;
snprintf(toybuf, sizeof(toybuf), "%s%s", TT.cdir, name);
- if ((fdin = open(toybuf, O_RDONLY)) == -1)
- error_exit("No crontab for '%s'", name);
+ fdin = xopenro(toybuf);
xsendfile(fdin, 1);
xclose(fdin);
}
@@ -233,7 +232,7 @@ static void update_crontab(char *src, char *dest)
snprintf(toybuf, sizeof(toybuf), "%s%s", TT.cdir, dest);
fdout = xcreate(toybuf, O_WRONLY|O_CREAT|O_TRUNC, 0600);
- fdin = xopen(src, O_RDONLY);
+ fdin = xopenro(src);
xsendfile(fdin, fdout);
xclose(fdin);
@@ -277,7 +276,7 @@ static void do_edit(struct passwd *pwd)
if (!stat(toybuf, &sb)) { // file exists and have some content.
if (sb.st_size) {
- srcfd = xopen(toybuf, O_RDONLY);
+ srcfd = xopenro(toybuf);
xsendfile(srcfd, destfd);
xclose(srcfd);
}
diff --git a/toys/pending/dd.c b/toys/pending/dd.c
index 9990a0f4..b09a7dfe 100644
--- a/toys/pending/dd.c
+++ b/toys/pending/dd.c
@@ -143,16 +143,6 @@ static void status()
}
}
-static int xmove_fd(int fd)
-{
- int newfd;
-
- if (fd > STDERR_FILENO) return fd;
- if ((newfd = fcntl(fd, F_DUPFD, 3) < 0)) perror_exit("dupfd IO");
- close(fd);
- return newfd;
-}
-
static void setup_inout()
{
/* for C_BS, in/out is done as it is. so only in.sz is enough.
@@ -164,10 +154,8 @@ static void setup_inout()
if (!TT.in.name) {
TT.in.name = "stdin";
TT.in.fd = STDIN_FILENO;
- } else {
- TT.in.fd = xopen(TT.in.name, O_RDONLY);
- TT.in.fd = xmove_fd(TT.in.fd);
- }
+ } else TT.in.fd = xopenro(TT.in.name);
+
//setup outout
if (!TT.out.name) {
TT.out.name = "stdout";
@@ -176,7 +164,6 @@ static void setup_inout()
int flags = O_WRONLY|O_CREAT;
if (!(toys.optflags&C_NOTRUNC)) flags |= O_TRUNC;
TT.out.fd = xcreate(TT.out.name, flags, 0666);
- TT.out.fd = xmove_fd(TT.out.fd);
}
if (TT.in.offset) {
diff --git a/toys/pending/dhcp.c b/toys/pending/dhcp.c
index cb9d15a1..b99bb4c0 100644
--- a/toys/pending/dhcp.c
+++ b/toys/pending/dhcp.c
@@ -566,7 +566,9 @@ static void run_script(dhcpc_result_t *res, char *name)
static uint32_t getxid(void)
{
uint32_t randnum;
- int fd = xopen("/dev/urandom", O_RDONLY);
+ int fd = xopenro("/dev/urandom");
+
+// TODO xreadfile
xreadall(fd, &randnum, sizeof(randnum));
xclose(fd);
return randnum;
diff --git a/toys/pending/dumpleases.c b/toys/pending/dumpleases.c
index 86cb4e76..ef17aab4 100644
--- a/toys/pending/dumpleases.c
+++ b/toys/pending/dumpleases.c
@@ -42,7 +42,7 @@ void dumpleases_main(void)
int i, fd;
if(!(toys.optflags & FLAG_f)) TT.file = "/var/lib/misc/dhcpd.leases"; //DEF_LEASE_FILE
- fd = xopen(TT.file, O_RDONLY);
+ fd = xopenro(TT.file);
xprintf("Mac Address IP Address Host Name Expires %s\n", (toys.optflags & FLAG_a) ? "at" : "in");
xread(fd, &written_time, sizeof(written_time));
current_time = time(NULL);
diff --git a/toys/pending/getty.c b/toys/pending/getty.c
index c7376288..25d04eaa 100644
--- a/toys/pending/getty.c
+++ b/toys/pending/getty.c
@@ -128,7 +128,7 @@ static void open_tty(void)
if ((setsid() < 0) && (getpid() != getsid(0)))
perror_exit("setsid");
xclose(0);
- xopen(TT.tty_name, O_RDWR|O_NDELAY|O_CLOEXEC);
+ xopen_stdio(TT.tty_name, O_RDWR|O_NDELAY|O_CLOEXEC);
fcntl(0, F_SETFL, fcntl(0, F_GETFL) & ~O_NONBLOCK); // Block read
dup2(0, 1);
dup2(0, 2);
diff --git a/toys/pending/klogd.c b/toys/pending/klogd.c
index 2c842889..d950981d 100644
--- a/toys/pending/klogd.c
+++ b/toys/pending/klogd.c
@@ -74,7 +74,7 @@ void klogd_main(void)
syslog(LOG_NOTICE, "KLOGD: started with Kernel ring buffer as log source\n");
klogctl(1, NULL, 0);
} else {
- TT.fd = xopen("/proc/kmsg", O_RDONLY); //_PATH_KLOG in paths.h
+ TT.fd = xopenro("/proc/kmsg"); //_PATH_KLOG in paths.h
syslog(LOG_NOTICE, "KLOGD: started with /proc/kmsg as log source\n");
}
openlog("Kernel", 0, LOG_KERN); //open connection to system logger..
diff --git a/toys/pending/last.c b/toys/pending/last.c
index b207afef..4b7b472e 100644
--- a/toys/pending/last.c
+++ b/toys/pending/last.c
@@ -96,7 +96,7 @@ void last_main(void)
pwidth = (toys.optflags & FLAG_W) ? 46 : 16;
*tm = time(tm+1);
- fd = xopen(file, O_RDONLY);
+ fd = xopenro(file);
loc = xlseek(fd, 0, SEEK_END);
// Loop through file structures in reverse order.
diff --git a/toys/pending/mdev.c b/toys/pending/mdev.c
index 975d31da..2688cf35 100644
--- a/toys/pending/mdev.c
+++ b/toys/pending/mdev.c
@@ -40,8 +40,8 @@ static void make_device(char *path)
gid_t gid = 0;
if (path) {
- // Try to read major/minor string
+ // Try to read major/minor string, returning if we can't
temp = strrchr(path, '/');
fd = open(path, O_RDONLY);
*temp = 0;
diff --git a/toys/pending/modprobe.c b/toys/pending/modprobe.c
index 7a35c186..ebf17a0e 100644
--- a/toys/pending/modprobe.c
+++ b/toys/pending/modprobe.c
@@ -367,7 +367,9 @@ static int ins_mod(char *modules, char *flags)
{
char *buf = NULL;
int len, res;
- int fd = xopen(modules, O_RDONLY);
+ int fd = xopenro(modules);
+
+ // TODO xreadfile()
len = fdlength(fd);
buf = xmalloc(len);
diff --git a/toys/pending/openvt.c b/toys/pending/openvt.c
index 042b897c..29f8a174 100644
--- a/toys/pending/openvt.c
+++ b/toys/pending/openvt.c
@@ -100,7 +100,7 @@ void openvt_main(void)
xioctl(fd, VT_GETSTATE, &vstate);
close(0); //new vt becomes stdin
- vt_fd = xopen(toybuf, O_RDWR);
+ vt_fd = xopen_stdio(toybuf, O_RDWR);
if (toys.optflags & FLAG_s) {
ioctl(vt_fd, VT_ACTIVATE, TT.vt_num);
ioctl(vt_fd, VT_WAITACTIVE, TT.vt_num);
diff --git a/toys/pending/sulogin.c b/toys/pending/sulogin.c
index e6cb8314..bc3638e3 100644
--- a/toys/pending/sulogin.c
+++ b/toys/pending/sulogin.c
@@ -88,7 +88,7 @@ void sulogin_main(void)
if (toys.optargs[0]) {
int fd;
- dup2((fd = xopen(toys.optargs[0], O_RDWR)), 0);
+ dup2((fd = xopen_stdin(toys.optargs[0], O_RDWR)), 0);
if (!isatty(0)) error_exit("%s: it is not a tty", toys.optargs[0]);
dup2( fd, 1);
dup2( fd, 2);
diff --git a/toys/pending/tftp.c b/toys/pending/tftp.c
index 60d5f172..1c6fe9e6 100644
--- a/toys/pending/tftp.c
+++ b/toys/pending/tftp.c
@@ -381,7 +381,7 @@ int file_put(void)
sd = init_tftp(&server);
packet = (uint8_t*)xzalloc(TFTP_IOBUFSIZE);
- fd = xopen(TT.local_file, O_RDONLY);
+ fd = xopenro(TT.local_file);
for (;;) { //first loop for request send and confirmation from server.
packetlen = mkpkt_request(packet, TFTP_OP_WRQ, TT.remote_file, 1);