Net-FreeDB

 view release on metacpan or  search on metacpan

lib/linux.h  view on Meta::CPAN

 /*********************************************
NAME: linux.h
FUNCTION: linux based cddb id generation code
CREATED BY: David Shultz
CREATED ON: 08/03/2001
*********************************************/
#ifndef LINUX_H
#define LINUX_H

struct toc {
    int min, sec, frame;
} cdtoc[100];

struct discdata {
    unsigned long   discid;
    int             num_of_trks;
    int             track_offsets[100];
    int             seconds;
};

unsigned int cddb_sum(int n)
{
    unsigned int ret;

    ret = 0;
    while (n > 0) {
        ret += (n % 10);
        n /= 10;
    }
    return ret;
}

unsigned long cddb_discid(int tot_trks)
{
    unsigned int i = 0;
    unsigned int t = 0;
    unsigned int n = 0;

    while (i < tot_trks) {
        n = n + cddb_sum((cdtoc[i].min * 60) + cdtoc[i].sec);
        i++;
    }

    t = ((cdtoc[tot_trks].min * 60) + cdtoc[tot_trks].sec) -
        ((cdtoc[0].min * 60) + cdtoc[0].sec);
    return ((n % 0xff) << 24 | t << 8 | tot_trks);
}

struct discdata get_disc_id(char* dev)
{
    struct discdata data;
    int i;

    data.num_of_trks = read_toc(dev);

    if (data.num_of_trks == -1) {
        return data;
    }

    data.discid = cddb_discid(data.num_of_trks);

    for (i = 0; i < data.num_of_trks; i++) {
        data.track_offsets[i] = (cdtoc[i].frame);
    }

    data.seconds = (cdtoc[data.num_of_trks].frame)/75;

    return data;
}

int read_toc(char* dev)
{
    int drive, i, status;
    struct cdrom_tochdr tochdr;
    struct cdrom_tocentry tocentry;

    drive = open(dev, O_RDONLY | O_NONBLOCK);
    if (drive == -1) {
        printf("Device Error: %d\n", errno);
        return(-1);
    }

    status = ioctl(drive, CDROM_DRIVE_STATUS, CDSL_CURRENT);
    if (status < 0) {
        printf("Error: Error getting drive status\n");
        return(-1);
    } else {
        switch(status) {
            case CDS_DISC_OK:
                printf("Disc ok, moving on\n");
                break;
            case CDS_TRAY_OPEN:
                printf("Error: Drive reporting tray open...exiting\n");
                close(drive);
                return(-1);
            case CDS_DRIVE_NOT_READY:
                printf("Error: Drive Not Ready...exiting\n");
                close(drive);
                return(-1);
            default:
                printf("This shouldn't happen\n");
                close(drive);
                return(-1);
        }
    }

    if (ioctl(drive, CDROMREADTOCHDR, &tochdr) == -1) {
        switch(errno) {
            case EBADF:
                printf("Error: Invalid device...exiting\n");
                break;



( run in 1.085 second using v1.01-cache-2.11-cpan-39bf76dae61 )