ubase

suckless linux base utils
git clone git://git.suckless.org/ubase
Log | Files | Refs | README | LICENSE

commit 90c75840894958edb000a1091e13d5dac6b86279
parent 4c714a929926e7da4e2d8959428306490f8cbd8e
Author: FRIGN <dev@frign.de>
Date:   Thu, 10 Sep 2015 00:04:54 +0200

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

Diffstat:
Meject.1 | 17++++++++++++-----
Meject.c | 58+++++++++++++++++++++++++++++++++++-----------------------
2 files changed, 47 insertions(+), 28 deletions(-)

diff --git a/eject.1 b/eject.1 @@ -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 @@ -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; @@ -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; }