Last active
December 20, 2015 09:09
-
-
Save JonnyJD/6105296 to your computer and use it in GitHub Desktop.
Preliminary tests to implement drutil's optical disc drive numbering in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <IOKit/IOKitLib.h> | |
#include <IOKit/IOBSD.h> | |
#include <IOKit/storage/IOCDMedia.h> | |
#include <IOKit/storage/IOCDBlockStorageDevice.h> | |
#include <CoreFoundation/CoreFoundation.h> | |
#define MAXPATHLEN 1024 | |
static void find_cd_block_devices(io_iterator_t *deviceIterator) | |
{ | |
mach_port_t masterPort; | |
CFMutableDictionaryRef classesToMatch; | |
IOMasterPort(MACH_PORT_NULL, &masterPort); | |
classesToMatch = IOServiceMatching(kIOCDBlockStorageDeviceClass); | |
IOServiceGetMatchingServices(masterPort, classesToMatch, deviceIterator); | |
} | |
static int get_device_file_path(io_object_t nextMedia, char * deviceFilePath) | |
{ | |
CFTypeRef deviceFilePathAsCFString; | |
*deviceFilePath = '\0'; | |
deviceFilePathAsCFString = IORegistryEntryCreateCFProperty(nextMedia, | |
CFSTR(kIOBSDNameKey), kCFAllocatorDefault, 0); | |
if (deviceFilePathAsCFString) { | |
if (CFStringGetCString(deviceFilePathAsCFString, deviceFilePath, | |
MAXPATHLEN, kCFStringEncodingASCII)) | |
return 1; | |
CFRelease(deviceFilePathAsCFString); | |
} | |
return 0; | |
} | |
static io_object_t get_media(io_object_t storageDevice) | |
{ | |
io_iterator_t childrenIterator; | |
io_object_t nextChild; | |
io_object_t mediaObject = IO_OBJECT_NULL; | |
IORegistryEntryCreateIterator(storageDevice, | |
kIOServicePlane, kIORegistryIterateRecursively, | |
&childrenIterator); | |
while ((nextChild = IOIteratorNext(childrenIterator))) { | |
if (IOObjectConformsTo(nextChild, kIOCDMediaClass)) | |
mediaObject = nextChild; | |
else | |
IOObjectRelease(nextChild); | |
} | |
IOObjectRelease(childrenIterator); | |
return mediaObject; | |
} | |
static void print_info_for_objects(io_iterator_t objectIterator) | |
{ | |
io_object_t nextObject; | |
io_object_t mediaObject; | |
io_name_t nodeName; | |
char device_name[MAXPATHLEN+1] = "\0"; | |
int index = 0; | |
while ((nextObject = IOIteratorNext(objectIterator))) { | |
printf("drive %d", ++index); | |
if ((mediaObject = get_media(nextObject))) { | |
if (get_device_file_path(mediaObject, device_name)) | |
printf(": %s\n", device_name); | |
} else | |
printf("\n", ++index); | |
IOObjectRelease(mediaObject); | |
IOObjectRelease(nextObject); | |
} | |
} | |
int main() { | |
io_iterator_t driveIterator; | |
find_cd_block_devices(&driveIterator); | |
print_info_for_objects(driveIterator); | |
IOObjectRelease(driveIterator); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
disc_drive_numbers: disc_drive_numbers.c | |
${CC} $^ -framework CoreFoundation -framework IOKit -o $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment