xref: /titanic_51/usr/src/cmd/audio/audioconvert/main.cc (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1993-2001 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
32*7c478bd9Sstevel@tonic-gate #include <string.h>
33*7c478bd9Sstevel@tonic-gate #include <unistd.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include <convert.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
42*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
43*7c478bd9Sstevel@tonic-gate #endif
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate const char	*opt_string = "pf:o:i:FTD?";
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate char		*Stdin;
48*7c478bd9Sstevel@tonic-gate char		*Stdout;
49*7c478bd9Sstevel@tonic-gate char		*Suffix = (char *)".AUDCVTMP";
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate char		*progname; // program name
52*7c478bd9Sstevel@tonic-gate char		*fake_argv[] = {(char *)"-", NULL}; // stdin with no args
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate extern char	*optarg;
55*7c478bd9Sstevel@tonic-gate extern int	optind;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate int		Statistics = 0;
58*7c478bd9Sstevel@tonic-gate int		Debug = 0;
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate void		init_header(AudioHdr&);
61*7c478bd9Sstevel@tonic-gate void		usage();
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate main(
64*7c478bd9Sstevel@tonic-gate 	int		argc,
65*7c478bd9Sstevel@tonic-gate 	char		*argv[])
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate 	AudioUnixfile*	ifp = NULL;	// input & output audio objects
68*7c478bd9Sstevel@tonic-gate 	AudioUnixfile*	ofp = NULL;
69*7c478bd9Sstevel@tonic-gate 	AudioHdr	ihdr;		// input/output headers
70*7c478bd9Sstevel@tonic-gate 	AudioHdr	ohdr;
71*7c478bd9Sstevel@tonic-gate 	char		*infile = NULL; // input/output file names
72*7c478bd9Sstevel@tonic-gate 	char		*outfile = NULL;
73*7c478bd9Sstevel@tonic-gate 	char		*realfile = NULL;
74*7c478bd9Sstevel@tonic-gate 	char		*out_fmt = NULL;	// output fmt string
75*7c478bd9Sstevel@tonic-gate 	AudioError	err;		// for error msgs
76*7c478bd9Sstevel@tonic-gate 	int		c;		// for getopt
77*7c478bd9Sstevel@tonic-gate 	int		pflag = 0; 	// in place flag
78*7c478bd9Sstevel@tonic-gate 	int		fflag = 0;	// ignore header (force conversion)
79*7c478bd9Sstevel@tonic-gate 	int		stdin_seen = 0;	// already read stdin
80*7c478bd9Sstevel@tonic-gate 	int		israw = 0;	// once we've seen -i, it's raw data
81*7c478bd9Sstevel@tonic-gate 	format_type	ofmt = F_SUN;	// output format type
82*7c478bd9Sstevel@tonic-gate 	format_type	ifmt = F_SUN;	// expected input format type
83*7c478bd9Sstevel@tonic-gate 	format_type	fmt = F_SUN;	// actual input format type
84*7c478bd9Sstevel@tonic-gate 	off_t		o_offset = 0;	// output offset (ignored)
85*7c478bd9Sstevel@tonic-gate 	off_t		i_offset = 0;	// input offset
86*7c478bd9Sstevel@tonic-gate 	int		i;
87*7c478bd9Sstevel@tonic-gate 	struct stat	st;
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	setlocale(LC_ALL, "");
90*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	// basename of program
93*7c478bd9Sstevel@tonic-gate 	if (progname = strrchr(argv[0], '/')) {
94*7c478bd9Sstevel@tonic-gate 		progname++;
95*7c478bd9Sstevel@tonic-gate 	} else {
96*7c478bd9Sstevel@tonic-gate 		progname = argv[0];
97*7c478bd9Sstevel@tonic-gate 	}
98*7c478bd9Sstevel@tonic-gate 	Stdin = MGET("(stdin)");
99*7c478bd9Sstevel@tonic-gate 	Stdout = MGET("(stdout)");
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	// init the input & output headers
102*7c478bd9Sstevel@tonic-gate 	init_header(ihdr);
103*7c478bd9Sstevel@tonic-gate 	init_header(ohdr);
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	// some conversions depend on invocation name. we'll create
106*7c478bd9Sstevel@tonic-gate 	// default input/output formats based on argv[0] that
107*7c478bd9Sstevel@tonic-gate 	// can be overridden by -o or -i options.
108*7c478bd9Sstevel@tonic-gate 	if (strcmp(progname, "ulaw2pcm") == 0) {
109*7c478bd9Sstevel@tonic-gate 		(void) parse_format((char *)"ulaw", ihdr, ifmt, i_offset);
110*7c478bd9Sstevel@tonic-gate 		(void) parse_format((char *)"pcm", ohdr, ofmt, o_offset);
111*7c478bd9Sstevel@tonic-gate 	} else if (strcmp(progname, "pcm2ulaw") == 0) {
112*7c478bd9Sstevel@tonic-gate 		(void) parse_format((char *)"pcm", ihdr, ifmt, i_offset);
113*7c478bd9Sstevel@tonic-gate 		(void) parse_format((char *)"ulaw", ohdr, ofmt, o_offset);
114*7c478bd9Sstevel@tonic-gate 	} else if (strcmp(progname, "adpcm_enc") == 0) {
115*7c478bd9Sstevel@tonic-gate 		(void) parse_format((char *)"ulaw", ihdr, ifmt, i_offset);
116*7c478bd9Sstevel@tonic-gate 		(void) parse_format((char *)"g721", ohdr, ofmt, o_offset);
117*7c478bd9Sstevel@tonic-gate 	} else if (strcmp(progname, "adpcm_dec") == 0) {
118*7c478bd9Sstevel@tonic-gate 		(void) parse_format((char *)"g721", ihdr, ifmt, i_offset);
119*7c478bd9Sstevel@tonic-gate 		(void) parse_format((char *)"ulaw", ohdr, ofmt, o_offset);
120*7c478bd9Sstevel@tonic-gate 	} else if (strcmp(progname, "raw2audio") == 0) {
121*7c478bd9Sstevel@tonic-gate 		(void) parse_format((char *)"ulaw", ihdr, ifmt, i_offset);
122*7c478bd9Sstevel@tonic-gate 		(void) parse_format((char *)"ulaw", ohdr, ofmt, o_offset);
123*7c478bd9Sstevel@tonic-gate 		israw++;
124*7c478bd9Sstevel@tonic-gate 		pflag++;
125*7c478bd9Sstevel@tonic-gate 	} else if (argc <= 1) {
126*7c478bd9Sstevel@tonic-gate 		// audioconvert with no arguments
127*7c478bd9Sstevel@tonic-gate 		usage();
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	// now parse the rest of the arg's
131*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, opt_string)) != -1) {
132*7c478bd9Sstevel@tonic-gate 		switch (c) {
133*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
134*7c478bd9Sstevel@tonic-gate 		case 'D':
135*7c478bd9Sstevel@tonic-gate 			// enable debug messages
136*7c478bd9Sstevel@tonic-gate 			Debug++;
137*7c478bd9Sstevel@tonic-gate 			break;
138*7c478bd9Sstevel@tonic-gate #endif
139*7c478bd9Sstevel@tonic-gate 		case 'p':
140*7c478bd9Sstevel@tonic-gate 			// convert files in place
141*7c478bd9Sstevel@tonic-gate 			if (outfile != NULL) {
142*7c478bd9Sstevel@tonic-gate 				Err(MGET("can't use -p with -o\n"));
143*7c478bd9Sstevel@tonic-gate 				exit(1);
144*7c478bd9Sstevel@tonic-gate 			}
145*7c478bd9Sstevel@tonic-gate 			pflag++;
146*7c478bd9Sstevel@tonic-gate 			break;
147*7c478bd9Sstevel@tonic-gate 		case 'F':
148*7c478bd9Sstevel@tonic-gate 			// force treatment of audio files as raw files
149*7c478bd9Sstevel@tonic-gate 			// (ignore filehdr).
150*7c478bd9Sstevel@tonic-gate 			fflag++;
151*7c478bd9Sstevel@tonic-gate 			break;
152*7c478bd9Sstevel@tonic-gate 		case 'f':
153*7c478bd9Sstevel@tonic-gate 			// save format string to parse later, but verify now
154*7c478bd9Sstevel@tonic-gate 			out_fmt = optarg;
155*7c478bd9Sstevel@tonic-gate 			if (parse_format(out_fmt, ohdr, ofmt, o_offset) == -1)
156*7c478bd9Sstevel@tonic-gate 				exit(1);
157*7c478bd9Sstevel@tonic-gate 			if (o_offset != 0) {
158*7c478bd9Sstevel@tonic-gate 				Err(MGET("can't specify an offset with -f\n"));
159*7c478bd9Sstevel@tonic-gate 				exit(1);
160*7c478bd9Sstevel@tonic-gate 			}
161*7c478bd9Sstevel@tonic-gate 			break;
162*7c478bd9Sstevel@tonic-gate 		case 'o':
163*7c478bd9Sstevel@tonic-gate 			if (pflag) {
164*7c478bd9Sstevel@tonic-gate 				Err(MGET("can't use -o with -p\n"));
165*7c478bd9Sstevel@tonic-gate 				exit(1);
166*7c478bd9Sstevel@tonic-gate 			}
167*7c478bd9Sstevel@tonic-gate 			outfile = optarg;
168*7c478bd9Sstevel@tonic-gate 			break;
169*7c478bd9Sstevel@tonic-gate 		case 'i':
170*7c478bd9Sstevel@tonic-gate 			// if bogus input header, exit ...
171*7c478bd9Sstevel@tonic-gate 			if (parse_format(optarg, ihdr, ifmt, i_offset) == -1) {
172*7c478bd9Sstevel@tonic-gate 				exit(1);
173*7c478bd9Sstevel@tonic-gate 			}
174*7c478bd9Sstevel@tonic-gate 			israw++;
175*7c478bd9Sstevel@tonic-gate 			break;
176*7c478bd9Sstevel@tonic-gate 		default:
177*7c478bd9Sstevel@tonic-gate 		case '?':
178*7c478bd9Sstevel@tonic-gate 			usage();
179*7c478bd9Sstevel@tonic-gate 		}
180*7c478bd9Sstevel@tonic-gate 	}
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 	// XXX - should check argument consistency here....
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	// If no args left, we're taking input from stdin.
185*7c478bd9Sstevel@tonic-gate 	// In this case, make argv point to a fake argv with "-" as a file
186*7c478bd9Sstevel@tonic-gate 	// name, and set optind and argc apropriately so we'll go through
187*7c478bd9Sstevel@tonic-gate 	// the loop below once.
188*7c478bd9Sstevel@tonic-gate 	if (optind >= argc) {
189*7c478bd9Sstevel@tonic-gate 		argv = fake_argv;
190*7c478bd9Sstevel@tonic-gate 		argc = 1;
191*7c478bd9Sstevel@tonic-gate 		optind = 0;
192*7c478bd9Sstevel@tonic-gate 		/*
193*7c478bd9Sstevel@tonic-gate 		 * XXX - we turn off pflag if stdin is the only input file.
194*7c478bd9Sstevel@tonic-gate 		 * this is kind of a hack. if invoked as raw2audio, pflag
195*7c478bd9Sstevel@tonic-gate 		 * it turned on. if no files are given, we want to turn
196*7c478bd9Sstevel@tonic-gate 		 * it off, otherwise we'll complain about using -p with
197*7c478bd9Sstevel@tonic-gate 		 * stdin, which won't make sense if invoked as raw2audio.
198*7c478bd9Sstevel@tonic-gate 		 * instead, just silently ignore. the message is still given
199*7c478bd9Sstevel@tonic-gate 		 * and stdin is ignored if it's specified as one of several
200*7c478bd9Sstevel@tonic-gate 		 * input files.
201*7c478bd9Sstevel@tonic-gate 		 */
202*7c478bd9Sstevel@tonic-gate 		pflag = 0;
203*7c478bd9Sstevel@tonic-gate 	}
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	// From this point on we're looking at file names or -i args
206*7c478bd9Sstevel@tonic-gate 	// for input format specs.
207*7c478bd9Sstevel@tonic-gate 	for (; optind < argc; optind++) {
208*7c478bd9Sstevel@tonic-gate 		// new input format spec.
209*7c478bd9Sstevel@tonic-gate 		if (strcmp(argv[optind], "-i") == 0) {
210*7c478bd9Sstevel@tonic-gate 			init_header(ihdr);
211*7c478bd9Sstevel@tonic-gate 			i_offset = 0;
212*7c478bd9Sstevel@tonic-gate 			ifmt = F_SUN;
213*7c478bd9Sstevel@tonic-gate 			// if bogus input header, exit ...
214*7c478bd9Sstevel@tonic-gate 			if (parse_format(argv[++optind], ihdr, ifmt, i_offset)
215*7c478bd9Sstevel@tonic-gate 			    == -1) {
216*7c478bd9Sstevel@tonic-gate 				exit(1);
217*7c478bd9Sstevel@tonic-gate 			}
218*7c478bd9Sstevel@tonic-gate 			israw++;
219*7c478bd9Sstevel@tonic-gate 		} else if (strcmp(argv[optind], "-") == 0) {
220*7c478bd9Sstevel@tonic-gate 			// ignore stdin argument if in place
221*7c478bd9Sstevel@tonic-gate 			if (pflag) {
222*7c478bd9Sstevel@tonic-gate 				Err(MGET("can't use %s with -p flag\n"),
223*7c478bd9Sstevel@tonic-gate 				    Stdin);
224*7c478bd9Sstevel@tonic-gate 				continue;
225*7c478bd9Sstevel@tonic-gate 			}
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 			if (stdin_seen) {
228*7c478bd9Sstevel@tonic-gate 				Err(MGET("already used stdin for input\n"));
229*7c478bd9Sstevel@tonic-gate 				continue;
230*7c478bd9Sstevel@tonic-gate 			} else {
231*7c478bd9Sstevel@tonic-gate 				stdin_seen++;
232*7c478bd9Sstevel@tonic-gate 			}
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 			infile = Stdin;
235*7c478bd9Sstevel@tonic-gate 		} else {
236*7c478bd9Sstevel@tonic-gate 			infile = argv[optind];
237*7c478bd9Sstevel@tonic-gate 		}
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 		// if no audio object returned, just continue to the next
240*7c478bd9Sstevel@tonic-gate 		// file. if a fatal error occurs, open_input_file()
241*7c478bd9Sstevel@tonic-gate 		// will exit the program.
242*7c478bd9Sstevel@tonic-gate 		ifp =
243*7c478bd9Sstevel@tonic-gate 		    open_input_file(infile, ihdr, israw, fflag, i_offset, fmt);
244*7c478bd9Sstevel@tonic-gate 		if (!ifp) {
245*7c478bd9Sstevel@tonic-gate 			continue;
246*7c478bd9Sstevel@tonic-gate 		}
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 		if ((err = ifp->Open()) != AUDIO_SUCCESS) {
249*7c478bd9Sstevel@tonic-gate 			Err(MGET("open error on input file %s - %s\n"),
250*7c478bd9Sstevel@tonic-gate 			    infile, err.msg());
251*7c478bd9Sstevel@tonic-gate 			exit(1);
252*7c478bd9Sstevel@tonic-gate 		}
253*7c478bd9Sstevel@tonic-gate 		ifp->Reference();
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 		// create the output file if not created yet, or if
256*7c478bd9Sstevel@tonic-gate 		// converting in place. ofp will be NULL only the first
257*7c478bd9Sstevel@tonic-gate 		// time through. use the header of the first input file
258*7c478bd9Sstevel@tonic-gate 		// to base the output format on - then create the output
259*7c478bd9Sstevel@tonic-gate 		// header w/the output format spec.
260*7c478bd9Sstevel@tonic-gate 		if ((ofp == NULL) && !pflag) {
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 			ohdr = ifp->GetHeader();
263*7c478bd9Sstevel@tonic-gate 			ohdr = ifp->GetHeader();
264*7c478bd9Sstevel@tonic-gate 			ofmt = ifmt;
265*7c478bd9Sstevel@tonic-gate 			// just use input hdr if no output hdr spec
266*7c478bd9Sstevel@tonic-gate 			if (out_fmt) {
267*7c478bd9Sstevel@tonic-gate 				if (parse_format(out_fmt, ohdr, ofmt, o_offset)
268*7c478bd9Sstevel@tonic-gate 				    == -1) {
269*7c478bd9Sstevel@tonic-gate 					exit(1);
270*7c478bd9Sstevel@tonic-gate 				}
271*7c478bd9Sstevel@tonic-gate 			}
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 			// need to check before output is opened ...
274*7c478bd9Sstevel@tonic-gate 			if (verify_conversion(ifp->GetHeader(), ohdr) == -1) {
275*7c478bd9Sstevel@tonic-gate 				// XXX - bomb out or skip?
276*7c478bd9Sstevel@tonic-gate 				exit(3);
277*7c478bd9Sstevel@tonic-gate 			}
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 			// Create the file and set the info string.
280*7c478bd9Sstevel@tonic-gate 			char		*infoString;
281*7c478bd9Sstevel@tonic-gate 			int  		infoStringLen;
282*7c478bd9Sstevel@tonic-gate 			infoString = ifp->GetInfostring(infoStringLen);
283*7c478bd9Sstevel@tonic-gate 			ofp = create_output_file(outfile, ohdr, ofmt,
284*7c478bd9Sstevel@tonic-gate 						    infoString);
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 		} else if (pflag) {
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 			// create new output header based on each input file
289*7c478bd9Sstevel@tonic-gate 			ohdr = ifp->GetHeader();
290*7c478bd9Sstevel@tonic-gate 			ofmt = ifmt;
291*7c478bd9Sstevel@tonic-gate 			// just use input hdr if no output hdr spec
292*7c478bd9Sstevel@tonic-gate 			if (out_fmt) {
293*7c478bd9Sstevel@tonic-gate 				if (parse_format(out_fmt, ohdr, ofmt, o_offset)
294*7c478bd9Sstevel@tonic-gate 				    == -1) {
295*7c478bd9Sstevel@tonic-gate 					exit(1);
296*7c478bd9Sstevel@tonic-gate 				}
297*7c478bd9Sstevel@tonic-gate 			}
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate 			// get the *real* path of the infile (follow sym-links),
300*7c478bd9Sstevel@tonic-gate 			// and the stat info.
301*7c478bd9Sstevel@tonic-gate 			realfile = infile;
302*7c478bd9Sstevel@tonic-gate 			get_realfile(realfile, &st);
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate 			// if the file is read-only, give up
305*7c478bd9Sstevel@tonic-gate 			if (access(realfile, W_OK)) {
306*7c478bd9Sstevel@tonic-gate 				// XXX - do we really want to exit?
307*7c478bd9Sstevel@tonic-gate 				perror(infile);
308*7c478bd9Sstevel@tonic-gate 				Err(MGET("cannot rewrite in place\n"));
309*7c478bd9Sstevel@tonic-gate 				exit(1);
310*7c478bd9Sstevel@tonic-gate 			}
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 			// this is now the output file.
313*7c478bd9Sstevel@tonic-gate 			i = strlen(realfile) + strlen(Suffix) + 1;
314*7c478bd9Sstevel@tonic-gate 			outfile = (char *)malloc((unsigned)i);
315*7c478bd9Sstevel@tonic-gate 			if (outfile == NULL) {
316*7c478bd9Sstevel@tonic-gate 				Err(MGET("out of memory\n"));
317*7c478bd9Sstevel@tonic-gate 				exit(1);
318*7c478bd9Sstevel@tonic-gate 			}
319*7c478bd9Sstevel@tonic-gate 			(void) sprintf(outfile, "%s%s", realfile, Suffix);
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 			// outfile will get re-assigned to a tmp file
322*7c478bd9Sstevel@tonic-gate 			if (verify_conversion(ifp->GetHeader(), ohdr) == -1) {
323*7c478bd9Sstevel@tonic-gate 				// XXX - bomb out or skip?
324*7c478bd9Sstevel@tonic-gate 				exit(3);
325*7c478bd9Sstevel@tonic-gate 			}
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 			// If no conversion, just skip the file
328*7c478bd9Sstevel@tonic-gate 			if (noop_conversion(ifp->GetHeader(), ohdr,
329*7c478bd9Sstevel@tonic-gate 			    fmt, ofmt, i_offset, o_offset)) {
330*7c478bd9Sstevel@tonic-gate 				if (Debug)
331*7c478bd9Sstevel@tonic-gate 				    Err(MGET(
332*7c478bd9Sstevel@tonic-gate 					"%s: no-op conversion...skipping\n"),
333*7c478bd9Sstevel@tonic-gate 					infile);
334*7c478bd9Sstevel@tonic-gate 				continue;
335*7c478bd9Sstevel@tonic-gate 			}
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate 			// Get the input info string.
338*7c478bd9Sstevel@tonic-gate 			char		*infoString;
339*7c478bd9Sstevel@tonic-gate 			int  		infoStringLen;
340*7c478bd9Sstevel@tonic-gate 			infoString = ifp->GetInfostring(infoStringLen);
341*7c478bd9Sstevel@tonic-gate 			ofp = create_output_file(outfile, ohdr, ofmt,
342*7c478bd9Sstevel@tonic-gate 						    infoString);
343*7c478bd9Sstevel@tonic-gate 		}
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate 		// verify that it's a valid conversion by looking at the
346*7c478bd9Sstevel@tonic-gate 		// file headers. (this will be called twice for the first
347*7c478bd9Sstevel@tonic-gate 		// file if *not* converting in place. that's ok....
348*7c478bd9Sstevel@tonic-gate 		if (!pflag && (verify_conversion(ifp->GetHeader(), ohdr)
349*7c478bd9Sstevel@tonic-gate 		    == -1)) {
350*7c478bd9Sstevel@tonic-gate 			// XXX - bomb out or skip file if invalid conversion?
351*7c478bd9Sstevel@tonic-gate 			exit(3);
352*7c478bd9Sstevel@tonic-gate 		}
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 		// do the conversion, if error, bomb out
355*7c478bd9Sstevel@tonic-gate 		if (do_convert(ifp, ofp) == -1) {
356*7c478bd9Sstevel@tonic-gate 			exit(4);
357*7c478bd9Sstevel@tonic-gate 		}
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 		ifp->Close();
360*7c478bd9Sstevel@tonic-gate 		ifp->Dereference();
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 		// if in place, finish up by renaming the outfile to
363*7c478bd9Sstevel@tonic-gate 		// back to the infile.
364*7c478bd9Sstevel@tonic-gate 		if (pflag) {
365*7c478bd9Sstevel@tonic-gate 			delete(ofp);	// will close and deref, etc.
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 			if (rename(outfile, realfile) < 0) {
368*7c478bd9Sstevel@tonic-gate 				perror(outfile);
369*7c478bd9Sstevel@tonic-gate 				Err(MGET("error renaming %s to %s"),
370*7c478bd9Sstevel@tonic-gate 				    outfile, realfile);
371*7c478bd9Sstevel@tonic-gate 				exit(1);
372*7c478bd9Sstevel@tonic-gate 			}
373*7c478bd9Sstevel@tonic-gate 			/* Set the permissions to match the original */
374*7c478bd9Sstevel@tonic-gate 			if (chmod(realfile, (int)st.st_mode) < 0) {
375*7c478bd9Sstevel@tonic-gate 				Err(MGET("WARNING: could not reset mode of"));
376*7c478bd9Sstevel@tonic-gate 				perror(realfile);
377*7c478bd9Sstevel@tonic-gate 			}
378*7c478bd9Sstevel@tonic-gate 		}
379*7c478bd9Sstevel@tonic-gate 	}
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 	if (!pflag) {
382*7c478bd9Sstevel@tonic-gate 		delete(ofp);		// close output file
383*7c478bd9Sstevel@tonic-gate 	}
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 	exit(0);		// outta here!
386*7c478bd9Sstevel@tonic-gate }
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate // initialize audio hdr to default val's
390*7c478bd9Sstevel@tonic-gate void
391*7c478bd9Sstevel@tonic-gate init_header(
392*7c478bd9Sstevel@tonic-gate 	AudioHdr&	hdr)
393*7c478bd9Sstevel@tonic-gate {
394*7c478bd9Sstevel@tonic-gate 	hdr.encoding = NONE;
395*7c478bd9Sstevel@tonic-gate 	hdr.sample_rate = 0;
396*7c478bd9Sstevel@tonic-gate 	hdr.samples_per_unit = 0;
397*7c478bd9Sstevel@tonic-gate 	hdr.bytes_per_unit = 0;
398*7c478bd9Sstevel@tonic-gate 	hdr.channels = 0;
399*7c478bd9Sstevel@tonic-gate }
400*7c478bd9Sstevel@tonic-gate 
401*7c478bd9Sstevel@tonic-gate extern "C" { void _doprnt(char *, ...); }
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate // report a fatal error and exit
404*7c478bd9Sstevel@tonic-gate void
405*7c478bd9Sstevel@tonic-gate Err(char *format, ...)
406*7c478bd9Sstevel@tonic-gate {
407*7c478bd9Sstevel@tonic-gate 	va_list ap;
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 	va_start(ap, format);
410*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s: ", progname);
411*7c478bd9Sstevel@tonic-gate 	_doprnt(format, ap, stderr);
412*7c478bd9Sstevel@tonic-gate 	fflush(stderr);
413*7c478bd9Sstevel@tonic-gate 	va_end(ap);
414*7c478bd9Sstevel@tonic-gate }
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate void
417*7c478bd9Sstevel@tonic-gate usage()
418*7c478bd9Sstevel@tonic-gate {
419*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, MGET(
420*7c478bd9Sstevel@tonic-gate 	    "Convert between audio file formats and data encodings -- usage:\n"
421*7c478bd9Sstevel@tonic-gate 	    "\t%s [-pF] [-f outfmt] [-o outfile] [[-i infmt] [file ...]] ...\n"
422*7c478bd9Sstevel@tonic-gate 	    "where:\n"
423*7c478bd9Sstevel@tonic-gate 	    "\t-p\tConvert files in place\n"
424*7c478bd9Sstevel@tonic-gate 	    "\t-F\tForce interpretation of -i (ignore existing file hdr)\n"
425*7c478bd9Sstevel@tonic-gate 	    "\t-f\tOutput format description\n"
426*7c478bd9Sstevel@tonic-gate 	    "\t-o\tOutput file (default: stdout)\n"
427*7c478bd9Sstevel@tonic-gate 	    "\t-i\tInput format description\n"
428*7c478bd9Sstevel@tonic-gate 	    "\tfile\tList of files to convert (default: stdin)\n\n"
429*7c478bd9Sstevel@tonic-gate 	    "Format Description:\n"
430*7c478bd9Sstevel@tonic-gate 	    "\tkeyword=value[,keyword=value...]\n"
431*7c478bd9Sstevel@tonic-gate 	    "where:\n"
432*7c478bd9Sstevel@tonic-gate 	    "\tKeywords:\tValues:\n"
433*7c478bd9Sstevel@tonic-gate 	    "\trate\t\tSample Rate in samples/second\n"
434*7c478bd9Sstevel@tonic-gate 	    "\tchannels\tNumber of interleaved channels\n"
435*7c478bd9Sstevel@tonic-gate 	    "\tencoding\tAudio encoding. One of:\n"
436*7c478bd9Sstevel@tonic-gate 	    "\t\t\t    ulaw, alaw, g721, g723,\n"
437*7c478bd9Sstevel@tonic-gate 	    "\t\t\t    linear8, linear16, linear32\n"
438*7c478bd9Sstevel@tonic-gate 	    "\t\t\t    pcm   (same as linear16)\n"
439*7c478bd9Sstevel@tonic-gate 	    "\t\t\t    voice (ulaw,mono,rate=8k)\n"
440*7c478bd9Sstevel@tonic-gate 	    "\t\t\t    cd    (linear16,stereo,rate=44.1k)\n"
441*7c478bd9Sstevel@tonic-gate 	    "\t\t\t    dat   (linear16,stereo,rate=48k)\n"
442*7c478bd9Sstevel@tonic-gate 	    "\tformat\t\tFile format. One of:\n"
443*7c478bd9Sstevel@tonic-gate 	    "\t\t\t    sun, raw (no format)\n"
444*7c478bd9Sstevel@tonic-gate 	    "\toffset\t\tByte offset (raw input only)\n"),
445*7c478bd9Sstevel@tonic-gate 	    progname);
446*7c478bd9Sstevel@tonic-gate 	exit(1);
447*7c478bd9Sstevel@tonic-gate }
448