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