LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-13-2006, 04:17 AM   #1
youjie
LQ Newbie
 
Registered: Dec 2006
Posts: 1

Rep: Reputation: 0
CDRW Backup


Hi guys,

i have just written a cd-rw backup script to backup files in the the cd-rw. Anyone know how to check whether there is any cd-rom in the cd writer, whether the cd-rom is full? Are there are commands? Thanks in advance.

 
Old 12-13-2006, 05:23 PM   #2
ruhler
LQ Newbie
 
Registered: Nov 2006
Posts: 7
Blog Entries: 1

Rep: Reputation: 1
You can ask the cd writer (using c code) if it has a cd-rom in it using the ioctls as described in cdrom.h. For me that file is in /usr/include/linux/cdrom.h.

For example, I wrote this program which returns 0 if there's a cd in the drive, or nonzero if there isn't a cd in the drive ready to go. It appears to work for me. I suspect there are also ioctls for checking if the cd-rom is full.

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <linux/cdrom.h>
#include <string.h>

// Return 0 if a cd is in the drive
// Return nonzero if no cd is in the drive

#define DEFAULT_DRIVE "/dev/cdrom"

int main(int argc, char* argv[])
{
char* drive = DEFAULT_DRIVE;
int i;

// Get the drive location from command line.
for (i = 1; i < argc; i++)
{
if (strcmp("-h", argv[i]) == 0)
{
printf("cdindrive [-h] [drive]\n");
printf("\t returns 0 if cdindrive, else nonzero.\n");
printf("\t -h prints this help.\n");
printf("\t drive is the cd drive to check (ex: /dev/cdrom0)\n");
}
else
{
drive = argv[i];
}
}

// open a descriptor to the drive
int fd;
fd = open(drive, O_RDONLY);
if (fd < 0)
{
if (errno == ENOMEDIUM)
{
// This happened when no cd was in the drive.
return 1;
}
perror("open");
return 1;
}

// See if it's ready
switch (ioctl(fd, CDROM_DRIVE_STATUS))
{
case -1:
perror("ioctl");
return 1;

case CDS_NO_INFO:
case CDS_NO_DISC:
case CDS_TRAY_OPEN:
case CDS_DRIVE_NOT_READY:
return 1;

case CDS_DISC_OK:
return 0;
}
return 1;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
neither my cdrw nor my dvd/cdrw recognize new medie jimappleby Linux - Hardware 1 11-12-2006 09:11 AM
QSI DVD/CDRW SBW-081 - Cannot blank cdrw breakthestate Linux - Hardware 3 02-01-2006 04:01 PM
Selective backup to CDRW, or other backup methods. trekk Linux - Software 1 11-03-2003 02:46 PM
How to backup Linux Root on a CDRW kvsr_unix Linux - General 3 09-14-2003 08:18 PM
Liteon 52x cdrw - K3b doesn't see it as cdrw LooseCanon Linux - Hardware 20 08-29-2003 10:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration