xref: /onnv-gate/usr/src/cmd/cdrw/main.c (revision 7024:c74e1591c8fd)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
56222Szk194757  * Common Development and Distribution License (the "License").
66222Szk194757  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
226222Szk194757  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <limits.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <libintl.h>
340Sstevel@tonic-gate #include <locale.h>
356222Szk194757 #include <dbus/dbus.h>
366222Szk194757 #include <hal/libhal.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include "msgs.h"
390Sstevel@tonic-gate #include "device.h"
400Sstevel@tonic-gate #include "util.h"
410Sstevel@tonic-gate #include "main.h"
420Sstevel@tonic-gate #include "options.h"
430Sstevel@tonic-gate #include "mmc.h"
440Sstevel@tonic-gate #include "misc_scsi.h"
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * global flags
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate int	debug = 0;
500Sstevel@tonic-gate int	keep_disc_open = 0;
510Sstevel@tonic-gate int	requested_speed = 0;
520Sstevel@tonic-gate int	simulation = 0;
530Sstevel@tonic-gate int	verbose = 0;
540Sstevel@tonic-gate char	*image_file = NULL;
550Sstevel@tonic-gate char	*blanking_type = NULL;
560Sstevel@tonic-gate int	audio_type = AUDIO_TYPE_NONE;
570Sstevel@tonic-gate int	extract_track_no = 0;
580Sstevel@tonic-gate char	*extract_file = NULL;
590Sstevel@tonic-gate char	*alt_tmp_dir = NULL;
600Sstevel@tonic-gate char	*copy_src = NULL;
610Sstevel@tonic-gate int	vol_running = 0;
620Sstevel@tonic-gate int	cflag = 0;
630Sstevel@tonic-gate int	tflag = 0;
640Sstevel@tonic-gate uid_t	ruid, cur_uid;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * global variables
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate cd_device *target = NULL;		/* Default target device */
700Sstevel@tonic-gate static char *tgtdev = NULL;
710Sstevel@tonic-gate int device_type = CD_RW;		/* Default to CD/RW */
720Sstevel@tonic-gate int write_mode = TAO_MODE;		/* Default to track at once */
730Sstevel@tonic-gate 
740Sstevel@tonic-gate static void
print_usage(void)750Sstevel@tonic-gate print_usage(void)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	err_msg(gettext("USAGE:\n"));
780Sstevel@tonic-gate 	err_msg(gettext("\tcdrw -i [ -vSCO ] [ -d device ] [ -p speed ]"));
790Sstevel@tonic-gate 	err_msg(gettext(" [ image-file ]\n"));
800Sstevel@tonic-gate 	err_msg(gettext("\tcdrw -a [ -vSCO ] [ -d device ] [ -p speed ]"));
810Sstevel@tonic-gate 	err_msg(gettext(" [ -T audio-type ] audio-file1 audio-file2 ...\n"));
820Sstevel@tonic-gate 	err_msg(gettext("\tcdrw -x [ -v ] [ -d device ] [ -T audio-type ]"));
830Sstevel@tonic-gate 	err_msg(gettext(" track-number audio-file\n"));
846361Sanbui 	err_msg(gettext("\tcdrw -c [ -SC ] [ -d device ] [ -p speed ]"));
850Sstevel@tonic-gate 	err_msg(gettext(" [ -m tmp-dir ] [ -s src-device ]\n"));
860Sstevel@tonic-gate 	err_msg(
870Sstevel@tonic-gate 	    gettext("\tcdrw -b [ -v ] [ -d device ] all | session | fast\n"));
880Sstevel@tonic-gate 	err_msg(gettext("\tcdrw -M [ -v ] [ -d device ]\n"));
896361Sanbui 	err_msg(gettext("\tcdrw -L [ -v ] [ -d device ]\n"));
900Sstevel@tonic-gate 	err_msg(gettext("\tcdrw -l [ -v ]\n"));
910Sstevel@tonic-gate 	err_msg(gettext("\tcdrw -h\n"));
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	exit(2);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate static void
check_invalid_option(options * specified,char * opstr)970Sstevel@tonic-gate check_invalid_option(options *specified, char *opstr)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	options c_op;
1000Sstevel@tonic-gate 	int ret;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	set_options_mask(&c_op, opstr);
1030Sstevel@tonic-gate 	if ((ret = compare_options_mask(&c_op, specified)) != 0) {
1040Sstevel@tonic-gate 		err_msg(
1050Sstevel@tonic-gate 		    gettext("Option %c is not defined for this operation.\n"),
1060Sstevel@tonic-gate 		    (char)ret);
1070Sstevel@tonic-gate 		print_usage();
1080Sstevel@tonic-gate 	}
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1116222Szk194757 LibHalContext *
attach_to_hald(void)1126222Szk194757 attach_to_hald(void)
1136222Szk194757 {
1146222Szk194757 	LibHalContext *ctx = NULL;
1156222Szk194757 	DBusConnection *con = NULL;
1166222Szk194757 	DBusError error;
1176222Szk194757 	hal_state_t state;
1186222Szk194757 
1196222Szk194757 	/* Initialize the dbus error states */
1206222Szk194757 	dbus_error_init(&error);
1216222Szk194757 
1226222Szk194757 	if ((con = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) == NULL) {
1236222Szk194757 		return (NULL);
1246222Szk194757 	}
1256222Szk194757 	state = DBUS_CONNECTION;
1266222Szk194757 
1276222Szk194757 	/* Allocate a new hal context to work with the dbus */
1286222Szk194757 	if ((ctx = libhal_ctx_new()) == NULL)
1296222Szk194757 		return (NULL);
1306222Szk194757 	state = HAL_CONTEXT;
1316222Szk194757 
1326222Szk194757 	/* Pair up the context with the connection */
1336222Szk194757 	if (!libhal_ctx_set_dbus_connection(ctx, con))
1346222Szk194757 		goto fail;
1356222Szk194757 	state = HAL_PAIRED;
1366222Szk194757 
1376222Szk194757 	/* If libhal_ctx_init fails hald is not present */
1386222Szk194757 	if (!libhal_ctx_init(ctx, &error)) {
1396222Szk194757 		goto fail;
1406222Szk194757 	}
1416222Szk194757 	state = HAL_INITIALIZED;
1426222Szk194757 
1436222Szk194757 	return (ctx);
1446222Szk194757 fail:
1456222Szk194757 	if (dbus_error_is_set(&error))
1466222Szk194757 		dbus_error_free(&error);
1476222Szk194757 	detach_from_hald(ctx, state);
1486222Szk194757 	return (NULL);
1496222Szk194757 
1506222Szk194757 }
1516222Szk194757 
1526222Szk194757 void
detach_from_hald(LibHalContext * ctx,hal_state_t state)1536222Szk194757 detach_from_hald(LibHalContext *ctx, hal_state_t state)
1546222Szk194757 {
1556222Szk194757 	DBusError error;
1566222Szk194757 	DBusConnection *con = libhal_ctx_get_dbus_connection(ctx);
1576222Szk194757 
1586222Szk194757 	dbus_error_init(&error);
1596222Szk194757 
1606222Szk194757 	switch (state) {
1616222Szk194757 	case HAL_INITIALIZED:
1626222Szk194757 		if (libhal_ctx_shutdown(ctx, &error) == FALSE)
1636222Szk194757 			if (dbus_error_is_set(&error))
1646222Szk194757 				dbus_error_free(&error);
1656222Szk194757 	/*FALLTHROUGH*/
1666222Szk194757 	case HAL_PAIRED:
1676222Szk194757 		(void) libhal_ctx_free(ctx);
1686222Szk194757 		dbus_connection_unref(con);
1696222Szk194757 		break;
1706222Szk194757 	case HAL_CONTEXT:
1716222Szk194757 		(void) libhal_ctx_free(ctx);
1726222Szk194757 		break;
1736222Szk194757 	case DBUS_CONNECTION:
1746222Szk194757 	default:
1756222Szk194757 		break;
1766222Szk194757 	}
1776222Szk194757 }
1786222Szk194757 
1796222Szk194757 /*
1806222Szk194757  * This function returns one if hald is running and
1816222Szk194757  * zero if hald is not running
1826222Szk194757  */
1836222Szk194757 int
hald_running(void)1846222Szk194757 hald_running(void)
1856222Szk194757 {
1866222Szk194757 	LibHalContext *ctx;
1876222Szk194757 
1886222Szk194757 	if ((ctx = attach_to_hald()) == NULL)
1896222Szk194757 		return (0);
1906222Szk194757 
1916222Szk194757 	detach_from_hald(ctx, HAL_INITIALIZED);
1926222Szk194757 	return (1);
1936222Szk194757 }
1946222Szk194757 
1950Sstevel@tonic-gate int
setup_target(int flag)1960Sstevel@tonic-gate setup_target(int flag)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	char *devpath;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	if (tgtdev != NULL) {
2010Sstevel@tonic-gate 		devpath = (char *)my_zalloc(PATH_MAX);
2020Sstevel@tonic-gate 		if (lookup_device(tgtdev, devpath)) {
2030Sstevel@tonic-gate 			target = get_device(tgtdev, devpath);
2040Sstevel@tonic-gate 		}
2050Sstevel@tonic-gate 		free(devpath);
2060Sstevel@tonic-gate 		if (target == NULL) {
2070Sstevel@tonic-gate 			return (0);
2080Sstevel@tonic-gate 		}
2090Sstevel@tonic-gate 		return (1);
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 	return (scan_for_cd_device(flag, &target));
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
214370Snakanon int
main(int argc,char ** argv)2150Sstevel@tonic-gate main(int argc, char **argv)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate 	int		c;
2180Sstevel@tonic-gate 	int		operations;
2190Sstevel@tonic-gate 	options		specified_ops;
2200Sstevel@tonic-gate 	int		aflag, iflag, Mflag, Lflag, lflag, bflag, xflag;
2210Sstevel@tonic-gate 	int		ret;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
2260Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
2270Sstevel@tonic-gate #endif
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	ruid = getuid();
2330Sstevel@tonic-gate 	cur_uid = geteuid();
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	if (check_auth(ruid) != 1)  {
2360Sstevel@tonic-gate 		err_msg(gettext(
2370Sstevel@tonic-gate 		    "Authorization failed, Cannot access disks.\n"));
2380Sstevel@tonic-gate 		exit(1);
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	if ((cur_uid == 0) && (ruid != 0)) {
2420Sstevel@tonic-gate 		priv_change_needed = 1;
2430Sstevel@tonic-gate 		lower_priv();
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 
2466222Szk194757 	vol_running = hald_running();
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	tgtdev = NULL;
2490Sstevel@tonic-gate 	operations = 0;
2500Sstevel@tonic-gate 	set_options_mask(&specified_ops, "");
2510Sstevel@tonic-gate 	iflag = Mflag = Lflag = lflag = bflag = aflag = xflag = cflag = 0;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "abcCd:hiLlm:MOp:s:ST:vVx")) != EOF) {
2540Sstevel@tonic-gate 		add_option(&specified_ops, c);
2550Sstevel@tonic-gate 		switch (c) {
2560Sstevel@tonic-gate 		case 'a':
2570Sstevel@tonic-gate 			aflag = 1;
2580Sstevel@tonic-gate 			operations++;
2590Sstevel@tonic-gate 			break;
2600Sstevel@tonic-gate 		case 'b':
2610Sstevel@tonic-gate 			bflag = 1;
2620Sstevel@tonic-gate 			operations++;
2630Sstevel@tonic-gate 			break;
2640Sstevel@tonic-gate 		case 'c':
2650Sstevel@tonic-gate 			cflag = 1;
2660Sstevel@tonic-gate 			operations++;
2670Sstevel@tonic-gate 			break;
2680Sstevel@tonic-gate 		case 'C':
269*7024Sec158148 			/*
270*7024Sec158148 			 * cdrw now attempts to use the stated medium capacity
271*7024Sec158148 			 * by default, so this option no longer has any effect.
272*7024Sec158148 			 * It remains in the interface for backwards
273*7024Sec158148 			 * compatibility only.
274*7024Sec158148 			 */
2750Sstevel@tonic-gate 			break;
2760Sstevel@tonic-gate 		case 'd':
2770Sstevel@tonic-gate 			tgtdev = optarg;
2780Sstevel@tonic-gate 			break;
2790Sstevel@tonic-gate 		case 'h':
2800Sstevel@tonic-gate 			print_usage(); /* will not return */
2810Sstevel@tonic-gate 			break;
2820Sstevel@tonic-gate 		case 'i':
2830Sstevel@tonic-gate 			iflag = 1;
2840Sstevel@tonic-gate 			operations++;
2850Sstevel@tonic-gate 			break;
2860Sstevel@tonic-gate 		case 'L':
2870Sstevel@tonic-gate 			Lflag = 1;
2880Sstevel@tonic-gate 			operations++;
2890Sstevel@tonic-gate 			break;
2900Sstevel@tonic-gate 		case 'l':
2910Sstevel@tonic-gate 			lflag = 1;
2920Sstevel@tonic-gate 			operations++;
2930Sstevel@tonic-gate 			break;
2940Sstevel@tonic-gate 		case 'm':
2950Sstevel@tonic-gate 			alt_tmp_dir = optarg;
2960Sstevel@tonic-gate 			break;
2970Sstevel@tonic-gate 		case 'M':
2980Sstevel@tonic-gate 			Mflag = 1;
2990Sstevel@tonic-gate 			operations++;
3000Sstevel@tonic-gate 			break;
3010Sstevel@tonic-gate 		case 'O':
3020Sstevel@tonic-gate 			keep_disc_open = 1;
3030Sstevel@tonic-gate 			break;
3040Sstevel@tonic-gate 		case 'p':
3050Sstevel@tonic-gate 			requested_speed = atoi(optarg);
3060Sstevel@tonic-gate 			break;
3070Sstevel@tonic-gate 		case 's':
3080Sstevel@tonic-gate 			copy_src = optarg;
3090Sstevel@tonic-gate 			break;
3100Sstevel@tonic-gate 		case 'S':
3110Sstevel@tonic-gate 			simulation++;
3120Sstevel@tonic-gate 			break;
3130Sstevel@tonic-gate 		case 'T':
3140Sstevel@tonic-gate 			audio_type = get_audio_type(optarg);
3150Sstevel@tonic-gate 			if (audio_type == -1) {
3160Sstevel@tonic-gate 				err_msg(gettext("Unknown audio type %s\n"),
3170Sstevel@tonic-gate 				    optarg);
3180Sstevel@tonic-gate 				exit(1);
3190Sstevel@tonic-gate 			}
3200Sstevel@tonic-gate 			break;
3210Sstevel@tonic-gate 		case 'v':
3220Sstevel@tonic-gate 			verbose++;
3230Sstevel@tonic-gate 			break;
3240Sstevel@tonic-gate 		case 'V':
3250Sstevel@tonic-gate 			/*
3260Sstevel@tonic-gate 			 * more verbose. this will print out debug comments
3270Sstevel@tonic-gate 			 */
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 			debug++;
3300Sstevel@tonic-gate 			break;
3310Sstevel@tonic-gate 		case 'x':
3320Sstevel@tonic-gate 			xflag++;
3330Sstevel@tonic-gate 			operations++;
3340Sstevel@tonic-gate 			break;
3350Sstevel@tonic-gate 		default:
3360Sstevel@tonic-gate 			print_usage();
3370Sstevel@tonic-gate 		}
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate 	if (operations == 0) {
3400Sstevel@tonic-gate 		err_msg(gettext("No operation specified.\n"));
3410Sstevel@tonic-gate 		exit(1);
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 	if (operations != 1) {
3440Sstevel@tonic-gate 		err_msg(gettext("More than one operation specified.\n"));
3450Sstevel@tonic-gate 		exit(1);
3460Sstevel@tonic-gate 	}
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	if (lflag) {
3490Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "lhvV");
3500Sstevel@tonic-gate 		list();
3510Sstevel@tonic-gate 	}
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	/*
3540Sstevel@tonic-gate 	 * we'll allow the user to specify the source device (-s) when
3550Sstevel@tonic-gate 	 *  extracting audio.
3560Sstevel@tonic-gate 	 */
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	if (xflag && copy_src)
3590Sstevel@tonic-gate 		tgtdev = copy_src;
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	/*
3620Sstevel@tonic-gate 	 * This will scan for all CD devices when xflag or Mflag
3630Sstevel@tonic-gate 	 * (extract audio, list toc) commands are used, providing
3640Sstevel@tonic-gate 	 * no CD-RW devices are found. Since these commands can
3650Sstevel@tonic-gate 	 * be used without a CD writer.
3660Sstevel@tonic-gate 	 */
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	if (xflag || Mflag) {
3690Sstevel@tonic-gate 		ret = setup_target(SCAN_ALL_CDS);
3700Sstevel@tonic-gate 	} else {
3710Sstevel@tonic-gate 		ret = setup_target(SCAN_WRITERS);
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	if (ret == 0) {
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 		if (tgtdev != NULL) {
3770Sstevel@tonic-gate 			err_msg(gettext(
3780Sstevel@tonic-gate 			    "Cannot find device %s.\n"), tgtdev);
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 		}
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 		if (vol_running) {
3830Sstevel@tonic-gate 			err_msg(gettext(
3840Sstevel@tonic-gate 			    "No CD writers found or no media in the drive.\n"));
3850Sstevel@tonic-gate 		} else {
3860Sstevel@tonic-gate 			if (cur_uid != 0) {
3870Sstevel@tonic-gate 				err_msg(gettext(
3880Sstevel@tonic-gate 				    "Volume manager is not running.\n"));
3890Sstevel@tonic-gate 				err_msg(gettext(
3900Sstevel@tonic-gate "Please start volume manager or run cdrw as root to access all devices.\n"));
3910Sstevel@tonic-gate 			} else {
3920Sstevel@tonic-gate 				err_msg(gettext(
3930Sstevel@tonic-gate 				    "No CD writers found.\n"));
3940Sstevel@tonic-gate 			}
3950Sstevel@tonic-gate 		}
3960Sstevel@tonic-gate 		exit(1);
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	} else if (ret != 1) {
3990Sstevel@tonic-gate 		err_msg(gettext("More than one CD device found.\n"));
4000Sstevel@tonic-gate 		err_msg(gettext("Specify one using -d option.\n"));
4010Sstevel@tonic-gate 		err_msg(gettext(
4020Sstevel@tonic-gate 		    "Or use -l option to list all the CD devices found\n"));
4030Sstevel@tonic-gate 		exit(1);
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 	(void) check_device(target, CHECK_TYPE_NOT_CDROM|EXIT_IF_CHECK_FAILED);
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	if (check_device(target, CHECK_NO_MEDIA) == 0) {
4080Sstevel@tonic-gate 		int retry;
4090Sstevel@tonic-gate 		for (retry = 0; retry < 5; retry++) {
4100Sstevel@tonic-gate 			if (check_device(target, CHECK_DEVICE_NOT_READY) == 0)
4110Sstevel@tonic-gate 				break;
4120Sstevel@tonic-gate 			(void) sleep(3);
4130Sstevel@tonic-gate 		}
4140Sstevel@tonic-gate 	}
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	if (aflag) {
4170Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "ahvSCOdpTV");
4180Sstevel@tonic-gate 		if (optind == argc) {
4190Sstevel@tonic-gate 			err_msg(gettext("No audio files specified.\n"));
4200Sstevel@tonic-gate 			exit(1);
4210Sstevel@tonic-gate 		}
4220Sstevel@tonic-gate 		write_audio(argv, optind, argc);
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 	if (Mflag) {
4250Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "MhvdV");
4260Sstevel@tonic-gate 		info();
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 	if (iflag) {
4290Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "ihvSCOdpV");
4300Sstevel@tonic-gate 		if (optind == (argc - 1)) {
4310Sstevel@tonic-gate 			image_file = argv[optind];
4320Sstevel@tonic-gate 			write_image();
4330Sstevel@tonic-gate 		}
4340Sstevel@tonic-gate 		if (optind == argc)
4350Sstevel@tonic-gate 			write_image();
4360Sstevel@tonic-gate 		err_msg(gettext("Command line parsing error.\n"));
4370Sstevel@tonic-gate 		err_msg(gettext("Only one image-file can be specified.\n"));
4380Sstevel@tonic-gate 		exit(1);
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 	if (bflag) {
4410Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "bhvdV");
4420Sstevel@tonic-gate 		if (optind != (argc - 1)) {
4430Sstevel@tonic-gate 			err_msg(gettext("Command line parsing error.\n"));
4440Sstevel@tonic-gate 			print_usage();
4450Sstevel@tonic-gate 		}
4460Sstevel@tonic-gate 		blanking_type = argv[argc - 1];
4470Sstevel@tonic-gate 		blank();
4480Sstevel@tonic-gate 	}
4490Sstevel@tonic-gate 	if (xflag) {
4500Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "xhpvdsTV");
4510Sstevel@tonic-gate 		if (optind != (argc - 2)) {
4520Sstevel@tonic-gate 			err_msg(gettext("Command line parsing error.\n"));
4530Sstevel@tonic-gate 			print_usage();
4540Sstevel@tonic-gate 		}
4550Sstevel@tonic-gate 		extract_track_no = atoi(argv[argc - 2]);
4560Sstevel@tonic-gate 		extract_file = argv[argc - 1];
4570Sstevel@tonic-gate 		extract_audio();
4580Sstevel@tonic-gate 	}
4590Sstevel@tonic-gate 	if (cflag) {
4600Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "chvSCdpmsV");
4610Sstevel@tonic-gate 		copy_cd();
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	/*
4650Sstevel@tonic-gate 	 * Open a closed disk, we do this by erasing the track tail
4660Sstevel@tonic-gate 	 * and then re-finalizing with an open leadout.
4670Sstevel@tonic-gate 	 */
4680Sstevel@tonic-gate 	if (Lflag) {
4690Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "LvdV");
4700Sstevel@tonic-gate 		(void) check_device(target, CHECK_NO_MEDIA |
4710Sstevel@tonic-gate 		    CHECK_DEVICE_NOT_READY | EXIT_IF_CHECK_FAILED);
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 		/* no need to erase blank media */
4740Sstevel@tonic-gate 		if (!check_device(target, CHECK_MEDIA_IS_NOT_BLANK))
4750Sstevel@tonic-gate 			exit(0);
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 		blanking_type = "leadout";
4780Sstevel@tonic-gate 		blank();
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 		write_init(TRACK_MODE_DATA);
4810Sstevel@tonic-gate 		(void) close_track(target->d_fd, 0, 1, 1);
4820Sstevel@tonic-gate 		(void) finalize(target);
4830Sstevel@tonic-gate 		(void) printf(gettext("done.\n"));
4840Sstevel@tonic-gate 		exit(0);
4850Sstevel@tonic-gate 	}
486370Snakanon 	return (0);
4870Sstevel@tonic-gate }
488