eject.c (867B)
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 <stdlib.h> 9 #include <unistd.h> 10 11 #include "util.h" 12 13 enum { 14 CDROM_EJECT = 0x5309, 15 CDROM_CLOSE_TRAY = 0x5319, 16 }; 17 18 static void 19 usage(void) 20 { 21 eprintf("usage: %s [-t] [devname]\n", argv0); 22 } 23 24 int 25 main(int argc, char *argv[]) 26 { 27 int fd, out; 28 char *cdrom = "/dev/sr0"; 29 int tflag = 0; 30 31 ARGBEGIN { 32 case 't': 33 tflag = 1; 34 break; 35 default: 36 usage(); 37 } ARGEND; 38 39 if (argc > 1) 40 usage(); 41 else if (argc == 1) 42 cdrom = argv[0]; 43 44 fd = open(cdrom, O_RDONLY | O_NONBLOCK); 45 if (fd < 0) 46 eprintf("open %s:", cdrom); 47 if (tflag) { 48 if (ioctl(fd, CDROM_CLOSE_TRAY, &out) < 0) 49 eprintf("ioctl:"); 50 } else { 51 if (ioctl(fd, CDROM_EJECT, &out) < 0) 52 eprintf("ioctl:"); 53 } 54 close(fd); 55 return 0; 56 }