xref: /minix3/minix/commands/eject/eject.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1 /*	eject 1.3 - Eject removable media		Author: Kees J. Bot
2  *								11 Dec 1993
3  */
4 #define nil 0
5 #include <sys/types.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <string.h>
11 #include <sys/ioctl.h>
12 
fatal(char * label)13 void fatal(char *label)
14 {
15 	fprintf(stderr, "eject: %s: %s\n", label, strerror(errno));
16 	exit(1);
17 }
18 
main(int argc,char ** argv)19 int main(int argc, char **argv)
20 {
21 	char *device;
22 	int fd;
23 
24 	if (argc != 2) {
25 		fprintf(stderr, "Usage: eject <device>\n");
26 		exit(1);
27 	}
28 
29 	device= argv[1];
30 
31 	/* Try to open it in whatever mode. */
32 	fd= open(device, O_RDONLY);
33 	if (fd < 0 && errno == EACCES) fd= open(device, O_WRONLY);
34 	if (fd < 0) fatal(device);
35 
36 	/* Tell it to eject. */
37 	if (ioctl(fd, DIOCEJECT, nil) < 0) fatal(device);
38 	exit(0);
39 }
40