1
0
mirror of git://git.suckless.org/ubase synced 2024-12-17 20:24:45 +00:00

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
.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

58
eject.c
View File

@ -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;
}