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