xref: /titanic_51/usr/src/cmd/cdrw/bstream.c (revision eab441e29d159ff1e25d3af8cd61798e641989b2)
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
554cafc85Szk194757  * Common Development and Distribution License (the "License").
654cafc85Szk194757  * 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 /*
2229c5e6ceSzk194757  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
25*eab441e2SMarcel Telka /*
26*eab441e2SMarcel Telka  * Copyright 2012 Marcel Telka <marcel@telka.sk>
27*eab441e2SMarcel Telka  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <fcntl.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
357c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <libintl.h>
397c478bd9Sstevel@tonic-gate #include <limits.h>
407c478bd9Sstevel@tonic-gate #include <audio/au.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "bstream.h"
437c478bd9Sstevel@tonic-gate #include "util.h"
447c478bd9Sstevel@tonic-gate #include "audio.h"
457c478bd9Sstevel@tonic-gate #include "byteorder.h"
467c478bd9Sstevel@tonic-gate #include "main.h"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate int str_errno;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate char *
517c478bd9Sstevel@tonic-gate str_errno_to_string(int serrno)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	switch (serrno) {
547c478bd9Sstevel@tonic-gate 	case STR_ERR_NO_ERR:
557c478bd9Sstevel@tonic-gate 		return (gettext("No error"));
567c478bd9Sstevel@tonic-gate 	case STR_ERR_NO_REG_FILE:
577c478bd9Sstevel@tonic-gate 		return (gettext("Not a regular file"));
587c478bd9Sstevel@tonic-gate 	case STR_ERR_NO_READ_STDIN:
597c478bd9Sstevel@tonic-gate 		return (gettext("Stdin not open for reading"));
607c478bd9Sstevel@tonic-gate 	case STR_ERR_AU_READ_ERR:
617c478bd9Sstevel@tonic-gate 		return (gettext("Unable to read au header"));
627c478bd9Sstevel@tonic-gate 	case STR_ERR_AU_UNSUPPORTED_FORMAT:
637c478bd9Sstevel@tonic-gate 		return (gettext("Unsupported au format"));
647c478bd9Sstevel@tonic-gate 	case STR_ERR_AU_BAD_HEADER:
657c478bd9Sstevel@tonic-gate 		return (gettext("Bad au header"));
667c478bd9Sstevel@tonic-gate 	case STR_ERR_WAV_READ_ERR:
677c478bd9Sstevel@tonic-gate 		return (gettext("Unable to read wav header"));
687c478bd9Sstevel@tonic-gate 	case STR_ERR_WAV_UNSUPPORTED_FORMAT:
697c478bd9Sstevel@tonic-gate 		return (gettext("Unsupported wav format"));
707c478bd9Sstevel@tonic-gate 	case STR_ERR_WAV_BAD_HEADER:
717c478bd9Sstevel@tonic-gate 		return (gettext("Bad wav header"));
7229c5e6ceSzk194757 	case STR_ERR_ISO_READ_ERR:
7329c5e6ceSzk194757 		return (gettext("Unable to read ISO header"));
7429c5e6ceSzk194757 	case STR_ERR_ISO_BAD_HEADER:
7529c5e6ceSzk194757 		return (gettext("Invalid ISO header or not an ISO"));
767c478bd9Sstevel@tonic-gate 	default:
777c478bd9Sstevel@tonic-gate 		return (gettext("unknown error"));
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static int
827c478bd9Sstevel@tonic-gate file_stream_size(bstreamhandle h, off_t *size)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	struct stat st;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	str_errno = 0;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if (fstat(h->bstr_fd, &st) < 0)
897c478bd9Sstevel@tonic-gate 		return (0);
907c478bd9Sstevel@tonic-gate 	if ((st.st_mode & S_IFMT) != S_IFREG) {
917c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_NO_REG_FILE;
927c478bd9Sstevel@tonic-gate 		return (0);
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 	*size = st.st_size;
957c478bd9Sstevel@tonic-gate 	return (1);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static int
997c478bd9Sstevel@tonic-gate audio_stream_size(bstreamhandle h, off_t *size)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	str_errno = 0;
1027c478bd9Sstevel@tonic-gate 	*size = (off_t)(uintptr_t)(h->bstr_private);
1037c478bd9Sstevel@tonic-gate 	return (1);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static int
1077c478bd9Sstevel@tonic-gate file_stream_read(bstreamhandle h, uchar_t *buf, off_t size)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	str_errno = 0;
1107c478bd9Sstevel@tonic-gate 	return (read(h->bstr_fd, buf, size));
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static int
1147c478bd9Sstevel@tonic-gate file_stream_write(bstreamhandle h, uchar_t *buf, off_t size)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	str_errno = 0;
1177c478bd9Sstevel@tonic-gate 	return (write(h->bstr_fd, buf, size));
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * with reverse byteorder
1227c478bd9Sstevel@tonic-gate  */
1237c478bd9Sstevel@tonic-gate static int
1247c478bd9Sstevel@tonic-gate file_stream_read_wrbo(bstreamhandle h, uchar_t *buf, off_t size)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	int cnt;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	str_errno = 0;
1297c478bd9Sstevel@tonic-gate 	cnt = read(h->bstr_fd, buf, size);
1307c478bd9Sstevel@tonic-gate 	if (cnt > 0) {
1317c478bd9Sstevel@tonic-gate 		int i;
1327c478bd9Sstevel@tonic-gate 		uchar_t ch;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 		for (i = 0; i < cnt; i += 2) {
1357c478bd9Sstevel@tonic-gate 			ch = buf[i];
1367c478bd9Sstevel@tonic-gate 			buf[i] = buf[i+1];
1377c478bd9Sstevel@tonic-gate 			buf[i+1] = ch;
1387c478bd9Sstevel@tonic-gate 		}
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 	return (cnt);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate  * This will change the byteorder in the buffer but that is fine with us.
1457c478bd9Sstevel@tonic-gate  */
1467c478bd9Sstevel@tonic-gate static int
1477c478bd9Sstevel@tonic-gate file_stream_write_wrbo(bstreamhandle h, uchar_t *buf, off_t size)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	int i;
1507c478bd9Sstevel@tonic-gate 	uchar_t ch;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	str_errno = 0;
1537c478bd9Sstevel@tonic-gate 	if (size > 0) {
1547c478bd9Sstevel@tonic-gate 		for (i = 0; i < size; i += 2) {
1557c478bd9Sstevel@tonic-gate 			ch = buf[i];
1567c478bd9Sstevel@tonic-gate 			buf[i] = buf[i+1];
1577c478bd9Sstevel@tonic-gate 			buf[i+1] = ch;
1587c478bd9Sstevel@tonic-gate 		}
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 	return (write(h->bstr_fd, buf, size));
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate static int
1647c478bd9Sstevel@tonic-gate file_stream_close(bstreamhandle h)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	int fd;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	str_errno = 0;
1697c478bd9Sstevel@tonic-gate 	fd = h->bstr_fd;
1707c478bd9Sstevel@tonic-gate 	free(h);
1717c478bd9Sstevel@tonic-gate 	return (close(fd));
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate static int
1757c478bd9Sstevel@tonic-gate stdin_stream_close(bstreamhandle h)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	str_errno = 0;
1787c478bd9Sstevel@tonic-gate 	free(h);
1797c478bd9Sstevel@tonic-gate 	return (0);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate static int
1837c478bd9Sstevel@tonic-gate wav_write_stream_close(bstreamhandle h)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	uint32_t sz;
1867c478bd9Sstevel@tonic-gate 	Wave_filehdr wav;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	str_errno = 0;
1897c478bd9Sstevel@tonic-gate 	(void) memset(&wav, 0, sizeof (wav));
1907c478bd9Sstevel@tonic-gate 	sz = lseek(h->bstr_fd, 0L, SEEK_END);
1917c478bd9Sstevel@tonic-gate 	(void) lseek(h->bstr_fd, 0L, SEEK_SET);
1927c478bd9Sstevel@tonic-gate 	if (read(h->bstr_fd, &wav, sizeof (wav)) != sizeof (wav)) {
1937c478bd9Sstevel@tonic-gate 		return (1);
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 	wav.total_chunk_size = CPU_TO_LE32(sz - 8);
1967c478bd9Sstevel@tonic-gate 	wav.data_size = CPU_TO_LE32(sz - 44);
1977c478bd9Sstevel@tonic-gate 	(void) lseek(h->bstr_fd, 0L, SEEK_SET);
1987c478bd9Sstevel@tonic-gate 	if (write(h->bstr_fd, &wav, sizeof (wav)) != sizeof (wav)) {
1997c478bd9Sstevel@tonic-gate 		return (1);
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 	(void) close(h->bstr_fd);
2027c478bd9Sstevel@tonic-gate 	free(h);
2037c478bd9Sstevel@tonic-gate 	return (0);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate static int
2077c478bd9Sstevel@tonic-gate au_write_stream_close(bstreamhandle h)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	uint32_t sz;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	str_errno = 0;
2127c478bd9Sstevel@tonic-gate 	sz = lseek(h->bstr_fd, 0L, SEEK_END);
2137c478bd9Sstevel@tonic-gate 	sz -= PRE_DEF_AU_HDR_LEN;
2147c478bd9Sstevel@tonic-gate 	sz = CPU_TO_BE32(sz);
2157c478bd9Sstevel@tonic-gate 	if (lseek(h->bstr_fd, 8L, SEEK_SET) < 0)
2167c478bd9Sstevel@tonic-gate 		return (1);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	if (write(h->bstr_fd, &sz, 4) < 0)
2197c478bd9Sstevel@tonic-gate 		return (1);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	(void) close(h->bstr_fd);
2227c478bd9Sstevel@tonic-gate 	free(h);
2237c478bd9Sstevel@tonic-gate 	return (0);
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /* ARGSUSED */
2277c478bd9Sstevel@tonic-gate static void
2287c478bd9Sstevel@tonic-gate stdin_stream_rewind(bstreamhandle h)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate static void
2337c478bd9Sstevel@tonic-gate file_stream_rewind(bstreamhandle h)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	(void) lseek(h->bstr_fd, 0L, SEEK_SET);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate static void
2397c478bd9Sstevel@tonic-gate au_stream_rewind(bstreamhandle h)
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate 	au_filehdr_t au;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	(void) lseek(h->bstr_fd, 0L, SEEK_SET);
2447c478bd9Sstevel@tonic-gate 	if (read(h->bstr_fd, &au, sizeof (au)) != sizeof (au)) {
2457c478bd9Sstevel@tonic-gate 		return;
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (lseek(h->bstr_fd, (long)(BE32_TO_CPU(au.au_offset)),
2497c478bd9Sstevel@tonic-gate 	    SEEK_SET) < 0) {
2507c478bd9Sstevel@tonic-gate 		return;
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate static void
2557c478bd9Sstevel@tonic-gate wav_stream_rewind(bstreamhandle h)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	(void) lseek(h->bstr_fd, (long)(sizeof (Wave_filehdr)), SEEK_SET);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate bstreamhandle
2617c478bd9Sstevel@tonic-gate open_file_read_stream(char *file)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	bstreamhandle h;
2647c478bd9Sstevel@tonic-gate 	int fd;
2657c478bd9Sstevel@tonic-gate 	struct stat st;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	str_errno = 0;
2687c478bd9Sstevel@tonic-gate 	if (stat(file, &st) < 0)
2697c478bd9Sstevel@tonic-gate 		return (NULL);
2707c478bd9Sstevel@tonic-gate 	if ((st.st_mode & S_IFMT) == S_IFDIR) {
2717c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_NO_REG_FILE;
2727c478bd9Sstevel@tonic-gate 		return (NULL);
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 	fd = open(file, O_RDONLY);
2757c478bd9Sstevel@tonic-gate 	if (fd < 0)
2767c478bd9Sstevel@tonic-gate 		return (NULL);
2777c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
2787c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
2797c478bd9Sstevel@tonic-gate 	h->bstr_read = file_stream_read;
2807c478bd9Sstevel@tonic-gate 	h->bstr_close = file_stream_close;
2817c478bd9Sstevel@tonic-gate 	h->bstr_size = file_stream_size;
2827c478bd9Sstevel@tonic-gate 	h->bstr_rewind = file_stream_rewind;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	return (h);
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate bstreamhandle
28829c5e6ceSzk194757 open_iso_read_stream(char *fname)
28929c5e6ceSzk194757 {
29029c5e6ceSzk194757 	bstreamhandle h;
29129c5e6ceSzk194757 	off_t iso_size = 0;
29229c5e6ceSzk194757 	char iso_desc[ISO9660_PRIMARY_DESC_SIZE];
29329c5e6ceSzk194757 
29429c5e6ceSzk194757 	h = open_file_read_stream(fname);
29529c5e6ceSzk194757 
29629c5e6ceSzk194757 	/* If we don't have a valid handle immediately return NULL */
29729c5e6ceSzk194757 	if (h == NULL)
29829c5e6ceSzk194757 		return (NULL);
29929c5e6ceSzk194757 
30029c5e6ceSzk194757 	if (debug)
301*eab441e2SMarcel Telka 		(void) printf("Checking the ISO file header\n");
30229c5e6ceSzk194757 
30329c5e6ceSzk194757 	/* Check to see if we have a valid sized ISO image */
30429c5e6ceSzk194757 	h->bstr_size(h, &iso_size);
30529c5e6ceSzk194757 	if (iso_size < ISO9660_HEADER_SIZE) {
30629c5e6ceSzk194757 		if (debug)
307*eab441e2SMarcel Telka 			(void) printf("ISO header size not sane.\n");
30829c5e6ceSzk194757 		h->bstr_close(h);
30929c5e6ceSzk194757 		str_errno = STR_ERR_ISO_BAD_HEADER;
31029c5e6ceSzk194757 		return (NULL);
31129c5e6ceSzk194757 	}
31229c5e6ceSzk194757 
31329c5e6ceSzk194757 	if (debug)
314*eab441e2SMarcel Telka 		(void) printf("ISO header size is sane.\n");
31529c5e6ceSzk194757 
31629c5e6ceSzk194757 	/* Skip over the boot block sector of the ISO. */
31729c5e6ceSzk194757 	(void) lseek(h->bstr_fd, ISO9660_BOOT_BLOCK_SIZE, SEEK_SET);
31829c5e6ceSzk194757 
31929c5e6ceSzk194757 	/*
32029c5e6ceSzk194757 	 * Try to read in the ISO Descriptor and validate this
321*eab441e2SMarcel Telka 	 * is in fact an ISO image.
32229c5e6ceSzk194757 	 */
32329c5e6ceSzk194757 	if (read(h->bstr_fd, iso_desc, ISO9660_PRIMARY_DESC_SIZE) ==
32429c5e6ceSzk194757 	    ISO9660_PRIMARY_DESC_SIZE) {
32529c5e6ceSzk194757 		/*
326*eab441e2SMarcel Telka 		 * Bytes one through five of a valid image should contain:
327*eab441e2SMarcel Telka 		 * - BEA01 (ISO 13490 image)
328*eab441e2SMarcel Telka 		 * - CD001 (ISO 9660 or ISO 13490 image)
329*eab441e2SMarcel Telka 		 * - CDROM (High Sierra format, the ISO 9660 predecessor)
330*eab441e2SMarcel Telka 		 * If neither is the case then we should close the stream,
331*eab441e2SMarcel Telka 		 * set str_errno, and return NULL.
33229c5e6ceSzk194757 		 */
333*eab441e2SMarcel Telka 		if (strncmp(iso_desc + ISO9660_STD_IDENT_OFFSET, "BEA01",
33429c5e6ceSzk194757 		    5) != 0 && strncmp(iso_desc + ISO9660_STD_IDENT_OFFSET,
335*eab441e2SMarcel Telka 		    "CD001", 5) != 0 && strncmp(iso_desc +
336*eab441e2SMarcel Telka 		    ISO9660_STD_IDENT_OFFSET, "CDROM", 5) != 0) {
33729c5e6ceSzk194757 			if (debug)
338*eab441e2SMarcel Telka 				(void) printf("Invalid ISO identifier.\n");
33929c5e6ceSzk194757 			h->bstr_close(h);
34029c5e6ceSzk194757 			str_errno = STR_ERR_ISO_BAD_HEADER;
34129c5e6ceSzk194757 			return (NULL);
34229c5e6ceSzk194757 		}
34329c5e6ceSzk194757 	} else {
34429c5e6ceSzk194757 		h->bstr_close(h);
34529c5e6ceSzk194757 		str_errno = STR_ERR_ISO_READ_ERR;
34629c5e6ceSzk194757 		return (NULL);
34729c5e6ceSzk194757 	}
34829c5e6ceSzk194757 
34929c5e6ceSzk194757 	/*
35029c5e6ceSzk194757 	 * Our ISO image is valid rewind the stream
35129c5e6ceSzk194757 	 * and return the handle.
35229c5e6ceSzk194757 	 */
35329c5e6ceSzk194757 	if (debug)
354*eab441e2SMarcel Telka 		(void) printf("ISO header is sane.\n");
35529c5e6ceSzk194757 	h->bstr_rewind(h);
35629c5e6ceSzk194757 	return (h);
35729c5e6ceSzk194757 }
35829c5e6ceSzk194757 
35929c5e6ceSzk194757 bstreamhandle
3607c478bd9Sstevel@tonic-gate open_stdin_read_stream(void)
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate 	bstreamhandle h;
3637c478bd9Sstevel@tonic-gate 	int mode;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	str_errno = 0;
3667c478bd9Sstevel@tonic-gate 	if ((mode = fcntl(0, F_GETFD, NULL)) < 0) {
3677c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_NO_READ_STDIN;
3687c478bd9Sstevel@tonic-gate 		return (NULL);
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 	mode &= 3;
3717c478bd9Sstevel@tonic-gate 	if ((mode != O_RDONLY) && (mode != O_RDWR)) {
3727c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_NO_READ_STDIN;
3737c478bd9Sstevel@tonic-gate 		return (NULL);
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
3767c478bd9Sstevel@tonic-gate 	h->bstr_fd = 0;
3777c478bd9Sstevel@tonic-gate 	h->bstr_read = file_stream_read;
3787c478bd9Sstevel@tonic-gate 	h->bstr_close = stdin_stream_close;
3797c478bd9Sstevel@tonic-gate 	h->bstr_size = file_stream_size;
3807c478bd9Sstevel@tonic-gate 	h->bstr_rewind = stdin_stream_rewind;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	return (h);
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate bstreamhandle
3867c478bd9Sstevel@tonic-gate open_au_read_stream(char *fname)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	bstreamhandle h;
3897c478bd9Sstevel@tonic-gate 	int fd, sav;
3907c478bd9Sstevel@tonic-gate 	au_filehdr_t *au;
3917c478bd9Sstevel@tonic-gate 	struct stat st;
3927c478bd9Sstevel@tonic-gate 	uint32_t data_size;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	au = NULL;
3957c478bd9Sstevel@tonic-gate 	str_errno = 0;
3967c478bd9Sstevel@tonic-gate 	fd = open(fname, O_RDONLY);
3977c478bd9Sstevel@tonic-gate 	if (fd < 0)
3987c478bd9Sstevel@tonic-gate 		return (NULL);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	if (fstat(fd, &st) < 0) {
4017c478bd9Sstevel@tonic-gate 		goto au_open_failed;
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate 	if ((st.st_mode & S_IFMT) != S_IFREG) {
4047c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_NO_REG_FILE;
4057c478bd9Sstevel@tonic-gate 		goto au_open_failed;
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 	au = (au_filehdr_t *)my_zalloc(sizeof (*au));
4087c478bd9Sstevel@tonic-gate 	if (read(fd, au, sizeof (*au)) != sizeof (*au)) {
4097c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_AU_READ_ERR;
4107c478bd9Sstevel@tonic-gate 		goto au_open_failed;
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate 	au->au_magic = BE32_TO_CPU(au->au_magic);
4137c478bd9Sstevel@tonic-gate 	au->au_offset = BE32_TO_CPU(au->au_offset);
4147c478bd9Sstevel@tonic-gate 	au->au_data_size = BE32_TO_CPU(au->au_data_size);
4157c478bd9Sstevel@tonic-gate 	au->au_encoding = BE32_TO_CPU(au->au_encoding);
4167c478bd9Sstevel@tonic-gate 	au->au_sample_rate = BE32_TO_CPU(au->au_sample_rate);
4177c478bd9Sstevel@tonic-gate 	au->au_channels = BE32_TO_CPU(au->au_channels);
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	if (au->au_magic != AUDIO_AU_FILE_MAGIC) {
4207c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_AU_BAD_HEADER;
4217c478bd9Sstevel@tonic-gate 		goto au_open_failed;
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 	if ((au->au_encoding != AUDIO_AU_ENCODING_LINEAR_16) ||
4247c478bd9Sstevel@tonic-gate 	    (au->au_sample_rate != 44100) || (au->au_channels != 2)) {
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_AU_UNSUPPORTED_FORMAT;
4277c478bd9Sstevel@tonic-gate 		goto au_open_failed;
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 	if (au->au_data_size != AUDIO_AU_UNKNOWN_SIZE) {
4307c478bd9Sstevel@tonic-gate 		if ((au->au_offset + au->au_data_size) != st.st_size) {
4317c478bd9Sstevel@tonic-gate 			str_errno = STR_ERR_AU_BAD_HEADER;
4327c478bd9Sstevel@tonic-gate 			goto au_open_failed;
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 		data_size = au->au_data_size;
4357c478bd9Sstevel@tonic-gate 	} else {
4367c478bd9Sstevel@tonic-gate 		data_size = st.st_size - au->au_offset;
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 	if (data_size == 0) {
4397c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_AU_UNSUPPORTED_FORMAT;
4407c478bd9Sstevel@tonic-gate 		goto au_open_failed;
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 	if (lseek(fd, au->au_offset, SEEK_SET) < 0) {
4437c478bd9Sstevel@tonic-gate 		goto au_open_failed;
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	free(au);
4477c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
4487c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
4497c478bd9Sstevel@tonic-gate 	h->bstr_read = file_stream_read_wrbo;
4507c478bd9Sstevel@tonic-gate 	h->bstr_close = file_stream_close;
4517c478bd9Sstevel@tonic-gate 	h->bstr_size = audio_stream_size;
4527c478bd9Sstevel@tonic-gate 	h->bstr_rewind = au_stream_rewind;
4537c478bd9Sstevel@tonic-gate 	h->bstr_private = (void *)data_size;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	return (h);
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate au_open_failed:
4587c478bd9Sstevel@tonic-gate 	sav = errno;
4597c478bd9Sstevel@tonic-gate 	(void) close(fd);
4607c478bd9Sstevel@tonic-gate 	if (au != NULL)
4617c478bd9Sstevel@tonic-gate 		free(au);
4627c478bd9Sstevel@tonic-gate 	errno = sav;
4637c478bd9Sstevel@tonic-gate 	return (NULL);
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate bstreamhandle
4677c478bd9Sstevel@tonic-gate open_wav_read_stream(char *fname)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate 	bstreamhandle h;
4707c478bd9Sstevel@tonic-gate 	int fd, sav;
4717c478bd9Sstevel@tonic-gate 	Wave_filehdr *wav;
4727c478bd9Sstevel@tonic-gate 	struct stat st;
4737c478bd9Sstevel@tonic-gate 	uint32_t data_size;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	wav = NULL;
4767c478bd9Sstevel@tonic-gate 	str_errno = 0;
4777c478bd9Sstevel@tonic-gate 	fd = open(fname, O_RDONLY);
4787c478bd9Sstevel@tonic-gate 	if (fd < 0)
4797c478bd9Sstevel@tonic-gate 		return (NULL);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	if (fstat(fd, &st) < 0) {
4827c478bd9Sstevel@tonic-gate 		goto wav_open_failed;
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 	if ((st.st_mode & S_IFMT) != S_IFREG) {
4857c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_NO_REG_FILE;
4867c478bd9Sstevel@tonic-gate 		goto wav_open_failed;
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 	wav = (Wave_filehdr *)my_zalloc(sizeof (*wav));
4897c478bd9Sstevel@tonic-gate 	if (read(fd, wav, sizeof (*wav)) != sizeof (*wav)) {
4907c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_WAV_READ_ERR;
4917c478bd9Sstevel@tonic-gate 		goto wav_open_failed;
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate 	if ((strncmp(wav->riff, "RIFF", 4) != 0) ||
4947c478bd9Sstevel@tonic-gate 	    (strncmp(wav->wave, "WAVE", 4) != 0)) {
4957c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_WAV_BAD_HEADER;
4967c478bd9Sstevel@tonic-gate 		goto wav_open_failed;
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 	if (((CPU_TO_LE32(wav->total_chunk_size) + 8) != st.st_size) ||
4997c478bd9Sstevel@tonic-gate 	    (strncmp(wav->fmt, "fmt ", 4) != 0) ||
5007c478bd9Sstevel@tonic-gate 	    (CPU_TO_LE16(wav->fmt_tag) != 1) ||
5017c478bd9Sstevel@tonic-gate 	    (CPU_TO_LE16(wav->n_channels) != 2) ||
5027c478bd9Sstevel@tonic-gate 	    (CPU_TO_LE32(wav->sample_rate) != 44100) ||
5037c478bd9Sstevel@tonic-gate 	    (CPU_TO_LE16(wav->bits_per_sample) != 16) ||
5047c478bd9Sstevel@tonic-gate 	    (strncmp(wav->data, "data", 4) != 0) ||
5057c478bd9Sstevel@tonic-gate 	    ((CPU_TO_LE32(wav->data_size) + 44) != st.st_size)) {
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_WAV_UNSUPPORTED_FORMAT;
5087c478bd9Sstevel@tonic-gate 		goto wav_open_failed;
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 	data_size = CPU_TO_LE32(wav->data_size);
5117c478bd9Sstevel@tonic-gate 	if (lseek(fd, sizeof (*wav), SEEK_SET) < 0) {
5127c478bd9Sstevel@tonic-gate 		goto wav_open_failed;
5137c478bd9Sstevel@tonic-gate 	}
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	free(wav);
5167c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
5177c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
5187c478bd9Sstevel@tonic-gate 	h->bstr_read = file_stream_read;
5197c478bd9Sstevel@tonic-gate 	h->bstr_close = file_stream_close;
5207c478bd9Sstevel@tonic-gate 	h->bstr_size = audio_stream_size;
5217c478bd9Sstevel@tonic-gate 	h->bstr_rewind = wav_stream_rewind;
5227c478bd9Sstevel@tonic-gate 	h->bstr_private = (void *)data_size;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	return (h);
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate wav_open_failed:
5277c478bd9Sstevel@tonic-gate 	sav = errno;
5287c478bd9Sstevel@tonic-gate 	(void) close(fd);
5297c478bd9Sstevel@tonic-gate 	if (wav != NULL)
5307c478bd9Sstevel@tonic-gate 		free(wav);
5317c478bd9Sstevel@tonic-gate 	errno = sav;
5327c478bd9Sstevel@tonic-gate 	return (NULL);
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate bstreamhandle
5367c478bd9Sstevel@tonic-gate open_aur_read_stream(char *fname)
5377c478bd9Sstevel@tonic-gate {
5387c478bd9Sstevel@tonic-gate 	bstreamhandle h;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	h = open_file_read_stream(fname);
5417c478bd9Sstevel@tonic-gate 	if (h != NULL) {
5427c478bd9Sstevel@tonic-gate 		h->bstr_read = file_stream_read_wrbo;
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 	return (h);
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate bstreamhandle
5487c478bd9Sstevel@tonic-gate open_au_write_stream(char *fname)
5497c478bd9Sstevel@tonic-gate {
5507c478bd9Sstevel@tonic-gate 	bstreamhandle h;
5517c478bd9Sstevel@tonic-gate 	int esav, fd;
5527c478bd9Sstevel@tonic-gate 	uchar_t head[] = PRE_DEF_AU_HDR;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	str_errno = 0;
5557c478bd9Sstevel@tonic-gate 	fd = -1;
5567c478bd9Sstevel@tonic-gate 	/* O_RDWR because we need to read while closing */
5577c478bd9Sstevel@tonic-gate 	fd = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666);
5587c478bd9Sstevel@tonic-gate 	if (fd < 0)
5597c478bd9Sstevel@tonic-gate 		goto open_au_write_stream_failed;
5607c478bd9Sstevel@tonic-gate 	if (write(fd, head, PRE_DEF_AU_HDR_LEN) != PRE_DEF_AU_HDR_LEN) {
5617c478bd9Sstevel@tonic-gate 		goto open_au_write_stream_failed;
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
5647c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
5657c478bd9Sstevel@tonic-gate 	h->bstr_write = file_stream_write_wrbo;
5667c478bd9Sstevel@tonic-gate 	h->bstr_close = au_write_stream_close;
5677c478bd9Sstevel@tonic-gate 	return (h);
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate open_au_write_stream_failed:
5707c478bd9Sstevel@tonic-gate 	esav = errno;
5717c478bd9Sstevel@tonic-gate 	if (fd != -1)
5727c478bd9Sstevel@tonic-gate 		(void) close(fd);
5737c478bd9Sstevel@tonic-gate 	errno = esav;
5747c478bd9Sstevel@tonic-gate 	return (NULL);
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate bstreamhandle
5787c478bd9Sstevel@tonic-gate open_wav_write_stream(char *fname)
5797c478bd9Sstevel@tonic-gate {
5807c478bd9Sstevel@tonic-gate 	bstreamhandle h;
5817c478bd9Sstevel@tonic-gate 	int esav, fd;
5827c478bd9Sstevel@tonic-gate 	uchar_t head[] = PRE_DEF_WAV_HDR;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	str_errno = 0;
5857c478bd9Sstevel@tonic-gate 	fd = -1;
5867c478bd9Sstevel@tonic-gate 	fd = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666);
5877c478bd9Sstevel@tonic-gate 	if (fd < 0)
5887c478bd9Sstevel@tonic-gate 		goto open_wav_write_stream_failed;
5897c478bd9Sstevel@tonic-gate 	if (write(fd, head, PRE_DEF_WAV_HDR_LEN) != PRE_DEF_WAV_HDR_LEN) {
5907c478bd9Sstevel@tonic-gate 		goto open_wav_write_stream_failed;
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
5937c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
5947c478bd9Sstevel@tonic-gate 	h->bstr_write = file_stream_write;
5957c478bd9Sstevel@tonic-gate 	h->bstr_close = wav_write_stream_close;
5967c478bd9Sstevel@tonic-gate 	return (h);
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate open_wav_write_stream_failed:
5997c478bd9Sstevel@tonic-gate 	esav = errno;
6007c478bd9Sstevel@tonic-gate 	if (fd != -1)
6017c478bd9Sstevel@tonic-gate 		(void) close(fd);
6027c478bd9Sstevel@tonic-gate 	errno = esav;
6037c478bd9Sstevel@tonic-gate 	return (NULL);
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate bstreamhandle
6077c478bd9Sstevel@tonic-gate open_aur_write_stream(char *fname)
6087c478bd9Sstevel@tonic-gate {
6097c478bd9Sstevel@tonic-gate 	bstreamhandle h;
6107c478bd9Sstevel@tonic-gate 	int fd;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	str_errno = 0;
6137c478bd9Sstevel@tonic-gate 	fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
6147c478bd9Sstevel@tonic-gate 	if (fd < 0)
6157c478bd9Sstevel@tonic-gate 		return (NULL);
6167c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
6177c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
6187c478bd9Sstevel@tonic-gate 	h->bstr_write = file_stream_write_wrbo;
6197c478bd9Sstevel@tonic-gate 	h->bstr_close = file_stream_close;
6207c478bd9Sstevel@tonic-gate 	return (h);
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate bstreamhandle
6247c478bd9Sstevel@tonic-gate open_file_write_stream(char *fname)
6257c478bd9Sstevel@tonic-gate {
6267c478bd9Sstevel@tonic-gate 	bstreamhandle h;
6277c478bd9Sstevel@tonic-gate 	int fd;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	str_errno = 0;
6307c478bd9Sstevel@tonic-gate 	fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
6317c478bd9Sstevel@tonic-gate 	if (fd < 0)
6327c478bd9Sstevel@tonic-gate 		return (NULL);
6337c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
6347c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
6357c478bd9Sstevel@tonic-gate 	h->bstr_write = file_stream_write;
6367c478bd9Sstevel@tonic-gate 	h->bstr_close = file_stream_close;
6377c478bd9Sstevel@tonic-gate 	return (h);
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate bstreamhandle
6417c478bd9Sstevel@tonic-gate open_temp_file_stream(void)
6427c478bd9Sstevel@tonic-gate {
6437c478bd9Sstevel@tonic-gate 	bstreamhandle h;
6447c478bd9Sstevel@tonic-gate 	char *t;
6457c478bd9Sstevel@tonic-gate 	int fd;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	str_errno = 0;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	t = (char *)get_tmp_name();
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	if (strlcat(t, "/cdXXXXXX", PATH_MAX) >= PATH_MAX)
6527c478bd9Sstevel@tonic-gate 		return (NULL);
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	fd = mkstemp(t);
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	if (debug)
6577c478bd9Sstevel@tonic-gate 		(void) printf("temp is: %s length: %d\n", t, strlen(t));
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	if (fd < 0)
6607c478bd9Sstevel@tonic-gate 		return (NULL);
6617c478bd9Sstevel@tonic-gate 	(void) unlink(t);
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
6647c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
6657c478bd9Sstevel@tonic-gate 	h->bstr_read = file_stream_read;
6667c478bd9Sstevel@tonic-gate 	h->bstr_write = file_stream_write;
6677c478bd9Sstevel@tonic-gate 	h->bstr_close = file_stream_close;
6687c478bd9Sstevel@tonic-gate 	h->bstr_size = file_stream_size;
6697c478bd9Sstevel@tonic-gate 	h->bstr_rewind = file_stream_rewind;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	return (h);
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate 
674416baccdSec158148 /*
675416baccdSec158148  * check_avail_temp_space returns 0 if there is adequate space
676416baccdSec158148  * in the temporary directory, or a non-zero error code if
677416baccdSec158148  * something goes wrong
678416baccdSec158148  */
6797c478bd9Sstevel@tonic-gate int
680416baccdSec158148 check_avail_temp_space(size_t req_size)
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate 	struct statvfs buf;
68354cafc85Szk194757 	u_longlong_t free_size = 0;
6847c478bd9Sstevel@tonic-gate 
685416baccdSec158148 	if (statvfs(get_tmp_name(), &buf) < 0) {
686416baccdSec158148 		return (errno);
687416baccdSec158148 	}
6887c478bd9Sstevel@tonic-gate 
689416baccdSec158148 	free_size = buf.f_bfree * buf.f_frsize;
6907c478bd9Sstevel@tonic-gate 
691416baccdSec158148 	if (free_size <= req_size)
692416baccdSec158148 		return (ENOMEM);
693416baccdSec158148 
6947c478bd9Sstevel@tonic-gate 	return (0);
6957c478bd9Sstevel@tonic-gate }
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate char *
6997c478bd9Sstevel@tonic-gate get_tmp_name(void)
7007c478bd9Sstevel@tonic-gate {
7017c478bd9Sstevel@tonic-gate 	char *t;
7027c478bd9Sstevel@tonic-gate 	char *envptr;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	t = (char *)my_zalloc(PATH_MAX);
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	/*
7077c478bd9Sstevel@tonic-gate 	 * generate temp directory path based on this order:
7087c478bd9Sstevel@tonic-gate 	 * user specified (-m option), temp env variable,
7097c478bd9Sstevel@tonic-gate 	 * and finally /tmp if nothing is found.
7107c478bd9Sstevel@tonic-gate 	 */
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	if (alt_tmp_dir) {
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 		/* copy and leave room for temp filename */
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 		(void) strlcpy(t, alt_tmp_dir, PATH_MAX - 10);
7177c478bd9Sstevel@tonic-gate 	} else {
7187c478bd9Sstevel@tonic-gate 		envptr = getenv("TMPDIR");
7197c478bd9Sstevel@tonic-gate 		if (envptr != NULL) {
7207c478bd9Sstevel@tonic-gate 			(void) strlcpy(t, envptr, PATH_MAX - 10);
7217c478bd9Sstevel@tonic-gate 		} else {
7227c478bd9Sstevel@tonic-gate 			(void) strlcpy(t, "/tmp", 5);
7237c478bd9Sstevel@tonic-gate 		}
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	/*
7277c478bd9Sstevel@tonic-gate 	 * no need to check if path is valid. statvfs will catch
7287c478bd9Sstevel@tonic-gate 	 * it later and fail with a proper error message.
7297c478bd9Sstevel@tonic-gate 	 */
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	return (t);
7327c478bd9Sstevel@tonic-gate }
733