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
This commit is contained in:
FRIGN 2015-09-10 00:04:54 +02:00 committed by sin
parent 4c714a9299
commit 90c7584089
2 changed files with 47 additions and 28 deletions

17
eject.1
View File

@ -1,18 +1,25 @@
.Dd February 2, 2015 .Dd September 9, 2015
.Dt EJECT 1 .Dt EJECT 1
.Os ubase .Os ubase
.Sh NAME .Sh NAME
.Nm eject .Nm eject
.Nd eject removable media .Nd control device trays
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl t .Op Fl t
.Op Ar device ...
.Sh DESCRIPTION .Sh DESCRIPTION
.Nm .Nm
allows the CD-ROM tray to be opened or closed under software opens the tray of each
control. If no arguments are given, the CD-ROM tray is opened. .Ar device .
If no
.Ar device
is given
.Nm
opens the tray of
.Pa /dev/sr0 .
.Sh OPTIONS .Sh OPTIONS
.Bl -tag -width Ds .Bl -tag -width Ds
.It Fl t .It Fl t
If supported, close the CD-ROM tray. Close instead of open the tray.
.El .El

58
eject.c
View File

@ -5,29 +5,50 @@
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
enum { enum {
CDROM_EJECT = 0x5309, OPEN_TRAY = 0x5309,
CDROM_CLOSE_TRAY = 0x5319, 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 static void
usage(void) usage(void)
{ {
eprintf("usage: %s [-t] [devname]\n", argv0); eprintf("usage: %s [-t] [device ...]\n", argv0);
} }
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int fd, out;
char *cdrom = "/dev/sr0";
int tflag = 0;
ARGBEGIN { ARGBEGIN {
case 't': case 't':
tflag = 1; tflag = 1;
@ -36,21 +57,12 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if (argc > 1) if (!argc) {
usage(); eject("/dev/sr0");
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:");
} else { } else {
if (ioctl(fd, CDROM_EJECT, &out) < 0) for (; *argv; argc--, argv++)
eprintf("ioctl:"); eject(*argv);
} }
close(fd);
return 0; return ret;
} }