xref: /illumos-gate/usr/src/cmd/cdrw/bstream.c (revision 29c5e6ce)
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
554cafc85Szk  * Common Development and Distribution License (the "License").
654cafc85Szk  * 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*29c5e6ceSzk  * Copyright 2007 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"));
71*29c5e6ceSzk 	case STR_ERR_ISO_READ_ERR:
72*29c5e6ceSzk 		return (gettext("Unable to read ISO header"));
73*29c5e6ceSzk 	case STR_ERR_ISO_BAD_HEADER:
74*29c5e6ceSzk 		return (gettext("Invalid ISO header or not an ISO"));
757c478bd9Sstevel@tonic-gate 	default:
767c478bd9Sstevel@tonic-gate 		return (gettext("unknown error"));
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate static int
817c478bd9Sstevel@tonic-gate file_stream_size(bstreamhandle h, off_t *size)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	struct stat st;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	str_errno = 0;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if (fstat(h->bstr_fd, &st) < 0)
887c478bd9Sstevel@tonic-gate 		return (0);
897c478bd9Sstevel@tonic-gate 	if ((st.st_mode & S_IFMT) != S_IFREG) {
907c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_NO_REG_FILE;
917c478bd9Sstevel@tonic-gate 		return (0);
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 	*size = st.st_size;
947c478bd9Sstevel@tonic-gate 	return (1);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static int
987c478bd9Sstevel@tonic-gate audio_stream_size(bstreamhandle h, off_t *size)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	str_errno = 0;
1017c478bd9Sstevel@tonic-gate 	*size = (off_t)(uintptr_t)(h->bstr_private);
1027c478bd9Sstevel@tonic-gate 	return (1);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static int
1067c478bd9Sstevel@tonic-gate file_stream_read(bstreamhandle h, uchar_t *buf, off_t size)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	str_errno = 0;
1097c478bd9Sstevel@tonic-gate 	return (read(h->bstr_fd, buf, size));
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate static int
1137c478bd9Sstevel@tonic-gate file_stream_write(bstreamhandle h, uchar_t *buf, off_t size)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	str_errno = 0;
1167c478bd9Sstevel@tonic-gate 	return (write(h->bstr_fd, buf, size));
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * with reverse byteorder
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate static int
1237c478bd9Sstevel@tonic-gate file_stream_read_wrbo(bstreamhandle h, uchar_t *buf, off_t size)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	int cnt;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	str_errno = 0;
1287c478bd9Sstevel@tonic-gate 	cnt = read(h->bstr_fd, buf, size);
1297c478bd9Sstevel@tonic-gate 	if (cnt > 0) {
1307c478bd9Sstevel@tonic-gate 		int i;
1317c478bd9Sstevel@tonic-gate 		uchar_t ch;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 		for (i = 0; i < cnt; i += 2) {
1347c478bd9Sstevel@tonic-gate 			ch = buf[i];
1357c478bd9Sstevel@tonic-gate 			buf[i] = buf[i+1];
1367c478bd9Sstevel@tonic-gate 			buf[i+1] = ch;
1377c478bd9Sstevel@tonic-gate 		}
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 	return (cnt);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * This will change the byteorder in the buffer but that is fine with us.
1447c478bd9Sstevel@tonic-gate  */
1457c478bd9Sstevel@tonic-gate static int
1467c478bd9Sstevel@tonic-gate file_stream_write_wrbo(bstreamhandle h, uchar_t *buf, off_t size)
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate 	int i;
1497c478bd9Sstevel@tonic-gate 	uchar_t ch;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	str_errno = 0;
1527c478bd9Sstevel@tonic-gate 	if (size > 0) {
1537c478bd9Sstevel@tonic-gate 		for (i = 0; i < size; i += 2) {
1547c478bd9Sstevel@tonic-gate 			ch = buf[i];
1557c478bd9Sstevel@tonic-gate 			buf[i] = buf[i+1];
1567c478bd9Sstevel@tonic-gate 			buf[i+1] = ch;
1577c478bd9Sstevel@tonic-gate 		}
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 	return (write(h->bstr_fd, buf, size));
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate static int
1637c478bd9Sstevel@tonic-gate file_stream_close(bstreamhandle h)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	int fd;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	str_errno = 0;
1687c478bd9Sstevel@tonic-gate 	fd = h->bstr_fd;
1697c478bd9Sstevel@tonic-gate 	free(h);
1707c478bd9Sstevel@tonic-gate 	return (close(fd));
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate static int
1747c478bd9Sstevel@tonic-gate stdin_stream_close(bstreamhandle h)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	str_errno = 0;
1777c478bd9Sstevel@tonic-gate 	free(h);
1787c478bd9Sstevel@tonic-gate 	return (0);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate static int
1827c478bd9Sstevel@tonic-gate wav_write_stream_close(bstreamhandle h)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	uint32_t sz;
1857c478bd9Sstevel@tonic-gate 	Wave_filehdr wav;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	str_errno = 0;
1887c478bd9Sstevel@tonic-gate 	(void) memset(&wav, 0, sizeof (wav));
1897c478bd9Sstevel@tonic-gate 	sz = lseek(h->bstr_fd, 0L, SEEK_END);
1907c478bd9Sstevel@tonic-gate 	(void) lseek(h->bstr_fd, 0L, SEEK_SET);
1917c478bd9Sstevel@tonic-gate 	if (read(h->bstr_fd, &wav, sizeof (wav)) != sizeof (wav)) {
1927c478bd9Sstevel@tonic-gate 		return (1);
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 	wav.total_chunk_size = CPU_TO_LE32(sz - 8);
1957c478bd9Sstevel@tonic-gate 	wav.data_size = CPU_TO_LE32(sz - 44);
1967c478bd9Sstevel@tonic-gate 	(void) lseek(h->bstr_fd, 0L, SEEK_SET);
1977c478bd9Sstevel@tonic-gate 	if (write(h->bstr_fd, &wav, sizeof (wav)) != sizeof (wav)) {
1987c478bd9Sstevel@tonic-gate 		return (1);
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 	(void) close(h->bstr_fd);
2017c478bd9Sstevel@tonic-gate 	free(h);
2027c478bd9Sstevel@tonic-gate 	return (0);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate static int
2067c478bd9Sstevel@tonic-gate au_write_stream_close(bstreamhandle h)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	uint32_t sz;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	str_errno = 0;
2117c478bd9Sstevel@tonic-gate 	sz = lseek(h->bstr_fd, 0L, SEEK_END);
2127c478bd9Sstevel@tonic-gate 	sz -= PRE_DEF_AU_HDR_LEN;
2137c478bd9Sstevel@tonic-gate 	sz = CPU_TO_BE32(sz);
2147c478bd9Sstevel@tonic-gate 	if (lseek(h->bstr_fd, 8L, SEEK_SET) < 0)
2157c478bd9Sstevel@tonic-gate 		return (1);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	if (write(h->bstr_fd, &sz, 4) < 0)
2187c478bd9Sstevel@tonic-gate 		return (1);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	(void) close(h->bstr_fd);
2217c478bd9Sstevel@tonic-gate 	free(h);
2227c478bd9Sstevel@tonic-gate 	return (0);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /* ARGSUSED */
2267c478bd9Sstevel@tonic-gate static void
2277c478bd9Sstevel@tonic-gate stdin_stream_rewind(bstreamhandle h)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate static void
2327c478bd9Sstevel@tonic-gate file_stream_rewind(bstreamhandle h)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	(void) lseek(h->bstr_fd, 0L, SEEK_SET);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate static void
2387c478bd9Sstevel@tonic-gate au_stream_rewind(bstreamhandle h)
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate 	au_filehdr_t au;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	(void) lseek(h->bstr_fd, 0L, SEEK_SET);
2437c478bd9Sstevel@tonic-gate 	if (read(h->bstr_fd, &au, sizeof (au)) != sizeof (au)) {
2447c478bd9Sstevel@tonic-gate 		return;
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if (lseek(h->bstr_fd, (long)(BE32_TO_CPU(au.au_offset)),
2487c478bd9Sstevel@tonic-gate 	    SEEK_SET) < 0) {
2497c478bd9Sstevel@tonic-gate 		return;
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate static void
2547c478bd9Sstevel@tonic-gate wav_stream_rewind(bstreamhandle h)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 	(void) lseek(h->bstr_fd, (long)(sizeof (Wave_filehdr)), SEEK_SET);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate bstreamhandle
2607c478bd9Sstevel@tonic-gate open_file_read_stream(char *file)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	bstreamhandle h;
2637c478bd9Sstevel@tonic-gate 	int fd;
2647c478bd9Sstevel@tonic-gate 	struct stat st;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	str_errno = 0;
2677c478bd9Sstevel@tonic-gate 	if (stat(file, &st) < 0)
2687c478bd9Sstevel@tonic-gate 		return (NULL);
2697c478bd9Sstevel@tonic-gate 	if ((st.st_mode & S_IFMT) == S_IFDIR) {
2707c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_NO_REG_FILE;
2717c478bd9Sstevel@tonic-gate 		return (NULL);
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 	fd = open(file, O_RDONLY);
2747c478bd9Sstevel@tonic-gate 	if (fd < 0)
2757c478bd9Sstevel@tonic-gate 		return (NULL);
2767c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
2777c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
2787c478bd9Sstevel@tonic-gate 	h->bstr_read = file_stream_read;
2797c478bd9Sstevel@tonic-gate 	h->bstr_close = file_stream_close;
2807c478bd9Sstevel@tonic-gate 	h->bstr_size = file_stream_size;
2817c478bd9Sstevel@tonic-gate 	h->bstr_rewind = file_stream_rewind;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	return (h);
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate 
286*29c5e6ceSzk bstreamhandle
287*29c5e6ceSzk open_iso_read_stream(char *fname)
288*29c5e6ceSzk {
289*29c5e6ceSzk 	bstreamhandle h;
290*29c5e6ceSzk 	off_t iso_size = 0;
291*29c5e6ceSzk 	char iso_desc[ISO9660_PRIMARY_DESC_SIZE];
292*29c5e6ceSzk 
293*29c5e6ceSzk 	h = open_file_read_stream(fname);
294*29c5e6ceSzk 
295*29c5e6ceSzk 	/* If we don't have a valid handle immediately return NULL */
296*29c5e6ceSzk 	if (h == NULL)
297*29c5e6ceSzk 		return (NULL);
298*29c5e6ceSzk 
299*29c5e6ceSzk 	if (debug)
300*29c5e6ceSzk 		(void) printf("Checking the ISO 9660 file header\n");
301*29c5e6ceSzk 
302*29c5e6ceSzk 	/* Check to see if we have a valid sized ISO image */
303*29c5e6ceSzk 	h->bstr_size(h, &iso_size);
304*29c5e6ceSzk 	if (iso_size < ISO9660_HEADER_SIZE) {
305*29c5e6ceSzk 		if (debug)
306*29c5e6ceSzk 			(void) printf("ISO 9660 header size not sane.\n");
307*29c5e6ceSzk 		h->bstr_close(h);
308*29c5e6ceSzk 		str_errno = STR_ERR_ISO_BAD_HEADER;
309*29c5e6ceSzk 		return (NULL);
310*29c5e6ceSzk 	}
311*29c5e6ceSzk 
312*29c5e6ceSzk 	if (debug)
313*29c5e6ceSzk 		(void) printf("ISO 9660 header size is sane.\n");
314*29c5e6ceSzk 
315*29c5e6ceSzk 	/* Skip over the boot block sector of the ISO. */
316*29c5e6ceSzk 	(void) lseek(h->bstr_fd, ISO9660_BOOT_BLOCK_SIZE, SEEK_SET);
317*29c5e6ceSzk 
318*29c5e6ceSzk 	/*
319*29c5e6ceSzk 	 * Try to read in the ISO Descriptor and validate this
320*29c5e6ceSzk 	 * is in fact an ISO 9660 image.
321*29c5e6ceSzk 	 */
322*29c5e6ceSzk 	if (read(h->bstr_fd, iso_desc, ISO9660_PRIMARY_DESC_SIZE) ==
323*29c5e6ceSzk 	    ISO9660_PRIMARY_DESC_SIZE) {
324*29c5e6ceSzk 		/*
325*29c5e6ceSzk 		 * Bytes one through five of a valid ISO 9660 cd image
326*29c5e6ceSzk 		 * should contain the string CD001. High Sierra format,
327*29c5e6ceSzk 		 * the ISO 9660 predecessor, fills this field with the
328*29c5e6ceSzk 		 * string CDROM. If neither is the case then we should
329*29c5e6ceSzk 		 * close the stream, set str_errno, and return NULL.
330*29c5e6ceSzk 		 */
331*29c5e6ceSzk 		if (strncmp(iso_desc + ISO9660_STD_IDENT_OFFSET, "CD001",
332*29c5e6ceSzk 		    5) != 0 && strncmp(iso_desc + ISO9660_STD_IDENT_OFFSET,
333*29c5e6ceSzk 		    "CDROM", 5) != 0) {
334*29c5e6ceSzk 			if (debug)
335*29c5e6ceSzk 				(void) printf("Invalid ISO 9660 identifier.\n");
336*29c5e6ceSzk 			h->bstr_close(h);
337*29c5e6ceSzk 			str_errno = STR_ERR_ISO_BAD_HEADER;
338*29c5e6ceSzk 			return (NULL);
339*29c5e6ceSzk 		}
340*29c5e6ceSzk 	} else {
341*29c5e6ceSzk 		h->bstr_close(h);
342*29c5e6ceSzk 		str_errno = STR_ERR_ISO_READ_ERR;
343*29c5e6ceSzk 		return (NULL);
344*29c5e6ceSzk 	}
345*29c5e6ceSzk 
346*29c5e6ceSzk 	/*
347*29c5e6ceSzk 	 * Our ISO image is valid rewind the stream
348*29c5e6ceSzk 	 * and return the handle.
349*29c5e6ceSzk 	 */
350*29c5e6ceSzk 	if (debug)
351*29c5e6ceSzk 		(void) printf("ISO 9660 header is sane.\n");
352*29c5e6ceSzk 	h->bstr_rewind(h);
353*29c5e6ceSzk 	return (h);
354*29c5e6ceSzk }
355*29c5e6ceSzk 
3567c478bd9Sstevel@tonic-gate bstreamhandle
3577c478bd9Sstevel@tonic-gate open_stdin_read_stream(void)
3587c478bd9Sstevel@tonic-gate {
3597c478bd9Sstevel@tonic-gate 	bstreamhandle h;
3607c478bd9Sstevel@tonic-gate 	int mode;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	str_errno = 0;
3637c478bd9Sstevel@tonic-gate 	if ((mode = fcntl(0, F_GETFD, NULL)) < 0) {
3647c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_NO_READ_STDIN;
3657c478bd9Sstevel@tonic-gate 		return (NULL);
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 	mode &= 3;
3687c478bd9Sstevel@tonic-gate 	if ((mode != O_RDONLY) && (mode != O_RDWR)) {
3697c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_NO_READ_STDIN;
3707c478bd9Sstevel@tonic-gate 		return (NULL);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
3737c478bd9Sstevel@tonic-gate 	h->bstr_fd = 0;
3747c478bd9Sstevel@tonic-gate 	h->bstr_read = file_stream_read;
3757c478bd9Sstevel@tonic-gate 	h->bstr_close = stdin_stream_close;
3767c478bd9Sstevel@tonic-gate 	h->bstr_size = file_stream_size;
3777c478bd9Sstevel@tonic-gate 	h->bstr_rewind = stdin_stream_rewind;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	return (h);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate bstreamhandle
3837c478bd9Sstevel@tonic-gate open_au_read_stream(char *fname)
3847c478bd9Sstevel@tonic-gate {
3857c478bd9Sstevel@tonic-gate 	bstreamhandle h;
3867c478bd9Sstevel@tonic-gate 	int fd, sav;
3877c478bd9Sstevel@tonic-gate 	au_filehdr_t *au;
3887c478bd9Sstevel@tonic-gate 	struct stat st;
3897c478bd9Sstevel@tonic-gate 	uint32_t data_size;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	au = NULL;
3927c478bd9Sstevel@tonic-gate 	str_errno = 0;
3937c478bd9Sstevel@tonic-gate 	fd = open(fname, O_RDONLY);
3947c478bd9Sstevel@tonic-gate 	if (fd < 0)
3957c478bd9Sstevel@tonic-gate 		return (NULL);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	if (fstat(fd, &st) < 0) {
3987c478bd9Sstevel@tonic-gate 		goto au_open_failed;
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 	if ((st.st_mode & S_IFMT) != S_IFREG) {
4017c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_NO_REG_FILE;
4027c478bd9Sstevel@tonic-gate 		goto au_open_failed;
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 	au = (au_filehdr_t *)my_zalloc(sizeof (*au));
4057c478bd9Sstevel@tonic-gate 	if (read(fd, au, sizeof (*au)) != sizeof (*au)) {
4067c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_AU_READ_ERR;
4077c478bd9Sstevel@tonic-gate 		goto au_open_failed;
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 	au->au_magic = BE32_TO_CPU(au->au_magic);
4107c478bd9Sstevel@tonic-gate 	au->au_offset = BE32_TO_CPU(au->au_offset);
4117c478bd9Sstevel@tonic-gate 	au->au_data_size = BE32_TO_CPU(au->au_data_size);
4127c478bd9Sstevel@tonic-gate 	au->au_encoding = BE32_TO_CPU(au->au_encoding);
4137c478bd9Sstevel@tonic-gate 	au->au_sample_rate = BE32_TO_CPU(au->au_sample_rate);
4147c478bd9Sstevel@tonic-gate 	au->au_channels = BE32_TO_CPU(au->au_channels);
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	if (au->au_magic != AUDIO_AU_FILE_MAGIC) {
4177c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_AU_BAD_HEADER;
4187c478bd9Sstevel@tonic-gate 		goto au_open_failed;
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate 	if ((au->au_encoding != AUDIO_AU_ENCODING_LINEAR_16) ||
4217c478bd9Sstevel@tonic-gate 	    (au->au_sample_rate != 44100) || (au->au_channels != 2)) {
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_AU_UNSUPPORTED_FORMAT;
4247c478bd9Sstevel@tonic-gate 		goto au_open_failed;
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 	if (au->au_data_size != AUDIO_AU_UNKNOWN_SIZE) {
4277c478bd9Sstevel@tonic-gate 		if ((au->au_offset + au->au_data_size) != st.st_size) {
4287c478bd9Sstevel@tonic-gate 			str_errno = STR_ERR_AU_BAD_HEADER;
4297c478bd9Sstevel@tonic-gate 			goto au_open_failed;
4307c478bd9Sstevel@tonic-gate 		}
4317c478bd9Sstevel@tonic-gate 		data_size = au->au_data_size;
4327c478bd9Sstevel@tonic-gate 	} else {
4337c478bd9Sstevel@tonic-gate 		data_size = st.st_size - au->au_offset;
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 	if (data_size == 0) {
4367c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_AU_UNSUPPORTED_FORMAT;
4377c478bd9Sstevel@tonic-gate 		goto au_open_failed;
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate 	if (lseek(fd, au->au_offset, SEEK_SET) < 0) {
4407c478bd9Sstevel@tonic-gate 		goto au_open_failed;
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	free(au);
4447c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
4457c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
4467c478bd9Sstevel@tonic-gate 	h->bstr_read = file_stream_read_wrbo;
4477c478bd9Sstevel@tonic-gate 	h->bstr_close = file_stream_close;
4487c478bd9Sstevel@tonic-gate 	h->bstr_size = audio_stream_size;
4497c478bd9Sstevel@tonic-gate 	h->bstr_rewind = au_stream_rewind;
4507c478bd9Sstevel@tonic-gate 	h->bstr_private = (void *)data_size;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	return (h);
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate au_open_failed:
4557c478bd9Sstevel@tonic-gate 	sav = errno;
4567c478bd9Sstevel@tonic-gate 	(void) close(fd);
4577c478bd9Sstevel@tonic-gate 	if (au != NULL)
4587c478bd9Sstevel@tonic-gate 		free(au);
4597c478bd9Sstevel@tonic-gate 	errno = sav;
4607c478bd9Sstevel@tonic-gate 	return (NULL);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate bstreamhandle
4647c478bd9Sstevel@tonic-gate open_wav_read_stream(char *fname)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	bstreamhandle h;
4677c478bd9Sstevel@tonic-gate 	int fd, sav;
4687c478bd9Sstevel@tonic-gate 	Wave_filehdr *wav;
4697c478bd9Sstevel@tonic-gate 	struct stat st;
4707c478bd9Sstevel@tonic-gate 	uint32_t data_size;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	wav = NULL;
4737c478bd9Sstevel@tonic-gate 	str_errno = 0;
4747c478bd9Sstevel@tonic-gate 	fd = open(fname, O_RDONLY);
4757c478bd9Sstevel@tonic-gate 	if (fd < 0)
4767c478bd9Sstevel@tonic-gate 		return (NULL);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	if (fstat(fd, &st) < 0) {
4797c478bd9Sstevel@tonic-gate 		goto wav_open_failed;
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate 	if ((st.st_mode & S_IFMT) != S_IFREG) {
4827c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_NO_REG_FILE;
4837c478bd9Sstevel@tonic-gate 		goto wav_open_failed;
4847c478bd9Sstevel@tonic-gate 	}
4857c478bd9Sstevel@tonic-gate 	wav = (Wave_filehdr *)my_zalloc(sizeof (*wav));
4867c478bd9Sstevel@tonic-gate 	if (read(fd, wav, sizeof (*wav)) != sizeof (*wav)) {
4877c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_WAV_READ_ERR;
4887c478bd9Sstevel@tonic-gate 		goto wav_open_failed;
4897c478bd9Sstevel@tonic-gate 	}
4907c478bd9Sstevel@tonic-gate 	if ((strncmp(wav->riff, "RIFF", 4) != 0) ||
4917c478bd9Sstevel@tonic-gate 		(strncmp(wav->wave, "WAVE", 4) != 0)) {
4927c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_WAV_BAD_HEADER;
4937c478bd9Sstevel@tonic-gate 		goto wav_open_failed;
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 	if (((CPU_TO_LE32(wav->total_chunk_size) + 8) != st.st_size) ||
4967c478bd9Sstevel@tonic-gate 	    (strncmp(wav->fmt, "fmt ", 4) != 0) ||
4977c478bd9Sstevel@tonic-gate 	    (CPU_TO_LE16(wav->fmt_tag) != 1) ||
4987c478bd9Sstevel@tonic-gate 	    (CPU_TO_LE16(wav->n_channels) != 2) ||
4997c478bd9Sstevel@tonic-gate 	    (CPU_TO_LE32(wav->sample_rate) != 44100) ||
5007c478bd9Sstevel@tonic-gate 	    (CPU_TO_LE16(wav->bits_per_sample) != 16) ||
5017c478bd9Sstevel@tonic-gate 	    (strncmp(wav->data, "data", 4) != 0) ||
5027c478bd9Sstevel@tonic-gate 	    ((CPU_TO_LE32(wav->data_size) + 44) != st.st_size)) {
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 		str_errno = STR_ERR_WAV_UNSUPPORTED_FORMAT;
5057c478bd9Sstevel@tonic-gate 		goto wav_open_failed;
5067c478bd9Sstevel@tonic-gate 	}
5077c478bd9Sstevel@tonic-gate 	data_size = CPU_TO_LE32(wav->data_size);
5087c478bd9Sstevel@tonic-gate 	if (lseek(fd, sizeof (*wav), SEEK_SET) < 0) {
5097c478bd9Sstevel@tonic-gate 		goto wav_open_failed;
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	free(wav);
5137c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
5147c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
5157c478bd9Sstevel@tonic-gate 	h->bstr_read = file_stream_read;
5167c478bd9Sstevel@tonic-gate 	h->bstr_close = file_stream_close;
5177c478bd9Sstevel@tonic-gate 	h->bstr_size = audio_stream_size;
5187c478bd9Sstevel@tonic-gate 	h->bstr_rewind = wav_stream_rewind;
5197c478bd9Sstevel@tonic-gate 	h->bstr_private = (void *)data_size;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	return (h);
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate wav_open_failed:
5247c478bd9Sstevel@tonic-gate 	sav = errno;
5257c478bd9Sstevel@tonic-gate 	(void) close(fd);
5267c478bd9Sstevel@tonic-gate 	if (wav != NULL)
5277c478bd9Sstevel@tonic-gate 		free(wav);
5287c478bd9Sstevel@tonic-gate 	errno = sav;
5297c478bd9Sstevel@tonic-gate 	return (NULL);
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate bstreamhandle
5337c478bd9Sstevel@tonic-gate open_aur_read_stream(char *fname)
5347c478bd9Sstevel@tonic-gate {
5357c478bd9Sstevel@tonic-gate 	bstreamhandle h;
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	h = open_file_read_stream(fname);
5387c478bd9Sstevel@tonic-gate 	if (h != NULL) {
5397c478bd9Sstevel@tonic-gate 		h->bstr_read = file_stream_read_wrbo;
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate 	return (h);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate bstreamhandle
5457c478bd9Sstevel@tonic-gate open_au_write_stream(char *fname)
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate 	bstreamhandle h;
5487c478bd9Sstevel@tonic-gate 	int esav, fd;
5497c478bd9Sstevel@tonic-gate 	uchar_t head[] = PRE_DEF_AU_HDR;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	str_errno = 0;
5527c478bd9Sstevel@tonic-gate 	fd = -1;
5537c478bd9Sstevel@tonic-gate 	/* O_RDWR because we need to read while closing */
5547c478bd9Sstevel@tonic-gate 	fd = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666);
5557c478bd9Sstevel@tonic-gate 	if (fd < 0)
5567c478bd9Sstevel@tonic-gate 		goto open_au_write_stream_failed;
5577c478bd9Sstevel@tonic-gate 	if (write(fd, head, PRE_DEF_AU_HDR_LEN) != PRE_DEF_AU_HDR_LEN) {
5587c478bd9Sstevel@tonic-gate 		goto open_au_write_stream_failed;
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
5617c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
5627c478bd9Sstevel@tonic-gate 	h->bstr_write = file_stream_write_wrbo;
5637c478bd9Sstevel@tonic-gate 	h->bstr_close = au_write_stream_close;
5647c478bd9Sstevel@tonic-gate 	return (h);
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate open_au_write_stream_failed:
5677c478bd9Sstevel@tonic-gate 	esav = errno;
5687c478bd9Sstevel@tonic-gate 	if (fd != -1)
5697c478bd9Sstevel@tonic-gate 		(void) close(fd);
5707c478bd9Sstevel@tonic-gate 	errno = esav;
5717c478bd9Sstevel@tonic-gate 	return (NULL);
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate bstreamhandle
5757c478bd9Sstevel@tonic-gate open_wav_write_stream(char *fname)
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate 	bstreamhandle h;
5787c478bd9Sstevel@tonic-gate 	int esav, fd;
5797c478bd9Sstevel@tonic-gate 	uchar_t head[] = PRE_DEF_WAV_HDR;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	str_errno = 0;
5827c478bd9Sstevel@tonic-gate 	fd = -1;
5837c478bd9Sstevel@tonic-gate 	fd = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666);
5847c478bd9Sstevel@tonic-gate 	if (fd < 0)
5857c478bd9Sstevel@tonic-gate 		goto open_wav_write_stream_failed;
5867c478bd9Sstevel@tonic-gate 	if (write(fd, head, PRE_DEF_WAV_HDR_LEN) != PRE_DEF_WAV_HDR_LEN) {
5877c478bd9Sstevel@tonic-gate 		goto open_wav_write_stream_failed;
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
5907c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
5917c478bd9Sstevel@tonic-gate 	h->bstr_write = file_stream_write;
5927c478bd9Sstevel@tonic-gate 	h->bstr_close = wav_write_stream_close;
5937c478bd9Sstevel@tonic-gate 	return (h);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate open_wav_write_stream_failed:
5967c478bd9Sstevel@tonic-gate 	esav = errno;
5977c478bd9Sstevel@tonic-gate 	if (fd != -1)
5987c478bd9Sstevel@tonic-gate 		(void) close(fd);
5997c478bd9Sstevel@tonic-gate 	errno = esav;
6007c478bd9Sstevel@tonic-gate 	return (NULL);
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate bstreamhandle
6047c478bd9Sstevel@tonic-gate open_aur_write_stream(char *fname)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	bstreamhandle h;
6077c478bd9Sstevel@tonic-gate 	int fd;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	str_errno = 0;
6107c478bd9Sstevel@tonic-gate 	fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
6117c478bd9Sstevel@tonic-gate 	if (fd < 0)
6127c478bd9Sstevel@tonic-gate 		return (NULL);
6137c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
6147c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
6157c478bd9Sstevel@tonic-gate 	h->bstr_write = file_stream_write_wrbo;
6167c478bd9Sstevel@tonic-gate 	h->bstr_close = file_stream_close;
6177c478bd9Sstevel@tonic-gate 	return (h);
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate bstreamhandle
6217c478bd9Sstevel@tonic-gate open_file_write_stream(char *fname)
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate 	bstreamhandle h;
6247c478bd9Sstevel@tonic-gate 	int fd;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	str_errno = 0;
6277c478bd9Sstevel@tonic-gate 	fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
6287c478bd9Sstevel@tonic-gate 	if (fd < 0)
6297c478bd9Sstevel@tonic-gate 		return (NULL);
6307c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
6317c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
6327c478bd9Sstevel@tonic-gate 	h->bstr_write = file_stream_write;
6337c478bd9Sstevel@tonic-gate 	h->bstr_close = file_stream_close;
6347c478bd9Sstevel@tonic-gate 	return (h);
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate bstreamhandle
6387c478bd9Sstevel@tonic-gate open_temp_file_stream(void)
6397c478bd9Sstevel@tonic-gate {
6407c478bd9Sstevel@tonic-gate 	bstreamhandle h;
6417c478bd9Sstevel@tonic-gate 	char *t;
6427c478bd9Sstevel@tonic-gate 	int fd;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	str_errno = 0;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	t = (char *)get_tmp_name();
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	if (strlcat(t, "/cdXXXXXX", PATH_MAX) >= PATH_MAX)
6497c478bd9Sstevel@tonic-gate 		return (NULL);
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	fd = mkstemp(t);
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	if (debug)
6547c478bd9Sstevel@tonic-gate 		(void) printf("temp is: %s length: %d\n", t, strlen(t));
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	if (fd < 0)
6577c478bd9Sstevel@tonic-gate 		return (NULL);
6587c478bd9Sstevel@tonic-gate 	(void) unlink(t);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	h = (bstreamhandle)my_zalloc(sizeof (*h));
6617c478bd9Sstevel@tonic-gate 	h->bstr_fd = fd;
6627c478bd9Sstevel@tonic-gate 	h->bstr_read = file_stream_read;
6637c478bd9Sstevel@tonic-gate 	h->bstr_write = file_stream_write;
6647c478bd9Sstevel@tonic-gate 	h->bstr_close = file_stream_close;
6657c478bd9Sstevel@tonic-gate 	h->bstr_size = file_stream_size;
6667c478bd9Sstevel@tonic-gate 	h->bstr_rewind = file_stream_rewind;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	return (h);
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate 
671416baccdSec /*
672416baccdSec  * check_avail_temp_space returns 0 if there is adequate space
673416baccdSec  * in the temporary directory, or a non-zero error code if
674416baccdSec  * something goes wrong
675416baccdSec  */
6767c478bd9Sstevel@tonic-gate int
677416baccdSec check_avail_temp_space(size_t req_size)
6787c478bd9Sstevel@tonic-gate {
6797c478bd9Sstevel@tonic-gate 	struct statvfs buf;
68054cafc85Szk 	u_longlong_t free_size = 0;
6817c478bd9Sstevel@tonic-gate 
682416baccdSec 	if (statvfs(get_tmp_name(), &buf) < 0) {
683416baccdSec 		return (errno);
684416baccdSec 	}
6857c478bd9Sstevel@tonic-gate 
686416baccdSec 	free_size = buf.f_bfree * buf.f_frsize;
6877c478bd9Sstevel@tonic-gate 
688416baccdSec 	if (free_size <= req_size)
689416baccdSec 		return (ENOMEM);
6907c478bd9Sstevel@tonic-gate 
691416baccdSec 	return (0);
6927c478bd9Sstevel@tonic-gate }
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate char *
6967c478bd9Sstevel@tonic-gate get_tmp_name(void)
6977c478bd9Sstevel@tonic-gate {
6987c478bd9Sstevel@tonic-gate 	char *t;
6997c478bd9Sstevel@tonic-gate 	char *envptr;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	t = (char *)my_zalloc(PATH_MAX);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	/*
7047c478bd9Sstevel@tonic-gate 	 * generate temp directory path based on this order:
7057c478bd9Sstevel@tonic-gate 	 * user specified (-m option), temp env variable,
7067c478bd9Sstevel@tonic-gate 	 * and finally /tmp if nothing is found.
7077c478bd9Sstevel@tonic-gate 	 */
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	if (alt_tmp_dir) {
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 		/* copy and leave room for temp filename */
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 		(void) strlcpy(t, alt_tmp_dir, PATH_MAX - 10);
7147c478bd9Sstevel@tonic-gate 	} else {
7157c478bd9Sstevel@tonic-gate 		envptr = getenv("TMPDIR");
7167c478bd9Sstevel@tonic-gate 		if (envptr != NULL) {
7177c478bd9Sstevel@tonic-gate 			(void) strlcpy(t, envptr, PATH_MAX - 10);
7187c478bd9Sstevel@tonic-gate 		} else {
7197c478bd9Sstevel@tonic-gate 			(void) strlcpy(t, "/tmp", 5);
7207c478bd9Sstevel@tonic-gate 		}
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	/*
7247c478bd9Sstevel@tonic-gate 	 * no need to check if path is valid. statvfs will catch
7257c478bd9Sstevel@tonic-gate 	 * it later and fail with a proper error message.
7267c478bd9Sstevel@tonic-gate 	 */
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	return (t);
7297c478bd9Sstevel@tonic-gate }
730