Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 48 lines (40 sloc) 0.821 kb
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
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/dkio.h>
#include <sys/vtoc.h>

void
usage(char* argv0) {
printf("Usage: %s [char dev]\n", argv0);
}

int
main(int argc, char* argv[]) {
char* devpath = argv[1];
int devnode;
struct stat buf;
int ret;
int removable;

if(argc != 2) {
usage(argv[0]);
return -1;
}

if ((devnode = open(devpath, O_RDONLY)) < 0 ) {
printf("Could not open %s\n", devpath);
usage(argv[0]);
return -1;
}
    
if (fstat(devnode, &buf) == -1 || !S_ISCHR(buf.st_mode)) {
printf("%s: not a character device\n", devpath);
usage(argv[0]);
close(devnode);
return -1;
}

ret = ioctl(devnode, DKIOCREMOVABLE, &removable);

if ((ret >= 0) && (removable != 0)) {
close(devnode);
return 0;
}

close(devnode);
return 1;
}
Something went wrong with that request. Please try again.