Question

I am distributing software that uses an optical disk, and at the default full speed it is too noisy to be acceptable. My goal is to reduce the speed of the disk using ioctl, but I don't know how to find /dev/disk(n) from /Volumes/MyDisk/Application.

The following is what I have so far, but I do not want the disk path hard-coded.

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <IOKit/storage/IOCDMediaBSDClient.h>

int main () {
    // ------------------------------------
    //   Open Drive
    // ------------------------------------
    int fd = open("/dev/disk1",O_RDONLY);
    if (fd == -1) {
        printf("Error opening drive \n");
        exit(1);
    }

    // ------------------------------------
    //   Get Speed
    // ------------------------------------
    unsigned int speed;
    if (ioctl(fd,DKIOCCDGETSPEED,&speed)) {
        printf("Must not be a CD \n");
    }
    else {
        printf("CD Speed: %d KB/s \n",speed);
    }

    // ------------------------------------
    //   Close Drive
    // ------------------------------------
    close(fd);
    return 0;
}
Was it helpful?

Solution

You may have to walk the disk entries in /dev, opening each and then using some other ioctl() to identify their type.

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <IOKit/storage/IOCDMediaBSDClient.h>

int main( int argc, char *argv[])
{
    int i, fd;
    unsigned short speed;
    char disk[40];

    for (i = 0; i < 100; ++i)
    {
        sprintf( disk, "/dev/disk%u", i);
        fd = open( disk, O_RDONLY);
        if (fd != -1)
        {
            if (ioctl( fd, DKIOCCDGETSPEED, &speed))
            {
                printf( "%s is not a CD\n", disk);
            }
            else
            {
                printf( "%s CD Speed is %u KB/s\n", disk, speed);
            }
            close( fd);
        }
    }

    return 0;
}

On my old MacBook Pro, without a disk in the DVD drive, it tells me that neither disk0 nor disk1 are CD drives. With a disk loaded (and the code fixed to use an unsigned short for speed), it reports /dev/disk2 as a CD with a speed of 4234 KB/s.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top