xref: /illumos-gate/usr/src/cmd/cdrw/bstream.c (revision 54cafc85)
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
5*54cafc85Szk  * Common Development and Distribution License (the "License").
6*54cafc85Szk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*54cafc85Szk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <fcntl.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <sys/stat.h>
347c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <libintl.h>
387c478bd9Sstevel@tonic-gate #include <limits.h>
397c478bd9Sstevel@tonic-gate #include <audio/au.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include "bstream.h"
427c478bd9Sstevel@tonic-gate #include "util.h"
437c478bd9Sstevel@tonic-gate #include "audio.h"
447c478bd9Sstevel@tonic-gate #include "byteorder.h"
457c478bd9Sstevel@tonic-gate #include "main.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate int str_errno;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate char *
507c478bd9Sstevel@tonic-gate str_errno_to_string(int serrno)
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate 	switch (serrno) {
537c478bd9Sstevel@tonic-gate 	case STR_ERR_NO_ERR:
547c478bd9Sstevel@tonic-gate 		return (gettext("No error"));
557c478bd9Sstevel@tonic-gate 	case STR_ERR_NO_REG_FILE:
567c478bd9Sstevel@tonic-gate 		return (gettext("Not a regular file"));
577c478bd9Sstevel@tonic-gate 	case STR_ERR_NO_READ_STDIN:
587c478bd9Sstevel@tonic-gate 		return (gettext("Stdin not open for reading"));
597c478bd9Sstevel@tonic-gate 	case STR_ERR_AU_READ_ERR:
607c478bd9Sstevel@tonic-gate 		return (gettext("Unable to read au header"));
617c478bd9Sstevel@tonic-gate 	case STR_ERR_AU_UNSUPPORTED_FORMAT:
627c478bd9Sstevel@tonic-gate 		return (gettext("Unsupported au format"));
637c478bd9Sstevel@tonic-gate 	case STR_ERR_AU_BAD_HEADER:
647c478bd9Sstevel@tonic-gate 		return (gettext("Bad au header"));
657c478bd9Sstevel@tonic-gate 	case STR_ERR_WAV_READ_ERR:
667c478bd9Sstevel@tonic-gate 		return (gettext("Unable to read wav header"));
677c478bd9Sstevel@tonic-gate 	case STR_ERR_WAV_UNSUPPORTED_FORMAT:
687c478bd9Sstevel@tonic-gate 		return (gettext("Unsupported wav format"));
697c478bd9Sstevel@tonic-gate 	case STR_ERR_WAV_BAD_HEADER:
707c478bd9Sstevel@tonic-gate 		return (gettext("Bad wav header"));
717c478bd9Sstevel@tonic-gate 	default:
727c478bd9Sstevel@tonic-gate 		return (gettext("unknown error"));
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static int
777c478bd9Sstevel@tonic-gate file_stream_size(bstreamhandle h, off_t *size)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	struct stat st;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	str_errno = 0;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if (fstat(h->bstr_fd, &st) < 0)
847c478bd9Sstevel@tonic-gate 		return (0);
857c478bd9Sstevel@tonic-gate 	if ((st.st_mode & S_IFMT) != S_IFREG) {
867c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_NO_REG_FILE;
877c478bd9Sstevel@tonic-gate 		return (0);
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 	*size = st.st_size;
907c478bd9Sstevel@tonic-gate 	return (1);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static int
947c478bd9Sstevel@tonic-gate audio_stream_size(bstreamhandle h, off_t *size)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 	str_errno = 0;
977c478bd9Sstevel@tonic-gate 	*size = (off_t)(uintptr_t)(h->bstr_private);
987c478bd9Sstevel@tonic-gate 	return (1);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static int
1027c478bd9Sstevel@tonic-gate file_stream_read(bstreamhandle h, uchar_t *buf, off_t size)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	str_errno = 0;
1057c478bd9Sstevel@tonic-gate 	return (read(h->bstr_fd, buf, size));
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate static int
1097c478bd9Sstevel@tonic-gate file_stream_write(bstreamhandle h, uchar_t *buf, off_t size)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	str_errno = 0;
1127c478bd9Sstevel@tonic-gate 	return (write(h->bstr_fd, buf, size));
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * with reverse byteorder
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate static int
1197c478bd9Sstevel@tonic-gate file_stream_read_wrbo(bstreamhandle h, uchar_t *buf, off_t size)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	int cnt;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	str_errno = 0;
1247c478bd9Sstevel@tonic-gate 	cnt = read(h->bstr_fd, buf, size);
1257c478bd9Sstevel@tonic-gate 	if (cnt > 0) {
1267c478bd9Sstevel@tonic-gate 		int i;
1277c478bd9Sstevel@tonic-gate 		uchar_t ch;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 		for (i = 0; i < cnt; i += 2) {
1307c478bd9Sstevel@tonic-gate 			ch = buf[i];
1317c478bd9Sstevel@tonic-gate 			buf[i] = buf[i+1];
1327c478bd9Sstevel@tonic-gate 			buf[i+1] = ch;
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 	return (cnt);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * This will change the byteorder in the buffer but that is fine with us.
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate static int
1427c478bd9Sstevel@tonic-gate file_stream_write_wrbo(bstreamhandle h, uchar_t *buf, off_t size)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	int i;
1457c478bd9Sstevel@tonic-gate 	uchar_t ch;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	str_errno = 0;
1487c478bd9Sstevel@tonic-gate 	if (size > 0) {
1497c478bd9Sstevel@tonic-gate 		for (i = 0; i < size; i += 2) {
1507c478bd9Sstevel@tonic-gate 			ch = buf[i];
1517c478bd9Sstevel@tonic-gate 			buf[i] = buf[i+1];
1527c478bd9Sstevel@tonic-gate 			buf[i+1] = ch;
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 	return (write(h->bstr_fd, buf, size));
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate static int
1597c478bd9Sstevel@tonic-gate file_stream_close(bstreamhandle h)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	int fd;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	str_errno = 0;
1647c478bd9Sstevel@tonic-gate 	fd = h->bstr_fd;
1657c478bd9Sstevel@tonic-gate 	free(h);
1667c478bd9Sstevel@tonic-gate 	return (close(fd));
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate static int
1707c478bd9Sstevel@tonic-gate stdin_stream_close(bstreamhandle h)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	str_errno = 0;
1737c478bd9Sstevel@tonic-gate 	free(h);
1747c478bd9Sstevel@tonic-gate 	return (0);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate static int
1787c478bd9Sstevel@tonic-gate wav_write_stream_close(bstreamhandle h)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	uint32_t sz;
1817c478bd9Sstevel@tonic-gate 	Wave_filehdr wav;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	str_errno = 0;
1847c478bd9Sstevel@tonic-gate 	(void) memset(&wav, 0, sizeof (wav));
1857c478bd9Sstevel@tonic-gate 	sz = lseek(h->bstr_fd, 0L, SEEK_END);
1867c478bd9Sstevel@tonic-gate 	(void) lseek(h->bstr_fd, 0L, SEEK_SET);
1877c478bd9Sstevel@tonic-gate 	if (read(h->bstr_fd, &wav, sizeof (wav)) != sizeof (wav)) {
1887c478bd9Sstevel@tonic-gate 		return (1);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 	wav.total_chunk_size = CPU_TO_LE32(sz - 8);
1917c478bd9Sstevel@tonic-gate 	wav.data_size = CPU_TO_LE32(sz - 44);
1927c478bd9Sstevel@tonic-gate 	(void) lseek(h->bstr_fd, 0L, SEEK_SET);
1937c478bd9Sstevel@tonic-gate 	if (write(h->bstr_fd, &wav, sizeof (wav)) != sizeof (wav)) {
1947c478bd9Sstevel@tonic-gate 		return (1);
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 	(void) close(h->bstr_fd);
1977c478bd9Sstevel@tonic-gate 	free(h);
1987c478bd9Sstevel@tonic-gate 	return (0);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate static int
2027c478bd9Sstevel@tonic-gate au_write_stream_close(bstreamhandle h)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	uint32_t sz;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	str_errno = 0;
2077c478bd9Sstevel@tonic-gate 	sz = lseek(h->bstr_fd, 0L, SEEK_END);
2087c478bd9Sstevel@tonic-gate 	sz -= PRE_DEF_AU_HDR_LEN;
2097c478bd9Sstevel@tonic-gate 	sz = CPU_TO_BE32(sz);
2107c478bd9Sstevel@tonic-gate 	if (lseek(h->bstr_fd, 8L, SEEK_SET) < 0)
2117c478bd9Sstevel@tonic-gate 		return (1);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if (write(h->bstr_fd, &sz, 4) < 0)
2147c478bd9Sstevel@tonic-gate 		return (1);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	(void) close(h->bstr_fd);
2177c478bd9Sstevel@tonic-gate 	free(h);
2187c478bd9Sstevel@tonic-gate 	return (0);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate /* ARGSUSED */
2227c478bd9Sstevel@tonic-gate static void
2237c478bd9Sstevel@tonic-gate stdin_stream_rewind(bstreamhandle h)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate static void
2287c478bd9Sstevel@tonic-gate file_stream_rewind(bstreamhandle h)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	(void) lseek(h->bstr_fd, 0L, SEEK_SET);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate static void
2347c478bd9Sstevel@tonic-gate au_stream_rewind(bstreamhandle h)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	au_filehdr_t au;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	(void) lseek(h->bstr_fd, 0L, SEEK_SET);
2397c478bd9Sstevel@tonic-gate 	if (read(h->bstr_fd, &au, sizeof (au)) != sizeof (au)) {
2407c478bd9Sstevel@tonic-gate 		return;
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	if (lseek(h->bstr_fd, (long)(BE32_TO_CPU(au.au_offset)),
2447c478bd9Sstevel@tonic-gate 	    SEEK_SET) < 0) {
2457c478bd9Sstevel@tonic-gate 		return;
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate static void
2507c478bd9Sstevel@tonic-gate wav_stream_rewind(bstreamhandle h)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	(void) lseek(h->bstr_fd, (long)(sizeof (Wave_filehdr)), SEEK_SET);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate bstreamhandle
2567c478bd9Sstevel@tonic-gate open_file_read_stream(char *file)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	bstreamhandle h;
2597c478bd9Sstevel@tonic-gate 	int fd;
2607c478bd9Sstevel@tonic-gate 	struct stat st;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	str_errno = 0;
2637c478bd9Sstevel@tonic-gate 	if (stat(file, &st) < 0)
2647c478bd9Sstevel@tonic-gate 		return (NULL);
2657c478bd9Sstevel@tonic-gate 	if ((st.st_mode & S_IFMT) == S_IFDIR) {
2667c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_NO_REG_FILE;
2677c478bd9Sstevel@tonic-gate 		return (NULL);
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 	fd = open(file, O_RDONLY);
2707c478bd9Sstevel@tonic-gate 	if (fd < 0)
2717c478bd9Sstevel@tonic-gate 		return (NULL);
2727c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
2737c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
2747c478bd9Sstevel@tonic-gate 	h->bstr_read = file_stream_read;
2757c478bd9Sstevel@tonic-gate 	h->bstr_close = file_stream_close;
2767c478bd9Sstevel@tonic-gate 	h->bstr_size = file_stream_size;
2777c478bd9Sstevel@tonic-gate 	h->bstr_rewind = file_stream_rewind;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	return (h);
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate bstreamhandle
2837c478bd9Sstevel@tonic-gate open_stdin_read_stream(void)
2847c478bd9Sstevel@tonic-gate {
2857c478bd9Sstevel@tonic-gate 	bstreamhandle h;
2867c478bd9Sstevel@tonic-gate 	int mode;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	str_errno = 0;
2897c478bd9Sstevel@tonic-gate 	if ((mode = fcntl(0, F_GETFD, NULL)) < 0) {
2907c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_NO_READ_STDIN;
2917c478bd9Sstevel@tonic-gate 		return (NULL);
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 	mode &= 3;
2947c478bd9Sstevel@tonic-gate 	if ((mode != O_RDONLY) && (mode != O_RDWR)) {
2957c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_NO_READ_STDIN;
2967c478bd9Sstevel@tonic-gate 		return (NULL);
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
2997c478bd9Sstevel@tonic-gate 	h->bstr_fd = 0;
3007c478bd9Sstevel@tonic-gate 	h->bstr_read = file_stream_read;
3017c478bd9Sstevel@tonic-gate 	h->bstr_close = stdin_stream_close;
3027c478bd9Sstevel@tonic-gate 	h->bstr_size = file_stream_size;
3037c478bd9Sstevel@tonic-gate 	h->bstr_rewind = stdin_stream_rewind;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	return (h);
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate bstreamhandle
3097c478bd9Sstevel@tonic-gate open_au_read_stream(char *fname)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate 	bstreamhandle h;
3127c478bd9Sstevel@tonic-gate 	int fd, sav;
3137c478bd9Sstevel@tonic-gate 	au_filehdr_t *au;
3147c478bd9Sstevel@tonic-gate 	struct stat st;
3157c478bd9Sstevel@tonic-gate 	uint32_t data_size;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	au = NULL;
3187c478bd9Sstevel@tonic-gate 	str_errno = 0;
3197c478bd9Sstevel@tonic-gate 	fd = open(fname, O_RDONLY);
3207c478bd9Sstevel@tonic-gate 	if (fd < 0)
3217c478bd9Sstevel@tonic-gate 		return (NULL);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if (fstat(fd, &st) < 0) {
3247c478bd9Sstevel@tonic-gate 		goto au_open_failed;
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 	if ((st.st_mode & S_IFMT) != S_IFREG) {
3277c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_NO_REG_FILE;
3287c478bd9Sstevel@tonic-gate 		goto au_open_failed;
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 	au = (au_filehdr_t *)my_zalloc(sizeof (*au));
3317c478bd9Sstevel@tonic-gate 	if (read(fd, au, sizeof (*au)) != sizeof (*au)) {
3327c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_AU_READ_ERR;
3337c478bd9Sstevel@tonic-gate 		goto au_open_failed;
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 	au->au_magic = BE32_TO_CPU(au->au_magic);
3367c478bd9Sstevel@tonic-gate 	au->au_offset = BE32_TO_CPU(au->au_offset);
3377c478bd9Sstevel@tonic-gate 	au->au_data_size = BE32_TO_CPU(au->au_data_size);
3387c478bd9Sstevel@tonic-gate 	au->au_encoding = BE32_TO_CPU(au->au_encoding);
3397c478bd9Sstevel@tonic-gate 	au->au_sample_rate = BE32_TO_CPU(au->au_sample_rate);
3407c478bd9Sstevel@tonic-gate 	au->au_channels = BE32_TO_CPU(au->au_channels);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	if (au->au_magic != AUDIO_AU_FILE_MAGIC) {
3437c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_AU_BAD_HEADER;
3447c478bd9Sstevel@tonic-gate 		goto au_open_failed;
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 	if ((au->au_encoding != AUDIO_AU_ENCODING_LINEAR_16) ||
3477c478bd9Sstevel@tonic-gate 	    (au->au_sample_rate != 44100) || (au->au_channels != 2)) {
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_AU_UNSUPPORTED_FORMAT;
3507c478bd9Sstevel@tonic-gate 		goto au_open_failed;
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 	if (au->au_data_size != AUDIO_AU_UNKNOWN_SIZE) {
3537c478bd9Sstevel@tonic-gate 		if ((au->au_offset + au->au_data_size) != st.st_size) {
3547c478bd9Sstevel@tonic-gate 			str_errno = STR_ERR_AU_BAD_HEADER;
3557c478bd9Sstevel@tonic-gate 			goto au_open_failed;
3567c478bd9Sstevel@tonic-gate 		}
3577c478bd9Sstevel@tonic-gate 		data_size = au->au_data_size;
3587c478bd9Sstevel@tonic-gate 	} else {
3597c478bd9Sstevel@tonic-gate 		data_size = st.st_size - au->au_offset;
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 	if (data_size == 0) {
3627c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_AU_UNSUPPORTED_FORMAT;
3637c478bd9Sstevel@tonic-gate 		goto au_open_failed;
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 	if (lseek(fd, au->au_offset, SEEK_SET) < 0) {
3667c478bd9Sstevel@tonic-gate 		goto au_open_failed;
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	free(au);
3707c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
3717c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
3727c478bd9Sstevel@tonic-gate 	h->bstr_read = file_stream_read_wrbo;
3737c478bd9Sstevel@tonic-gate 	h->bstr_close = file_stream_close;
3747c478bd9Sstevel@tonic-gate 	h->bstr_size = audio_stream_size;
3757c478bd9Sstevel@tonic-gate 	h->bstr_rewind = au_stream_rewind;
3767c478bd9Sstevel@tonic-gate 	h->bstr_private = (void *)data_size;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	return (h);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate au_open_failed:
3817c478bd9Sstevel@tonic-gate 	sav = errno;
3827c478bd9Sstevel@tonic-gate 	(void) close(fd);
3837c478bd9Sstevel@tonic-gate 	if (au != NULL)
3847c478bd9Sstevel@tonic-gate 		free(au);
3857c478bd9Sstevel@tonic-gate 	errno = sav;
3867c478bd9Sstevel@tonic-gate 	return (NULL);
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate bstreamhandle
3907c478bd9Sstevel@tonic-gate open_wav_read_stream(char *fname)
3917c478bd9Sstevel@tonic-gate {
3927c478bd9Sstevel@tonic-gate 	bstreamhandle h;
3937c478bd9Sstevel@tonic-gate 	int fd, sav;
3947c478bd9Sstevel@tonic-gate 	Wave_filehdr *wav;
3957c478bd9Sstevel@tonic-gate 	struct stat st;
3967c478bd9Sstevel@tonic-gate 	uint32_t data_size;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	wav = NULL;
3997c478bd9Sstevel@tonic-gate 	str_errno = 0;
4007c478bd9Sstevel@tonic-gate 	fd = open(fname, O_RDONLY);
4017c478bd9Sstevel@tonic-gate 	if (fd < 0)
4027c478bd9Sstevel@tonic-gate 		return (NULL);
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	if (fstat(fd, &st) < 0) {
4057c478bd9Sstevel@tonic-gate 		goto wav_open_failed;
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 	if ((st.st_mode & S_IFMT) != S_IFREG) {
4087c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_NO_REG_FILE;
4097c478bd9Sstevel@tonic-gate 		goto wav_open_failed;
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 	wav = (Wave_filehdr *)my_zalloc(sizeof (*wav));
4127c478bd9Sstevel@tonic-gate 	if (read(fd, wav, sizeof (*wav)) != sizeof (*wav)) {
4137c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_WAV_READ_ERR;
4147c478bd9Sstevel@tonic-gate 		goto wav_open_failed;
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 	if ((strncmp(wav->riff, "RIFF", 4) != 0) ||
4177c478bd9Sstevel@tonic-gate 		(strncmp(wav->wave, "WAVE", 4) != 0)) {
4187c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_WAV_BAD_HEADER;
4197c478bd9Sstevel@tonic-gate 		goto wav_open_failed;
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 	if (((CPU_TO_LE32(wav->total_chunk_size) + 8) != st.st_size) ||
4227c478bd9Sstevel@tonic-gate 	    (strncmp(wav->fmt, "fmt ", 4) != 0) ||
4237c478bd9Sstevel@tonic-gate 	    (CPU_TO_LE16(wav->fmt_tag) != 1) ||
4247c478bd9Sstevel@tonic-gate 	    (CPU_TO_LE16(wav->n_channels) != 2) ||
4257c478bd9Sstevel@tonic-gate 	    (CPU_TO_LE32(wav->sample_rate) != 44100) ||
4267c478bd9Sstevel@tonic-gate 	    (CPU_TO_LE16(wav->bits_per_sample) != 16) ||
4277c478bd9Sstevel@tonic-gate 	    (strncmp(wav->data, "data", 4) != 0) ||
4287c478bd9Sstevel@tonic-gate 	    ((CPU_TO_LE32(wav->data_size) + 44) != st.st_size)) {
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_WAV_UNSUPPORTED_FORMAT;
4317c478bd9Sstevel@tonic-gate 		goto wav_open_failed;
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 	data_size = CPU_TO_LE32(wav->data_size);
4347c478bd9Sstevel@tonic-gate 	if (lseek(fd, sizeof (*wav), SEEK_SET) < 0) {
4357c478bd9Sstevel@tonic-gate 		goto wav_open_failed;
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	free(wav);
4397c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
4407c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
4417c478bd9Sstevel@tonic-gate 	h->bstr_read = file_stream_read;
4427c478bd9Sstevel@tonic-gate 	h->bstr_close = file_stream_close;
4437c478bd9Sstevel@tonic-gate 	h->bstr_size = audio_stream_size;
4447c478bd9Sstevel@tonic-gate 	h->bstr_rewind = wav_stream_rewind;
4457c478bd9Sstevel@tonic-gate 	h->bstr_private = (void *)data_size;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	return (h);
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate wav_open_failed:
4507c478bd9Sstevel@tonic-gate 	sav = errno;
4517c478bd9Sstevel@tonic-gate 	(void) close(fd);
4527c478bd9Sstevel@tonic-gate 	if (wav != NULL)
4537c478bd9Sstevel@tonic-gate 		free(wav);
4547c478bd9Sstevel@tonic-gate 	errno = sav;
4557c478bd9Sstevel@tonic-gate 	return (NULL);
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate bstreamhandle
4597c478bd9Sstevel@tonic-gate open_aur_read_stream(char *fname)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate 	bstreamhandle h;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	h = open_file_read_stream(fname);
4647c478bd9Sstevel@tonic-gate 	if (h != NULL) {
4657c478bd9Sstevel@tonic-gate 		h->bstr_read = file_stream_read_wrbo;
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 	return (h);
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate bstreamhandle
4717c478bd9Sstevel@tonic-gate open_au_write_stream(char *fname)
4727c478bd9Sstevel@tonic-gate {
4737c478bd9Sstevel@tonic-gate 	bstreamhandle h;
4747c478bd9Sstevel@tonic-gate 	int esav, fd;
4757c478bd9Sstevel@tonic-gate 	uchar_t head[] = PRE_DEF_AU_HDR;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	str_errno = 0;
4787c478bd9Sstevel@tonic-gate 	fd = -1;
4797c478bd9Sstevel@tonic-gate 	/* O_RDWR because we need to read while closing */
4807c478bd9Sstevel@tonic-gate 	fd = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666);
4817c478bd9Sstevel@tonic-gate 	if (fd < 0)
4827c478bd9Sstevel@tonic-gate 		goto open_au_write_stream_failed;
4837c478bd9Sstevel@tonic-gate 	if (write(fd, head, PRE_DEF_AU_HDR_LEN) != PRE_DEF_AU_HDR_LEN) {
4847c478bd9Sstevel@tonic-gate 		goto open_au_write_stream_failed;
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
4877c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
4887c478bd9Sstevel@tonic-gate 	h->bstr_write = file_stream_write_wrbo;
4897c478bd9Sstevel@tonic-gate 	h->bstr_close = au_write_stream_close;
4907c478bd9Sstevel@tonic-gate 	return (h);
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate open_au_write_stream_failed:
4937c478bd9Sstevel@tonic-gate 	esav = errno;
4947c478bd9Sstevel@tonic-gate 	if (fd != -1)
4957c478bd9Sstevel@tonic-gate 		(void) close(fd);
4967c478bd9Sstevel@tonic-gate 	errno = esav;
4977c478bd9Sstevel@tonic-gate 	return (NULL);
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate bstreamhandle
5017c478bd9Sstevel@tonic-gate open_wav_write_stream(char *fname)
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate 	bstreamhandle h;
5047c478bd9Sstevel@tonic-gate 	int esav, fd;
5057c478bd9Sstevel@tonic-gate 	uchar_t head[] = PRE_DEF_WAV_HDR;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	str_errno = 0;
5087c478bd9Sstevel@tonic-gate 	fd = -1;
5097c478bd9Sstevel@tonic-gate 	fd = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666);
5107c478bd9Sstevel@tonic-gate 	if (fd < 0)
5117c478bd9Sstevel@tonic-gate 		goto open_wav_write_stream_failed;
5127c478bd9Sstevel@tonic-gate 	if (write(fd, head, PRE_DEF_WAV_HDR_LEN) != PRE_DEF_WAV_HDR_LEN) {
5137c478bd9Sstevel@tonic-gate 		goto open_wav_write_stream_failed;
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
5167c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
5177c478bd9Sstevel@tonic-gate 	h->bstr_write = file_stream_write;
5187c478bd9Sstevel@tonic-gate 	h->bstr_close = wav_write_stream_close;
5197c478bd9Sstevel@tonic-gate 	return (h);
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate open_wav_write_stream_failed:
5227c478bd9Sstevel@tonic-gate 	esav = errno;
5237c478bd9Sstevel@tonic-gate 	if (fd != -1)
5247c478bd9Sstevel@tonic-gate 		(void) close(fd);
5257c478bd9Sstevel@tonic-gate 	errno = esav;
5267c478bd9Sstevel@tonic-gate 	return (NULL);
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate bstreamhandle
5307c478bd9Sstevel@tonic-gate open_aur_write_stream(char *fname)
5317c478bd9Sstevel@tonic-gate {
5327c478bd9Sstevel@tonic-gate 	bstreamhandle h;
5337c478bd9Sstevel@tonic-gate 	int fd;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	str_errno = 0;
5367c478bd9Sstevel@tonic-gate 	fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
5377c478bd9Sstevel@tonic-gate 	if (fd < 0)
5387c478bd9Sstevel@tonic-gate 		return (NULL);
5397c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
5407c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
5417c478bd9Sstevel@tonic-gate 	h->bstr_write = file_stream_write_wrbo;
5427c478bd9Sstevel@tonic-gate 	h->bstr_close = file_stream_close;
5437c478bd9Sstevel@tonic-gate 	return (h);
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate bstreamhandle
5477c478bd9Sstevel@tonic-gate open_file_write_stream(char *fname)
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate 	bstreamhandle h;
5507c478bd9Sstevel@tonic-gate 	int fd;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	str_errno = 0;
5537c478bd9Sstevel@tonic-gate 	fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
5547c478bd9Sstevel@tonic-gate 	if (fd < 0)
5557c478bd9Sstevel@tonic-gate 		return (NULL);
5567c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
5577c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
5587c478bd9Sstevel@tonic-gate 	h->bstr_write = file_stream_write;
5597c478bd9Sstevel@tonic-gate 	h->bstr_close = file_stream_close;
5607c478bd9Sstevel@tonic-gate 	return (h);
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate bstreamhandle
5647c478bd9Sstevel@tonic-gate open_temp_file_stream(void)
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate 	bstreamhandle h;
5677c478bd9Sstevel@tonic-gate 	char *t;
5687c478bd9Sstevel@tonic-gate 	int fd;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	str_errno = 0;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	t = (char *)get_tmp_name();
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	if (strlcat(t, "/cdXXXXXX", PATH_MAX) >= PATH_MAX)
5757c478bd9Sstevel@tonic-gate 		return (NULL);
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	fd = mkstemp(t);
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	if (debug)
5807c478bd9Sstevel@tonic-gate 		(void) printf("temp is: %s length: %d\n", t, strlen(t));
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	if (fd < 0)
5837c478bd9Sstevel@tonic-gate 		return (NULL);
5847c478bd9Sstevel@tonic-gate 	(void) unlink(t);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
5877c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
5887c478bd9Sstevel@tonic-gate 	h->bstr_read = file_stream_read;
5897c478bd9Sstevel@tonic-gate 	h->bstr_write = file_stream_write;
5907c478bd9Sstevel@tonic-gate 	h->bstr_close = file_stream_close;
5917c478bd9Sstevel@tonic-gate 	h->bstr_size = file_stream_size;
5927c478bd9Sstevel@tonic-gate 	h->bstr_rewind = file_stream_rewind;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	return (h);
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate 
597416baccdSec /*
598416baccdSec  * check_avail_temp_space returns 0 if there is adequate space
599416baccdSec  * in the temporary directory, or a non-zero error code if
600416baccdSec  * something goes wrong
601416baccdSec  */
6027c478bd9Sstevel@tonic-gate int
603416baccdSec check_avail_temp_space(size_t req_size)
6047c478bd9Sstevel@tonic-gate {
6057c478bd9Sstevel@tonic-gate 	struct statvfs buf;
606*54cafc85Szk 	u_longlong_t free_size = 0;
6077c478bd9Sstevel@tonic-gate 
608416baccdSec 	if (statvfs(get_tmp_name(), &buf) < 0) {
609416baccdSec 		return (errno);
610416baccdSec 	}
6117c478bd9Sstevel@tonic-gate 
612416baccdSec 	free_size = buf.f_bfree * buf.f_frsize;
6137c478bd9Sstevel@tonic-gate 
614416baccdSec 	if (free_size <= req_size)
615416baccdSec 		return (ENOMEM);
6167c478bd9Sstevel@tonic-gate 
617416baccdSec 	return (0);
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate char *
6227c478bd9Sstevel@tonic-gate get_tmp_name(void)
6237c478bd9Sstevel@tonic-gate {
6247c478bd9Sstevel@tonic-gate 	char *t;
6257c478bd9Sstevel@tonic-gate 	char *envptr;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	t = (char *)my_zalloc(PATH_MAX);
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	/*
6307c478bd9Sstevel@tonic-gate 	 * generate temp directory path based on this order:
6317c478bd9Sstevel@tonic-gate 	 * user specified (-m option), temp env variable,
6327c478bd9Sstevel@tonic-gate 	 * and finally /tmp if nothing is found.
6337c478bd9Sstevel@tonic-gate 	 */
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	if (alt_tmp_dir) {
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 		/* copy and leave room for temp filename */
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 		(void) strlcpy(t, alt_tmp_dir, PATH_MAX - 10);
6407c478bd9Sstevel@tonic-gate 	} else {
6417c478bd9Sstevel@tonic-gate 		envptr = getenv("TMPDIR");
6427c478bd9Sstevel@tonic-gate 		if (envptr != NULL) {
6437c478bd9Sstevel@tonic-gate 			(void) strlcpy(t, envptr, PATH_MAX - 10);
6447c478bd9Sstevel@tonic-gate 		} else {
6457c478bd9Sstevel@tonic-gate 			(void) strlcpy(t, "/tmp", 5);
6467c478bd9Sstevel@tonic-gate 		}
6477c478bd9Sstevel@tonic-gate 	}
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	/*
6507c478bd9Sstevel@tonic-gate 	 * no need to check if path is valid. statvfs will catch
6517c478bd9Sstevel@tonic-gate 	 * it later and fail with a proper error message.
6527c478bd9Sstevel@tonic-gate 	 */
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	return (t);
6557c478bd9Sstevel@tonic-gate }
656