eject.c - ubase - suckless linux base utils
 (HTM) git clone git://git.suckless.org/ubase
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       eject.c (1054B)
       ---
            1 /* See LICENSE file for copyright and license details. */
            2 #include <sys/ioctl.h>
            3 #include <sys/stat.h>
            4 #include <sys/types.h>
            5 
            6 #include <fcntl.h>
            7 #include <stdio.h>
            8 #include <unistd.h>
            9 
           10 #include "util.h"
           11 
           12 enum {
           13         OPEN_TRAY = 0x5309,
           14         CLOSE_TRAY = 0x5319,
           15 };
           16 
           17 static int tflag = 0;
           18 static int ret = 0;
           19 
           20 static void
           21 eject(const char *devname)
           22 {
           23         int fd, out;
           24 
           25         if ((fd = open(devname, O_RDONLY | O_NONBLOCK)) < 0) {
           26                 weprintf("open %s:", devname);
           27                 ret = 1;
           28         } else if (tflag && ioctl(fd, CLOSE_TRAY, &out) < 0) {
           29                 weprintf("ioctl %s:", devname);
           30                 ret = 1;
           31         } else if (!tflag && ioctl(fd, OPEN_TRAY, &out) < 0) {
           32                 weprintf("ioctl %s:", devname);
           33                 ret = 1;
           34         }
           35 
           36         if (fd >= 0 && close(fd) < 0) {
           37                 weprintf("close %s:", devname);
           38                 ret = 1;
           39         }
           40 }
           41 
           42 
           43 static void
           44 usage(void)
           45 {
           46         eprintf("usage: %s [-t] [device ...]\n", argv0);
           47 }
           48 
           49 int
           50 main(int argc, char *argv[])
           51 {
           52         ARGBEGIN {
           53         case 't':
           54                 tflag = 1;
           55                 break;
           56         default:
           57                 usage();
           58         } ARGEND;
           59 
           60         if (!argc) {
           61                 eject("/dev/sr0");
           62         } else {
           63                 for (; *argv; argc--, argv++)
           64                         eject(*argv);
           65         }
           66 
           67         return ret;
           68 }