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