ubase/eject.c

69 lines
1.0 KiB
C
Raw Normal View History

2013-09-04 12:31:06 +00:00
/* See LICENSE file for copyright and license details. */
#include <sys/ioctl.h>
2014-06-30 18:03:41 +00:00
#include <sys/stat.h>
#include <sys/types.h>
2013-09-04 12:31:06 +00:00
#include <fcntl.h>
#include <stdio.h>
2014-06-30 18:03:41 +00:00
#include <unistd.h>
2013-09-04 12:31:06 +00:00
#include "util.h"
enum {
OPEN_TRAY = 0x5309,
CLOSE_TRAY = 0x5319,
2013-09-04 12:31:06 +00:00
};
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;
}
}
2013-09-04 12:31:06 +00:00
static void
usage(void)
{
eprintf("usage: %s [-t] [device ...]\n", argv0);
2013-09-04 12:31:06 +00:00
}
int
main(int argc, char *argv[])
{
ARGBEGIN {
case 't':
tflag = 1;
break;
default:
usage();
} ARGEND;
if (!argc) {
eject("/dev/sr0");
2013-09-04 12:31:06 +00:00
} else {
for (; *argv; argc--, argv++)
eject(*argv);
2013-09-04 12:31:06 +00:00
}
return ret;
2013-09-04 12:31:06 +00:00
}