#!/usr/bin/perl -w # $Id: burn_cd,v 1.7 1999/09/23 18:19:23 jabrown Exp $ # Jeff Brown # THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # This is meant to be a simple front-end to cdrecord, not a comprehensive one. my $cd_device = "0,6,0"; # Bus, target, LUN my $default_write_speed = "4"; my @valid_speeds = ("1", "2", "4", "6"); my $buffer_size = "16m"; my $prog_name = "burn_cd"; $ENV{'PATH'}="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin"; sub usage_long { print STDERR "Usage: ${prog_name} [-dummy] [-speed N] imagefile.iso\n" . " ${prog_name} -blank \n" . "\n" . "The first form will burn a single-session data CD containing the given ISO\n" . "filesystem image.\n" . " -dummy: do a test write with the laser off.\n" . " -speed N: write at N-speed, where N is one of: @{valid_speeds} [default=${default_write_speed}].\n" . "\n" . "The second form will erase a CD-RW disk.\n" . " \"fast\": erase only the beginning meta-data so the disc can be rewritten.\n" . " \"all\": erase the entire disc.\n" . "\n" . "Load the CD-R media before beginning, and DO NOT INTERRUPT ANY WRITE --\n" . "EVEN A DUMMY ONE -- it may hang the CD-R drive, necessitating a reboot.\n"; exit 1; } sub usage_short { print STDERR "${prog_name}: invalid command line. Use \"${prog_name} -h\" for usage info.\n"; exit 1; } my $operation; # 0 = write CD, 1 = erase CD-RW my $dummy_flag = 0; my $write_speed; # one of @valid_speeds my $blank_mode; # "fast" or "all" my $iso_fname; undef $write_speed; undef $blank_mode; undef $iso_fname; if ($#ARGV < 0) { usage_short; } if ($ARGV[0] eq "-blank") { $operation = 1; # Erase CD-RW while ($#ARGV >= 0) { $arg = shift(@ARGV); if ($arg eq "-blank") { $arg = shift(@ARGV); if (defined($arg) && (($arg eq "fast") || ($arg eq "all"))) { $blank_mode = $arg; } else { usage_short; } } else { usage_short; } } if (!defined($blank_mode)) { usage_short; } } else { $operation = 0; # Write CD while ($#ARGV >= 0) { $arg = shift(@ARGV); if ($arg eq "-h" || $arg eq "-?" || $arg eq "-help") { usage_long; } elsif ($arg eq "-dummy") { $dummy_flag = 1; } elsif ($arg eq "-speed") { $arg = shift(@ARGV); if (defined($arg) && scalar(grep(/^${arg}$/, @valid_speeds) > 0)) { $write_speed = $arg; } else { usage_short; } } elsif (!defined($iso_fname)) { $iso_fname = $arg; } else { usage_short; } } if (!defined($iso_fname)) { usage_short; } } @cmd_line = ("cdrecord", "-v", "dev=${cd_device}"); if ($operation == 0) { # Be anal with filenames to keep from confusing cdrecord's parser. if ($iso_fname =~ /^-/) { print STDERR "${prog_name}: Input filenames may not begin with '-' " . "('$iso_fname')\n"; exit 1; } elsif (!($iso_fname =~ m:^[a-zA-Z0-9_/.-]+$:)) { print STDERR "${prog_name}: Input filenames may only contain " . "[a-zA-Z0-9_/.-] ('$iso_fname')\n"; exit 1; } $pid = fork(); if (!defined($pid)) { print STDERR "${prog_name}: fork() failed: $!\n"; exit 1; } elsif ($pid == 0) { # Child -- drop any suid/sgid $> = $<; $) = $(; if (! -r $iso_fname) { print STDERR "${prog_name}: cannot read input file '${iso_fname}'\n"; exit 1; } exit 0; } else { # Parent if ((waitpid($pid, 0) == -1) || ($? != 0)) { exit 1; } } push(@cmd_line, ("-dao", "fs=${buffer_size}", "-data")); push(@cmd_line, (defined($write_speed)) ? "-speed=${write_speed}" : "-speed=${default_write_speed}"); if ($dummy_flag) { push(@cmd_line, "-dummy"); } push(@cmd_line, "$iso_fname"); } elsif ($operation == 1) { push(@cmd_line, "blank=${blank_mode}"); } else { print STDERR "${prog_name}: Oy! Unknown operation code ${operation}.\n"; exit 2; } print "Command: @cmd_line\n"; sleep(1); system(@cmd_line);