-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdetach.c
More file actions
34 lines (29 loc) · 739 Bytes
/
detach.c
File metadata and controls
34 lines (29 loc) · 739 Bytes
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
#include <assert.h>
#include <stdio.h>
//#include <sys/disk.h>
#include <sys/ioctl.h>
#define DKIOCEJECT _IO('d', 21)
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
if(!argv[1]) {
fprintf(stderr, "Usage: detach diskN\n");
return 1;
}
char *p = argv[1];
if(p[0] != '/') {
asprintf(&p, "/dev/%s", p);
}
int fd = open(p, O_RDONLY);
if(fd == -1) {
fprintf(stderr, "Could not open: %s\n", strerror(errno));
return 1;
}
int ret = ioctl(fd, DKIOCEJECT);
if(ret == -1) {
fprintf(stderr, "Could not ioctl(DKIOCEJECT): %s\n", strerror(errno));
}
return 0;
}