summaryrefslogtreecommitdiff
path: root/minix/commands/eject/eject.c
blob: f0821296cfb83e8f6f4dde7542f734b6e8364932 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*	eject 1.3 - Eject removable media		Author: Kees J. Bot
 *								11 Dec 1993
 */
#define nil 0
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>

void fatal(char *label)
{
	fprintf(stderr, "eject: %s: %s\n", label, strerror(errno));
	exit(1);
}

int main(int argc, char **argv)
{
	char *device;
	int fd;

	if (argc != 2) {
		fprintf(stderr, "Usage: eject <device>\n");
		exit(1);
	}

	device= argv[1];

	/* Try to open it in whatever mode. */
	fd= open(device, O_RDONLY);
	if (fd < 0 && errno == EACCES) fd= open(device, O_WRONLY);
	if (fd < 0) fatal(device);

	/* Tell it to eject. */
	if (ioctl(fd, DIOCEJECT, nil) < 0) fatal(device);
	exit(0);
}