2 Copyright (c) 2000-2012 Craig Condit, Stefan Huelswitt.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
7 1. Redistributions of source code must retain the above copyright notice,
8 this list of conditions and the following disclaimer.
9 2. Redistributions in binary form must reproduce the above copyright notice,
10 this list of conditions and the following disclaimer in the documentation
11 and/or other materials provided with the distribution.
13 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
14 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
17 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #define _LARGEFILE64_SOURCE
30 #include <sys/ioctl.h>
40 /* size of leadin/out depending of how many tracks are on cd */
41 #define LEADOUT_1 11400 /* 1th track */
42 #define LEADOUT_2 6900 /* 2nd and more tracks */
43 /* number of (unreadable) runout sectos */
48 extern char *prg_name;
50 /****************************************************************************/
52 void get_param(int fd, unsigned long *ahead, unsigned long *fahead)
54 #if defined(BLKRAGET) && defined(BLKFRAGET)
56 ioctl(fd,BLKRAGET,ahead);
57 ioctl(fd,BLKFRAGET,fahead);
58 DEBUG("get_param: readahead=%ld freadahead=%ld\n",*ahead,*fahead);
60 fprintf(stderr,"Can't get readahead parameter. Ioctl's not available\n");
64 /****************************************************************************/
66 void set_param(int fd, unsigned long ahead, unsigned long fahead)
68 #if defined(BLKRAGET) && defined(BLKFRAGET)
69 ioctl(fd,BLKRASET,ahead);
70 ioctl(fd,BLKFRASET,fahead);
71 DEBUG("set_param: readahead=%ld freadahead=%ld\n",ahead,fahead);
73 fprintf(stderr,"Can't set readahead parameter. Ioctl's not available\n");
77 /****************************************************************************/
79 int getCdHeader(struct cd_header *cd)
81 struct cdrom_tochdr cd_header;
82 struct cdrom_tocentry cd_entry;
85 if(ioctl(fd,CDROMREADTOCHDR,&cd_header)<0) {
86 if(verbose) fprintf(stderr,"%s: Unable to read CD header, assuming empty CD-R\n",prg_name);
87 cd->start_track=1; cd->end_track=0;
92 cd->start_track=cd_header.cdth_trk0;
93 cd->end_track =cd_header.cdth_trk1;
94 tracks=cd->end_track-cd->start_track+1;
96 cd_entry.cdte_format=CDROM_LBA;
97 cd_entry.cdte_track=CDROM_LEADOUT;
98 if(ioctl(fd,CDROMREADTOCENTRY,&cd_entry)<0) error("Ioctl failed (lead-out)");
100 cd->used=cd_entry.cdte_addr.lba + (tracks==1 ? LEADOUT_1:LEADOUT_2);
104 /****************************************************************************/
106 void getCdTrack(int num, struct cd_track *cd)
108 struct cdrom_tocentry cd_entry;
110 cd_entry.cdte_format=CDROM_LBA;
111 cd_entry.cdte_track=num;
112 if(ioctl(fd,CDROMREADTOCENTRY,&cd_entry)<0) error("Ioctl failed (toc entry)");
114 cd->start_sec =cd_entry.cdte_addr.lba;
115 cd->leadout_size=RUNOUT+(num==cd->start_track ? LEADOUT_1:LEADOUT_2);
116 cd->is_data =(cd_entry.cdte_ctrl&CDROM_DATA_TRACK) ? 1:0;