[hackers] [ubase] Refactor eject(1) || FRIGN
commit 90c75840894958edb000a1091e13d5dac6b86279
Author: FRIGN <dev_AT_frign.de>
AuthorDate: Thu Sep 10 00:04:54 2015 +0200
Commit: sin <sin_AT_2f30.org>
CommitDate: Wed Sep 9 23:33:17 2015 +0100
Refactor eject(1)
Reword manpage to be more general (you do not only eject CD-ROM-drives,
but BluRay-drives, floppy drives, LaserDisk-readers, toaster, whatever).
Allow to specify multiple devices in the command line. Doesn't add
LOC (the few more lines added are due to stricter error-checking)
and might become handy for somebody in the future while not
breaking scripts that assume only one argument.
Crying like GNU coreutils when more than one device is given is
not suckless:
$ eject /dev/sr0 /dev/sr1
eject: too many arguments
diff --git a/eject.1 b/eject.1
index 5f1fcaf..480db5f 100644
--- a/eject.1
+++ b/eject.1
_AT_@ -1,18 +1,25 @@
-.Dd February 2, 2015
+.Dd September 9, 2015
.Dt EJECT 1
.Os ubase
.Sh NAME
.Nm eject
-.Nd eject removable media
+.Nd control device trays
.Sh SYNOPSIS
.Nm
.Op Fl t
+.Op Ar device ...
.Sh DESCRIPTION
.Nm
-allows the CD-ROM tray to be opened or closed under software
-control. If no arguments are given, the CD-ROM tray is opened.
+opens the tray of each
+.Ar device .
+If no
+.Ar device
+is given
+.Nm
+opens the tray of
+.Pa /dev/sr0 .
.Sh OPTIONS
.Bl -tag -width Ds
.It Fl t
-If supported, close the CD-ROM tray.
+Close instead of open the tray.
.El
diff --git a/eject.c b/eject.c
index 23a16e8..1de9179 100644
--- a/eject.c
+++ b/eject.c
_AT_@ -5,29 +5,50 @@
#include <fcntl.h>
#include <stdio.h>
-#include <stdlib.h>
#include <unistd.h>
#include "util.h"
enum {
- CDROM_EJECT = 0x5309,
- CDROM_CLOSE_TRAY = 0x5319,
+ OPEN_TRAY = 0x5309,
+ CLOSE_TRAY = 0x5319,
};
+static int tflag = 0;
+static int ret = 0;
+
+static void
+eject(const char *devname)
+{
+ int fd, out;
+
+ if ((fd = open(devname, O_RDONLY | O_NONBLOCK)) < 0) {
+ weprintf("open %s:", devname);
+ ret = 1;
+ } else if (tflag && ioctl(fd, CLOSE_TRAY, &out) < 0) {
+ weprintf("ioctl %s:", devname);
+ ret = 1;
+ } else if (!tflag && ioctl(fd, OPEN_TRAY, &out) < 0) {
+ weprintf("ioctl %s:", devname);
+ ret = 1;
+ }
+
+ if (fd >= 0 && close(fd) < 0) {
+ weprintf("close %s:", devname);
+ ret = 1;
+ }
+}
+
+
static void
usage(void)
{
- eprintf("usage: %s [-t] [devname]\n", argv0);
+ eprintf("usage: %s [-t] [device ...]\n", argv0);
}
int
main(int argc, char *argv[])
{
- int fd, out;
- char *cdrom = "/dev/sr0";
- int tflag = 0;
-
ARGBEGIN {
case 't':
tflag = 1;
_AT_@ -36,21 +57,12 @@ main(int argc, char *argv[])
usage();
} ARGEND;
- if (argc > 1)
- usage();
- else if (argc == 1)
- cdrom = argv[0];
-
- fd = open(cdrom, O_RDONLY | O_NONBLOCK);
- if (fd < 0)
- eprintf("open %s:", cdrom);
- if (tflag) {
- if (ioctl(fd, CDROM_CLOSE_TRAY, &out) < 0)
- eprintf("ioctl:");
+ if (!argc) {
+ eject("/dev/sr0");
} else {
- if (ioctl(fd, CDROM_EJECT, &out) < 0)
- eprintf("ioctl:");
+ for (; *argv; argc--, argv++)
+ eject(*argv);
}
- close(fd);
- return 0;
+
+ return ret;
}
Received on Thu Sep 10 2015 - 00:33:23 CEST
This archive was generated by hypermail 2.3.0
: Thu Sep 10 2015 - 00:36:12 CEST