17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright (c) 1993-2001 by Sun Microsystems, Inc.
247c478bdstevel@tonic-gate * All rights reserved.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate#include <stdlib.h>
287c478bdstevel@tonic-gate#include <stdio.h>
297c478bdstevel@tonic-gate#include <stdarg.h>
307c478bdstevel@tonic-gate#include <string.h>
317c478bdstevel@tonic-gate#include <unistd.h>
327c478bdstevel@tonic-gate#include <sys/types.h>
337c478bdstevel@tonic-gate#include <sys/stat.h>
347c478bdstevel@tonic-gate#include <sys/file.h>
357c478bdstevel@tonic-gate#include <sys/param.h>
367c478bdstevel@tonic-gate
377c478bdstevel@tonic-gate#include <convert.h>
387c478bdstevel@tonic-gate
397c478bdstevel@tonic-gate#if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
407c478bdstevel@tonic-gate#define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
417c478bdstevel@tonic-gate#endif
427c478bdstevel@tonic-gate
437c478bdstevel@tonic-gateconst char	*opt_string = "pf:o:i:FTD?";
447c478bdstevel@tonic-gate
457c478bdstevel@tonic-gatechar		*Stdin;
467c478bdstevel@tonic-gatechar		*Stdout;
477c478bdstevel@tonic-gatechar		*Suffix = (char *)".AUDCVTMP";
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gatechar		*progname; // program name
507c478bdstevel@tonic-gatechar		*fake_argv[] = {(char *)"-", NULL}; // stdin with no args
517c478bdstevel@tonic-gate
527c478bdstevel@tonic-gateextern char	*optarg;
537c478bdstevel@tonic-gateextern int	optind;
547c478bdstevel@tonic-gate
557c478bdstevel@tonic-gateint		Statistics = 0;
567c478bdstevel@tonic-gateint		Debug = 0;
577c478bdstevel@tonic-gate
587c478bdstevel@tonic-gatevoid		init_header(AudioHdr&);
597c478bdstevel@tonic-gatevoid		usage();
607c478bdstevel@tonic-gate
612325c4fToomas Soomeint
622325c4fToomas Soomemain(int argc, char *argv[])
637c478bdstevel@tonic-gate{
647c478bdstevel@tonic-gate	AudioUnixfile*	ifp = NULL;	// input & output audio objects
657c478bdstevel@tonic-gate	AudioUnixfile*	ofp = NULL;
667c478bdstevel@tonic-gate	AudioHdr	ihdr;		// input/output headers
677c478bdstevel@tonic-gate	AudioHdr	ohdr;
687c478bdstevel@tonic-gate	char		*infile = NULL; // input/output file names
697c478bdstevel@tonic-gate	char		*outfile = NULL;
707c478bdstevel@tonic-gate	char		*realfile = NULL;
717c478bdstevel@tonic-gate	char		*out_fmt = NULL;	// output fmt string
727c478bdstevel@tonic-gate	AudioError	err;		// for error msgs
737c478bdstevel@tonic-gate	int		c;		// for getopt
742325c4fToomas Soome	int		pflag = 0;	// in place flag
757c478bdstevel@tonic-gate	int		fflag = 0;	// ignore header (force conversion)
767c478bdstevel@tonic-gate	int		stdin_seen = 0;	// already read stdin
777c478bdstevel@tonic-gate	int		israw = 0;	// once we've seen -i, it's raw data
787c478bdstevel@tonic-gate	format_type	ofmt = F_SUN;	// output format type
797c478bdstevel@tonic-gate	format_type	ifmt = F_SUN;	// expected input format type
807c478bdstevel@tonic-gate	format_type	fmt = F_SUN;	// actual input format type
817c478bdstevel@tonic-gate	off_t		o_offset = 0;	// output offset (ignored)
827c478bdstevel@tonic-gate	off_t		i_offset = 0;	// input offset
837c478bdstevel@tonic-gate	int		i;
847c478bdstevel@tonic-gate	struct stat	st;
857c478bdstevel@tonic-gate
867c478bdstevel@tonic-gate	setlocale(LC_ALL, "");
877c478bdstevel@tonic-gate	(void) textdomain(TEXT_DOMAIN);
887c478bdstevel@tonic-gate
897c478bdstevel@tonic-gate	// basename of program
907c478bdstevel@tonic-gate	if (progname = strrchr(argv[0], '/')) {
917c478bdstevel@tonic-gate		progname++;
927c478bdstevel@tonic-gate	} else {
937c478bdstevel@tonic-gate		progname = argv[0];
947c478bdstevel@tonic-gate	}
957c478bdstevel@tonic-gate	Stdin = MGET("(stdin)");
967c478bdstevel@tonic-gate	Stdout = MGET("(stdout)");
977c478bdstevel@tonic-gate
987c478bdstevel@tonic-gate	// init the input & output headers
997c478bdstevel@tonic-gate	init_header(ihdr);
1007c478bdstevel@tonic-gate	init_header(ohdr);
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gate	// some conversions depend on invocation name. we'll create
1037c478bdstevel@tonic-gate	// default input/output formats based on argv[0] that
1047c478bdstevel@tonic-gate	// can be overridden by -o or -i options.
1057c478bdstevel@tonic-gate	if (strcmp(progname, "ulaw2pcm") == 0) {
1067c478bdstevel@tonic-gate		(void) parse_format((char *)"ulaw", ihdr, ifmt, i_offset);
1077c478bdstevel@tonic-gate		(void) parse_format((char *)"pcm", ohdr, ofmt, o_offset);
1087c478bdstevel@tonic-gate	} else if (strcmp(progname, "pcm2ulaw") == 0) {
1097c478bdstevel@tonic-gate		(void) parse_format((char *)"pcm", ihdr, ifmt, i_offset);
1107c478bdstevel@tonic-gate		(void) parse_format((char *)"ulaw", ohdr, ofmt, o_offset);
1117c478bdstevel@tonic-gate	} else if (strcmp(progname, "adpcm_enc") == 0) {
1127c478bdstevel@tonic-gate		(void) parse_format((char *)"ulaw", ihdr, ifmt, i_offset);
1137c478bdstevel@tonic-gate		(void) parse_format((char *)"g721", ohdr, ofmt, o_offset);
1147c478bdstevel@tonic-gate	} else if (strcmp(progname, "adpcm_dec") == 0) {
1157c478bdstevel@tonic-gate		(void) parse_format((char *)"g721", ihdr, ifmt, i_offset);
1167c478bdstevel@tonic-gate		(void) parse_format((char *)"ulaw", ohdr, ofmt, o_offset);
1177c478bdstevel@tonic-gate	} else if (strcmp(progname, "raw2audio") == 0) {
1187c478bdstevel@tonic-gate		(void) parse_format((char *)"ulaw", ihdr, ifmt, i_offset);
1197c478bdstevel@tonic-gate		(void) parse_format((char *)"ulaw", ohdr, ofmt, o_offset);
1207c478bdstevel@tonic-gate		israw++;
1217c478bdstevel@tonic-gate		pflag++;
1227c478bdstevel@tonic-gate	} else if (argc <= 1) {
1237c478bdstevel@tonic-gate		// audioconvert with no arguments
1247c478bdstevel@tonic-gate		usage();
1257c478bdstevel@tonic-gate	}
1267c478bdstevel@tonic-gate
1277c478bdstevel@tonic-gate	// now parse the rest of the arg's
1287c478bdstevel@tonic-gate	while ((c = getopt(argc, argv, opt_string)) != -1) {
1297c478bdstevel@tonic-gate		switch (c) {
1307c478bdstevel@tonic-gate#ifdef DEBUG
1317c478bdstevel@tonic-gate		case 'D':
1327c478bdstevel@tonic-gate			// enable debug messages
1337c478bdstevel@tonic-gate			Debug++;
1347c478bdstevel@tonic-gate			break;
1357c478bdstevel@tonic-gate#endif
1367c478bdstevel@tonic-gate		case 'p':
1377c478bdstevel@tonic-gate			// convert files in place
1387c478bdstevel@tonic-gate			if (outfile != NULL) {
1397c478bdstevel@tonic-gate				Err(MGET("can't use -p with -o\n"));
1407c478bdstevel@tonic-gate				exit(1);
1417c478bdstevel@tonic-gate			}
1427c478bdstevel@tonic-gate			pflag++;
1437c478bdstevel@tonic-gate			break;
1447c478bdstevel@tonic-gate		case 'F':
1457c478bdstevel@tonic-gate			// force treatment of audio files as raw files
1467c478bdstevel@tonic-gate			// (ignore filehdr).
1477c478bdstevel@tonic-gate			fflag++;
1487c478bdstevel@tonic-gate			break;
1497c478bdstevel@tonic-gate		case 'f':
1507c478bdstevel@tonic-gate			// save format string to parse later, but verify now
1517c478bdstevel@tonic-gate			out_fmt = optarg;
1527c478bdstevel@tonic-gate			if (parse_format(out_fmt, ohdr, ofmt, o_offset) == -1)
1537c478bdstevel@tonic-gate				exit(1);
1547c478bdstevel@tonic-gate			if (o_offset != 0) {
1557c478bdstevel@tonic-gate				Err(MGET("can't specify an offset with -f\n"));
1567c478bdstevel@tonic-gate				exit(1);
1577c478bdstevel@tonic-gate			}
1587c478bdstevel@tonic-gate			break;
1597c478bdstevel@tonic-gate		case 'o':
1607c478bdstevel@tonic-gate			if (pflag) {
1617c478bdstevel@tonic-gate				Err(MGET("can't use -o with -p\n"));
1627c478bdstevel@tonic-gate				exit(1);
1637c478bdstevel@tonic-gate			}
1647c478bdstevel@tonic-gate			outfile = optarg;
1657c478bdstevel@tonic-gate			break;
1667c478bdstevel@tonic-gate		case 'i':
1677c478bdstevel@tonic-gate			// if bogus input header, exit ...
1687c478bdstevel@tonic-gate			if (parse_format(optarg, ihdr, ifmt, i_offset) == -1) {
1697c478bdstevel@tonic-gate				exit(1);
1707c478bdstevel@tonic-gate			}
1717c478bdstevel@tonic-gate			israw++;
1727c478bdstevel@tonic-gate			break;
1737c478bdstevel@tonic-gate		default:
1747c478bdstevel@tonic-gate		case '?':
1757c478bdstevel@tonic-gate			usage();
1767c478bdstevel@tonic-gate		}
1777c478bdstevel@tonic-gate	}
1787c478bdstevel@tonic-gate
1797c478bdstevel@tonic-gate	// XXX - should check argument consistency here....
1807c478bdstevel@tonic-gate
1817c478bdstevel@tonic-gate	// If no args left, we're taking input from stdin.
1827c478bdstevel@tonic-gate	// In this case, make argv point to a fake argv with "-" as a file
1837c478bdstevel@tonic-gate	// name, and set optind and argc apropriately so we'll go through
1847c478bdstevel@tonic-gate	// the loop below once.
1857c478bdstevel@tonic-gate	if (optind >= argc) {
1867c478bdstevel@tonic-gate		argv = fake_argv;
1877c478bdstevel@tonic-gate		argc = 1;
1887c478bdstevel@tonic-gate		optind = 0;
1897c478bdstevel@tonic-gate		/*
1907c478bdstevel@tonic-gate		 * XXX - we turn off pflag if stdin is the only input file.
1917c478bdstevel@tonic-gate		 * this is kind of a hack. if invoked as raw2audio, pflag
1927c478bdstevel@tonic-gate		 * it turned on. if no files are given, we want to turn
1937c478bdstevel@tonic-gate		 * it off, otherwise we'll complain about using -p with
1947c478bdstevel@tonic-gate		 * stdin, which won't make sense if invoked as raw2audio.
1957c478bdstevel@tonic-gate		 * instead, just silently ignore. the message is still given
1967c478bdstevel@tonic-gate		 * and stdin is ignored if it's specified as one of several
1977c478bdstevel@tonic-gate		 * input files.
1987c478bdstevel@tonic-gate		 */
1997c478bdstevel@tonic-gate		pflag = 0;
2007c478bdstevel@tonic-gate	}
2017c478bdstevel@tonic-gate
2027c478bdstevel@tonic-gate	// From this point on we're looking at file names or -i args
2037c478bdstevel@tonic-gate	// for input format specs.
2047c478bdstevel@tonic-gate	for (; optind < argc; optind++) {
2057c478bdstevel@tonic-gate		// new input format spec.
2067c478bdstevel@tonic-gate		if (strcmp(argv[optind], "-i") == 0) {
2077c478bdstevel@tonic-gate			init_header(ihdr);
2087c478bdstevel@tonic-gate			i_offset = 0;
2097c478bdstevel@tonic-gate			ifmt = F_SUN;
2107c478bdstevel@tonic-gate			// if bogus input header, exit ...
2117c478bdstevel@tonic-gate			if (parse_format(argv[++optind], ihdr, ifmt, i_offset)
2127c478bdstevel@tonic-gate			    == -1) {
2137c478bdstevel@tonic-gate				exit(1);
2147c478bdstevel@tonic-gate			}
2157c478bdstevel@tonic-gate			israw++;
2167c478bdstevel@tonic-gate		} else if (strcmp(argv[optind], "-") == 0) {
2177c478bdstevel@tonic-gate			// ignore stdin argument if in place
2187c478bdstevel@tonic-gate			if (pflag) {
2197c478bdstevel@tonic-gate				Err(MGET("can't use %s with -p flag\n"),
2207c478bdstevel@tonic-gate				    Stdin);
2217c478bdstevel@tonic-gate				continue;
2227c478bdstevel@tonic-gate			}
2237c478bdstevel@tonic-gate
2247c478bdstevel@tonic-gate			if (stdin_seen) {
2257c478bdstevel@tonic-gate				Err(MGET("already used stdin for input\n"));
2267c478bdstevel@tonic-gate				continue;
2277c478bdstevel@tonic-gate			} else {
2287c478bdstevel@tonic-gate				stdin_seen++;
2297c478bdstevel@tonic-gate			}
2307c478bdstevel@tonic-gate
2317c478bdstevel@tonic-gate			infile = Stdin;
2327c478bdstevel@tonic-gate		} else {
2337c478bdstevel@tonic-gate			infile = argv[optind];
2347c478bdstevel@tonic-gate		}
2357c478bdstevel@tonic-gate
2367c478bdstevel@tonic-gate		// if no audio object returned, just continue to the next
2377c478bdstevel@tonic-gate		// file. if a fatal error occurs, open_input_file()
2387c478bdstevel@tonic-gate		// will exit the program.
2397c478bdstevel@tonic-gate		ifp =
2407c478bdstevel@tonic-gate		    open_input_file(infile, ihdr, israw, fflag, i_offset, fmt);
2417c478bdstevel@tonic-gate		if (!ifp) {
2427c478bdstevel@tonic-gate			continue;
2437c478bdstevel@tonic-gate		}
2447c478bdstevel@tonic-gate
2457c478bdstevel@tonic-gate		if ((err = ifp->Open()) != AUDIO_SUCCESS) {
2467c478bdstevel@tonic-gate			Err(MGET("open error on input file %s - %s\n"),
2477c478bdstevel@tonic-gate			    infile, err.msg());
2487c478bdstevel@tonic-gate			exit(1);
2497c478bdstevel@tonic-gate		}
2507c478bdstevel@tonic-gate		ifp->Reference();
2517c478bdstevel@tonic-gate
2527c478bdstevel@tonic-gate		// create the output file if not created yet, or if
2537c478bdstevel@tonic-gate		// converting in place. ofp will be NULL only the first
2547c478bdstevel@tonic-gate		// time through. use the header of the first input file
2557c478bdstevel@tonic-gate		// to base the output format on - then create the output
2567c478bdstevel@tonic-gate		// header w/the output format spec.
2577c478bdstevel@tonic-gate		if ((ofp == NULL) && !pflag) {
2587c478bdstevel@tonic-gate
2597c478bdstevel@tonic-gate			ohdr = ifp->GetHeader();
2607c478bdstevel@tonic-gate			ohdr = ifp->GetHeader();
2617c478bdstevel@tonic-gate			ofmt = ifmt;
2627c478bdstevel@tonic-gate			// just use input hdr if no output hdr spec
2637c478bdstevel@tonic-gate			if (out_fmt) {
2647c478bdstevel@tonic-gate				if (parse_format(out_fmt, ohdr, ofmt, o_offset)
2657c478bdstevel@tonic-gate				    == -1) {
2667c478bdstevel@tonic-gate					exit(1);
2677c478bdstevel@tonic-gate				}
2687c478bdstevel@tonic-gate			}
2697c478bdstevel@tonic-gate
2707c478bdstevel@tonic-gate			// need to check before output is opened ...
2717c478bdstevel@tonic-gate			if (verify_conversion(ifp->GetHeader(), ohdr) == -1) {
2727c478bdstevel@tonic-gate				// XXX - bomb out or skip?
2737c478bdstevel@tonic-gate				exit(3);
2747c478bdstevel@tonic-gate			}
2757c478bdstevel@tonic-gate
2767c478bdstevel@tonic-gate			// Create the file and set the info string.
2777c478bdstevel@tonic-gate			char		*infoString;
2782325c4fToomas Soome			int		infoStringLen;
2797c478bdstevel@tonic-gate			infoString = ifp->GetInfostring(infoStringLen);
2807c478bdstevel@tonic-gate			ofp = create_output_file(outfile, ohdr, ofmt,
2817c478bdstevel@tonic-gate						    infoString);
2827c478bdstevel@tonic-gate
2837c478bdstevel@tonic-gate		} else if (pflag) {
2847c478bdstevel@tonic-gate
2857c478bdstevel@tonic-gate			// create new output header based on each input file
2867c478bdstevel@tonic-gate			ohdr = ifp->GetHeader();
2877c478bdstevel@tonic-gate			ofmt = ifmt;
2887c478bdstevel@tonic-gate			// just use input hdr if no output hdr spec
2897c478bdstevel@tonic-gate			if (out_fmt) {
2907c478bdstevel@tonic-gate				if (parse_format(out_fmt, ohdr, ofmt, o_offset)
2917c478bdstevel@tonic-gate				    == -1) {
2927c478bdstevel@tonic-gate					exit(1);
2937c478bdstevel@tonic-gate				}
2947c478bdstevel@tonic-gate			}
2957c478bdstevel@tonic-gate
2967c478bdstevel@tonic-gate			// get the *real* path of the infile (follow sym-links),
2977c478bdstevel@tonic-gate			// and the stat info.
2987c478bdstevel@tonic-gate			realfile = infile;
2997c478bdstevel@tonic-gate			get_realfile(realfile, &st);
3007c478bdstevel@tonic-gate
3017c478bdstevel@tonic-gate			// if the file is read-only, give up
3027c478bdstevel@tonic-gate			if (access(realfile, W_OK)) {
3037c478bdstevel@tonic-gate				// XXX - do we really want to exit?
3047c478bdstevel@tonic-gate				perror(infile);
3057c478bdstevel@tonic-gate				Err(MGET("cannot rewrite in place\n"));
3067c478bdstevel@tonic-gate				exit(1);
3077c478bdstevel@tonic-gate			}
3087c478bdstevel@tonic-gate
3097c478bdstevel@tonic-gate			// this is now the output file.
3107c478bdstevel@tonic-gate			i = strlen(realfile) + strlen(Suffix) + 1;
3117c478bdstevel@tonic-gate			outfile = (char *)malloc((unsigned)i);
3127c478bdstevel@tonic-gate			if (outfile == NULL) {
3137c478bdstevel@tonic-gate				Err(MGET("out of memory\n"));
3147c478bdstevel@tonic-gate				exit(1);
3157c478bdstevel@tonic-gate			}
3167c478bdstevel@tonic-gate			(void) sprintf(outfile, "%s%s", realfile, Suffix);
3177c478bdstevel@tonic-gate
3187c478bdstevel@tonic-gate			// outfile will get re-assigned to a tmp file
3197c478bdstevel@tonic-gate			if (verify_conversion(ifp->GetHeader(), ohdr) == -1) {
3207c478bdstevel@tonic-gate				// XXX - bomb out or skip?
3217c478bdstevel@tonic-gate				exit(3);
3227c478bdstevel@tonic-gate			}
3237c478bdstevel@tonic-gate
3247c478bdstevel@tonic-gate			// If no conversion, just skip the file
3257c478bdstevel@tonic-gate			if (noop_conversion(ifp->GetHeader(), ohdr,
3267c478bdstevel@tonic-gate			    fmt, ofmt, i_offset, o_offset)) {
3277c478bdstevel@tonic-gate				if (Debug)
3287c478bdstevel@tonic-gate				    Err(MGET(
3297c478bdstevel@tonic-gate					"%s: no-op conversion...skipping\n"),
3307c478bdstevel@tonic-gate					infile);
3317c478bdstevel@tonic-gate				continue;
3327c478bdstevel@tonic-gate			}
3337c478bdstevel@tonic-gate
3347c478bdstevel@tonic-gate			// Get the input info string.
3357c478bdstevel@tonic-gate			char		*infoString;
3362325c4fToomas Soome			int		infoStringLen;
3377c478bdstevel@tonic-gate			infoString = ifp->GetInfostring(infoStringLen);
3387c478bdstevel@tonic-gate			ofp = create_output_file(outfile, ohdr, ofmt,
3397c478bdstevel@tonic-gate						    infoString);
3407c478bdstevel@tonic-gate		}
3417c478bdstevel@tonic-gate
3427c478bdstevel@tonic-gate		// verify that it's a valid conversion by looking at the
3437c478bdstevel@tonic-gate		// file headers. (this will be called twice for the first
3447c478bdstevel@tonic-gate		// file if *not* converting in place. that's ok....
3457c478bdstevel@tonic-gate		if (!pflag && (verify_conversion(ifp->GetHeader(), ohdr)
3467c478bdstevel@tonic-gate		    == -1)) {
3477c478bdstevel@tonic-gate			// XXX - bomb out or skip file if invalid conversion?
3487c478bdstevel@tonic-gate			exit(3);
3497c478bdstevel@tonic-gate		}
3507c478bdstevel@tonic-gate
3517c478bdstevel@tonic-gate		// do the conversion, if error, bomb out
3527c478bdstevel@tonic-gate		if (do_convert(ifp, ofp) == -1) {
3537c478bdstevel@tonic-gate			exit(4);
3547c478bdstevel@tonic-gate		}
3557c478bdstevel@tonic-gate
3567c478bdstevel@tonic-gate		ifp->Close();
3577c478bdstevel@tonic-gate		ifp->Dereference();
3587c478bdstevel@tonic-gate
3597c478bdstevel@tonic-gate		// if in place, finish up by renaming the outfile to
3607c478bdstevel@tonic-gate		// back to the infile.
3617c478bdstevel@tonic-gate		if (pflag) {
3627c478bdstevel@tonic-gate			delete(ofp);	// will close and deref, etc.
3637c478bdstevel@tonic-gate
3647c478bdstevel@tonic-gate			if (rename(outfile, realfile) < 0) {
3657c478bdstevel@tonic-gate				perror(outfile);
3667c478bdstevel@tonic-gate				Err(MGET("error renaming %s to %s"),
3677c478bdstevel@tonic-gate				    outfile, realfile);
3687c478bdstevel@tonic-gate				exit(1);
3697c478bdstevel@tonic-gate			}
3707c478bdstevel@tonic-gate			/* Set the permissions to match the original */
3717c478bdstevel@tonic-gate			if (chmod(realfile, (int)st.st_mode) < 0) {
3727c478bdstevel@tonic-gate				Err(MGET("WARNING: could not reset mode of"));
3737c478bdstevel@tonic-gate				perror(realfile);
3747c478bdstevel@tonic-gate			}
3757c478bdstevel@tonic-gate		}
3767c478bdstevel@tonic-gate	}
3777c478bdstevel@tonic-gate
3787c478bdstevel@tonic-gate	if (!pflag) {
3797c478bdstevel@tonic-gate		delete(ofp);		// close output file
3807c478bdstevel@tonic-gate	}
3817c478bdstevel@tonic-gate
3822325c4fToomas Soome	return (0);
3837c478bdstevel@tonic-gate}
3847c478bdstevel@tonic-gate
3857c478bdstevel@tonic-gate
3867c478bdstevel@tonic-gate// initialize audio hdr to default val's
3877c478bdstevel@tonic-gatevoid
3887c478bdstevel@tonic-gateinit_header(
3897c478bdstevel@tonic-gate	AudioHdr&	hdr)
3907c478bdstevel@tonic-gate{
3917c478bdstevel@tonic-gate	hdr.encoding = NONE;
3927c478bdstevel@tonic-gate	hdr.sample_rate = 0;
3937c478bdstevel@tonic-gate	hdr.samples_per_unit = 0;
3947c478bdstevel@tonic-gate	hdr.bytes_per_unit = 0;
3957c478bdstevel@tonic-gate	hdr.channels = 0;
3967c478bdstevel@tonic-gate}
3977c478bdstevel@tonic-gate
3987c478bdstevel@tonic-gateextern "C" { void _doprnt(char *, ...); }
3997c478bdstevel@tonic-gate
4007c478bdstevel@tonic-gate// report a fatal error and exit
4017c478bdstevel@tonic-gatevoid
4027c478bdstevel@tonic-gateErr(char *format, ...)
4037c478bdstevel@tonic-gate{
4047c478bdstevel@tonic-gate	va_list ap;
4057c478bdstevel@tonic-gate
4067c478bdstevel@tonic-gate	va_start(ap, format);
4077c478bdstevel@tonic-gate	fprintf(stderr, "%s: ", progname);
4087c478bdstevel@tonic-gate	_doprnt(format, ap, stderr);
4097c478bdstevel@tonic-gate	fflush(stderr);
4107c478bdstevel@tonic-gate	va_end(ap);
4117c478bdstevel@tonic-gate}
4127c478bdstevel@tonic-gate
4137c478bdstevel@tonic-gatevoid
4147c478bdstevel@tonic-gateusage()
4157c478bdstevel@tonic-gate{
4167c478bdstevel@tonic-gate	fprintf(stderr, MGET(
4177c478bdstevel@tonic-gate	    "Convert between audio file formats and data encodings -- usage:\n"
4187c478bdstevel@tonic-gate	    "\t%s [-pF] [-f outfmt] [-o outfile] [[-i infmt] [file ...]] ...\n"
4197c478bdstevel@tonic-gate	    "where:\n"
4207c478bdstevel@tonic-gate	    "\t-p\tConvert files in place\n"
4217c478bdstevel@tonic-gate	    "\t-F\tForce interpretation of -i (ignore existing file hdr)\n"
4227c478bdstevel@tonic-gate	    "\t-f\tOutput format description\n"
4237c478bdstevel@tonic-gate	    "\t-o\tOutput file (default: stdout)\n"
4247c478bdstevel@tonic-gate	    "\t-i\tInput format description\n"
4257c478bdstevel@tonic-gate	    "\tfile\tList of files to convert (default: stdin)\n\n"
4267c478bdstevel@tonic-gate	    "Format Description:\n"
4277c478bdstevel@tonic-gate	    "\tkeyword=value[,keyword=value...]\n"
4287c478bdstevel@tonic-gate	    "where:\n"
4297c478bdstevel@tonic-gate	    "\tKeywords:\tValues:\n"
4307c478bdstevel@tonic-gate	    "\trate\t\tSample Rate in samples/second\n"
4317c478bdstevel@tonic-gate	    "\tchannels\tNumber of interleaved channels\n"
4327c478bdstevel@tonic-gate	    "\tencoding\tAudio encoding. One of:\n"
4337c478bdstevel@tonic-gate	    "\t\t\t    ulaw, alaw, g721, g723,\n"
4347c478bdstevel@tonic-gate	    "\t\t\t    linear8, linear16, linear32\n"
4357c478bdstevel@tonic-gate	    "\t\t\t    pcm   (same as linear16)\n"
4367c478bdstevel@tonic-gate	    "\t\t\t    voice (ulaw,mono,rate=8k)\n"
4377c478bdstevel@tonic-gate	    "\t\t\t    cd    (linear16,stereo,rate=44.1k)\n"
4387c478bdstevel@tonic-gate	    "\t\t\t    dat   (linear16,stereo,rate=48k)\n"
4397c478bdstevel@tonic-gate	    "\tformat\t\tFile format. One of:\n"
4407c478bdstevel@tonic-gate	    "\t\t\t    sun, raw (no format)\n"
4417c478bdstevel@tonic-gate	    "\toffset\t\tByte offset (raw input only)\n"),
4427c478bdstevel@tonic-gate	    progname);
4437c478bdstevel@tonic-gate	exit(1);
4447c478bdstevel@tonic-gate}
445