-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheject.c
166 lines (143 loc) · 4.03 KB
/
eject.c
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Eject removable drive tool in windows, works like a simple eject tool
* in linux;
* For cdrom test, used to lock/unlock, eject/close cdrom in windows platform;
*
* Tested on win7 64bit OS and compile it with dev c++;
*
*/
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
void usage(int ret)
{
printf("eject removable media\n");
printf("eject [-t] [-i off|on|1|0] x:\n");
printf("\t: -t: close cdrom\n");
printf("\t: -i on|off|1|0: close cdrom\n");
printf("\t: x: cdrom drive letter eg, E:");
exit(ret);
}
static DWORD get_drive_type(WCHAR drive)
{
static const WCHAR rootW[] = {'a',':','\\',0};
WCHAR path[16];
memcpy( path, rootW, sizeof(rootW));
path[0] = drive;
return GetDriveTypeW(path);
}
static BOOL cdrom_io_control(HANDLE handle, DWORD command, void *pr)
{
DWORD result;
DWORD buffer_size = 0;
if(pr != NULL) buffer_size = sizeof(pr);
if(!DeviceIoControl(handle, command, pr, buffer_size, NULL, 0,
&result, NULL))
{
printf("%s failed with err %d", command, GetLastError());
exit(GetLastError());
}
return TRUE;
}
static HANDLE get_handler(WCHAR drive)
{
static const WCHAR deviceW[] = {'\\', '\\', '.', '\\', 'a', ':', 0};
WCHAR buffer[16];
HANDLE handle;
if (get_drive_type(drive) != DRIVE_CDROM)
{
printf("Drive %c: is not a CD or is not mounted\n", (char)drive);
exit(1);
}
memcpy(buffer, deviceW, sizeof(deviceW));
buffer[4] = drive;
handle = CreateFileW(buffer, GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, 0 );
if (handle == INVALID_HANDLE_VALUE)
{
printf("Cannot get_handler device for drive %c:\n", (char)drive);
exit(1);
}
cdrom_io_control(handle, FSCTL_LOCK_VOLUME, NULL);
return handle;
}
static BOOL close_handler(HANDLE handle)
{
BOOL ret;
ret = cdrom_io_control(handle, FSCTL_UNLOCK_VOLUME, NULL);
CloseHandle(handle);
return ret;
}
static BOOL eject_cdrom(WCHAR drive)
{
HANDLE handle;
PREVENT_MEDIA_REMOVAL removal;
removal.PreventMediaRemoval = FALSE;
handle = get_handler(drive);
cdrom_io_control(handle, FSCTL_DISMOUNT_VOLUME, NULL);
cdrom_io_control(handle, IOCTL_STORAGE_MEDIA_REMOVAL, &removal);
cdrom_io_control(handle, IOCTL_STORAGE_EJECT_MEDIA, NULL);
close_handler(handle);
return TRUE;
}
static BOOL close_cdrom(WCHAR drive)
{
HANDLE handle;
BOOL ret;
handle = get_handler(drive);
ret = cdrom_io_control(handle, IOCTL_STORAGE_LOAD_MEDIA, NULL);
close_handler(handle);
return ret;
}
static BOOL lock_cdrom(WCHAR drive)
{
HANDLE handle;
PREVENT_MEDIA_REMOVAL removal;
removal.PreventMediaRemoval = TRUE;
handle = get_handler(drive);
cdrom_io_control(handle, IOCTL_STORAGE_MEDIA_REMOVAL, &removal);
close_handler(handle);
return TRUE;
}
static BOOL unlock_cdrom(WCHAR drive)
{
HANDLE handle;
PREVENT_MEDIA_REMOVAL removal;
removal.PreventMediaRemoval = FALSE;
handle = get_handler(drive);
cdrom_io_control(handle, IOCTL_STORAGE_MEDIA_REMOVAL, &removal);
close_handler(handle);
return TRUE;
}
int main(int argc, char *argv[])
{
int ch;
opterr = 0;
if (argc < 2) usage(1);
DWORD drive = argv[argc -1][0];
while((ch = getopt(argc, argv, "hi:t")) != -1)
{
switch(ch)
{
case 'h':
usage(0);
case 'i':
if (!strcmp(optarg, "on") || !strcmp(optarg, "1"))
lock_cdrom(drive) ? exit(0) : exit(1);
if (!strcmp(optarg, "off") || !strcmp(optarg, "0"))
unlock_cdrom(drive) ? exit(0) : exit(1);
usage(1);
case 't':
close_cdrom(drive) ? exit(0) : exit(1);
default:
usage(1);
}
}
eject_cdrom(drive);
return 0;
}