1
0
mirror of git://git.suckless.org/ubase synced 2024-12-18 04:34:43 +00:00
ubase/eject.c
FRIGN 90c7584089 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
2015-09-09 23:33:17 +01:00

69 lines
1.0 KiB
C

/* See LICENSE file for copyright and license details. */
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include "util.h"
enum {
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] [device ...]\n", argv0);
}
int
main(int argc, char *argv[])
{
ARGBEGIN {
case 't':
tflag = 1;
break;
default:
usage();
} ARGEND;
if (!argc) {
eject("/dev/sr0");
} else {
for (; *argv; argc--, argv++)
eject(*argv);
}
return ret;
}