xref: /titanic_41/usr/src/cmd/sendmail/libsm/strio.c (revision 49218d4f8e4d84d1c08aeb267bcf6e451f2056dc)
17c478bd9Sstevel@tonic-gate /*
2*49218d4fSjbeck  * Copyright (c) 2000-2002, 2004, 2005 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *      All rights reserved.
47c478bd9Sstevel@tonic-gate  * Copyright (c) 1990, 1993
57c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * This code is derived from software contributed to Berkeley by
87c478bd9Sstevel@tonic-gate  * Chris Torek.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
117c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
127c478bd9Sstevel@tonic-gate  * the sendmail distribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate #include <sm/gen.h>
18*49218d4fSjbeck SM_IDSTR(id, "@(#)$Id: strio.c,v 1.44 2005/06/09 21:40:19 ca Exp $")
197c478bd9Sstevel@tonic-gate #include <stdlib.h>
207c478bd9Sstevel@tonic-gate #include <unistd.h>
217c478bd9Sstevel@tonic-gate #include <fcntl.h>
227c478bd9Sstevel@tonic-gate #include <string.h>
237c478bd9Sstevel@tonic-gate #include <errno.h>
247c478bd9Sstevel@tonic-gate #include <sm/rpool.h>
257c478bd9Sstevel@tonic-gate #include <sm/io.h>
267c478bd9Sstevel@tonic-gate #include <sm/heap.h>
277c478bd9Sstevel@tonic-gate #include <sm/conf.h>
287c478bd9Sstevel@tonic-gate #include "local.h"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate static int	sm_strsetmode __P((SM_FILE_T*, const int *));
317c478bd9Sstevel@tonic-gate static int	sm_strgetmode __P((SM_FILE_T*, int *));
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate **  Cookie structure for the "strio" file type
357c478bd9Sstevel@tonic-gate */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate struct sm_str_obj
387c478bd9Sstevel@tonic-gate {
397c478bd9Sstevel@tonic-gate 	char		*strio_base;
407c478bd9Sstevel@tonic-gate 	char		*strio_end;
417c478bd9Sstevel@tonic-gate 	size_t		strio_size;
427c478bd9Sstevel@tonic-gate 	size_t		strio_offset;
437c478bd9Sstevel@tonic-gate 	int		strio_flags;
447c478bd9Sstevel@tonic-gate 	const void	*strio_rpool;
457c478bd9Sstevel@tonic-gate };
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate typedef struct sm_str_obj SM_STR_OBJ_T;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate **  SM_STRGROW -- increase storage space for string
517c478bd9Sstevel@tonic-gate **
527c478bd9Sstevel@tonic-gate **	Parameters:
537c478bd9Sstevel@tonic-gate **		s -- current cookie
547c478bd9Sstevel@tonic-gate **		size -- new storage size request
557c478bd9Sstevel@tonic-gate **
567c478bd9Sstevel@tonic-gate **	Returns:
577c478bd9Sstevel@tonic-gate **		true iff successful.
587c478bd9Sstevel@tonic-gate */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static bool sm_strgrow __P((SM_STR_OBJ_T *, size_t));
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static bool
sm_strgrow(s,size)637c478bd9Sstevel@tonic-gate sm_strgrow(s, size)
647c478bd9Sstevel@tonic-gate 	SM_STR_OBJ_T *s;
657c478bd9Sstevel@tonic-gate 	size_t size;
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	register void *p;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	if (s->strio_size >= size)
707c478bd9Sstevel@tonic-gate 		return true;
717c478bd9Sstevel@tonic-gate 	p = sm_realloc(s->strio_base, size);
727c478bd9Sstevel@tonic-gate 	if (p == NULL)
737c478bd9Sstevel@tonic-gate 		return false;
747c478bd9Sstevel@tonic-gate 	s->strio_base = p;
757c478bd9Sstevel@tonic-gate 	s->strio_end = s->strio_base + size;
767c478bd9Sstevel@tonic-gate 	s->strio_size = size;
777c478bd9Sstevel@tonic-gate 	return true;
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate **  SM_STRREAD -- read a portion of the string
827c478bd9Sstevel@tonic-gate **
837c478bd9Sstevel@tonic-gate **	Parameters:
847c478bd9Sstevel@tonic-gate **		fp -- the file pointer
857c478bd9Sstevel@tonic-gate **		buf -- location to place read data
867c478bd9Sstevel@tonic-gate **		n -- number of bytes to read
877c478bd9Sstevel@tonic-gate **
887c478bd9Sstevel@tonic-gate **	Returns:
897c478bd9Sstevel@tonic-gate **		Failure: -1 and sets errno
907c478bd9Sstevel@tonic-gate **		Success: >=0, number of bytes read
917c478bd9Sstevel@tonic-gate */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate ssize_t
sm_strread(fp,buf,n)947c478bd9Sstevel@tonic-gate sm_strread(fp, buf, n)
957c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
967c478bd9Sstevel@tonic-gate 	char *buf;
977c478bd9Sstevel@tonic-gate 	size_t n;
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	register SM_STR_OBJ_T *s = fp->f_cookie;
1007c478bd9Sstevel@tonic-gate 	int len;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if (!(s->strio_flags & SMRD) && !(s->strio_flags & SMRW))
1037c478bd9Sstevel@tonic-gate 	{
1047c478bd9Sstevel@tonic-gate 		errno = EBADF;
1057c478bd9Sstevel@tonic-gate 		return -1;
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 	len = SM_MIN(s->strio_size - s->strio_offset, n);
1087c478bd9Sstevel@tonic-gate 	(void) memmove(buf, s->strio_base + s->strio_offset, len);
1097c478bd9Sstevel@tonic-gate 	s->strio_offset += len;
1107c478bd9Sstevel@tonic-gate 	return len;
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate **  SM_STRWRITE -- write a portion of the string
1157c478bd9Sstevel@tonic-gate **
1167c478bd9Sstevel@tonic-gate **	Parameters:
1177c478bd9Sstevel@tonic-gate **		fp -- the file pointer
1187c478bd9Sstevel@tonic-gate **		buf -- location of data for writing
1197c478bd9Sstevel@tonic-gate **		n -- number of bytes to write
1207c478bd9Sstevel@tonic-gate **
1217c478bd9Sstevel@tonic-gate **	Returns:
1227c478bd9Sstevel@tonic-gate **		Failure: -1 and sets errno
1237c478bd9Sstevel@tonic-gate **		Success: >=0, number of bytes written
1247c478bd9Sstevel@tonic-gate */
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate ssize_t
sm_strwrite(fp,buf,n)1277c478bd9Sstevel@tonic-gate sm_strwrite(fp, buf, n)
1287c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
1297c478bd9Sstevel@tonic-gate 	char const *buf;
1307c478bd9Sstevel@tonic-gate 	size_t n;
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	register SM_STR_OBJ_T *s = fp->f_cookie;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	if (!(s->strio_flags & SMWR) && !(s->strio_flags & SMRW))
1357c478bd9Sstevel@tonic-gate 	{
1367c478bd9Sstevel@tonic-gate 		errno = EBADF;
1377c478bd9Sstevel@tonic-gate 		return -1;
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 	if (n + s->strio_offset > s->strio_size)
1407c478bd9Sstevel@tonic-gate 	{
1417c478bd9Sstevel@tonic-gate 		if (!sm_strgrow(s, n + s->strio_offset))
1427c478bd9Sstevel@tonic-gate 			return 0;
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 	(void) memmove(s->strio_base + s->strio_offset, buf, n);
1457c478bd9Sstevel@tonic-gate 	s->strio_offset += n;
1467c478bd9Sstevel@tonic-gate 	return n;
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate **  SM_STRSEEK -- position the offset pointer for the string
1517c478bd9Sstevel@tonic-gate **
1527c478bd9Sstevel@tonic-gate **	Only SM_IO_SEEK_SET, SM_IO_SEEK_CUR and SM_IO_SEEK_END are valid
1537c478bd9Sstevel@tonic-gate **	values for whence.
1547c478bd9Sstevel@tonic-gate **
1557c478bd9Sstevel@tonic-gate **	Parameters:
1567c478bd9Sstevel@tonic-gate **		fp -- the file pointer
1577c478bd9Sstevel@tonic-gate **		offset -- number of bytes offset from "base"
1587c478bd9Sstevel@tonic-gate **		whence -- determines "base" for 'offset'
1597c478bd9Sstevel@tonic-gate **
1607c478bd9Sstevel@tonic-gate **	Returns:
1617c478bd9Sstevel@tonic-gate **		Failure: -1 and sets errno
1627c478bd9Sstevel@tonic-gate **		Success: >=0, number of bytes read
1637c478bd9Sstevel@tonic-gate */
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate off_t
sm_strseek(fp,offset,whence)1667c478bd9Sstevel@tonic-gate sm_strseek(fp, offset, whence)
1677c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
1687c478bd9Sstevel@tonic-gate 	off_t offset;
1697c478bd9Sstevel@tonic-gate 	int whence;
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	register off_t ret;
1727c478bd9Sstevel@tonic-gate 	register SM_STR_OBJ_T *s = fp->f_cookie;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate reseek:
1757c478bd9Sstevel@tonic-gate 	switch (whence)
1767c478bd9Sstevel@tonic-gate 	{
1777c478bd9Sstevel@tonic-gate 	  case SM_IO_SEEK_SET:
1787c478bd9Sstevel@tonic-gate 		ret = offset;
1797c478bd9Sstevel@tonic-gate 		break;
1807c478bd9Sstevel@tonic-gate 	  case SM_IO_SEEK_CUR:
1817c478bd9Sstevel@tonic-gate 		ret = s->strio_offset + offset;
1827c478bd9Sstevel@tonic-gate 		break;
1837c478bd9Sstevel@tonic-gate 	  case SM_IO_SEEK_END:
1847c478bd9Sstevel@tonic-gate 		ret = s->strio_size;
1857c478bd9Sstevel@tonic-gate 		break;
1867c478bd9Sstevel@tonic-gate 	  default:
1877c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1887c478bd9Sstevel@tonic-gate 		return -1;
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 	if (ret < 0 || ret > (off_t)(size_t)(-1))	/* XXX ugly */
1917c478bd9Sstevel@tonic-gate 		return -1;
1927c478bd9Sstevel@tonic-gate 	if ((size_t) ret > s->strio_size)
1937c478bd9Sstevel@tonic-gate 	{
1947c478bd9Sstevel@tonic-gate 		if (sm_strgrow(s, (size_t)ret))
1957c478bd9Sstevel@tonic-gate 			goto reseek;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		/* errno set by sm_strgrow */
1987c478bd9Sstevel@tonic-gate 		return -1;
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 	s->strio_offset = (size_t) ret;
2017c478bd9Sstevel@tonic-gate 	return ret;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /*
2057c478bd9Sstevel@tonic-gate **  SM_STROPEN -- open a string file type
2067c478bd9Sstevel@tonic-gate **
2077c478bd9Sstevel@tonic-gate **	Parameters:
2087c478bd9Sstevel@tonic-gate **		fp -- file pointer open to be associated with
2097c478bd9Sstevel@tonic-gate **		info -- initial contents (NULL for none)
2107c478bd9Sstevel@tonic-gate **		flags -- flags for methods of access (was mode)
2117c478bd9Sstevel@tonic-gate **		rpool -- resource pool to use memory from (if applicable)
2127c478bd9Sstevel@tonic-gate **
2137c478bd9Sstevel@tonic-gate **	Results:
2147c478bd9Sstevel@tonic-gate **		Success: 0 (zero)
2157c478bd9Sstevel@tonic-gate **		Failure: -1 and sets errno
2167c478bd9Sstevel@tonic-gate */
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate int
sm_stropen(fp,info,flags,rpool)2197c478bd9Sstevel@tonic-gate sm_stropen(fp, info, flags, rpool)
2207c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
2217c478bd9Sstevel@tonic-gate 	const void *info;
2227c478bd9Sstevel@tonic-gate 	int flags;
2237c478bd9Sstevel@tonic-gate 	const void *rpool;
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	register SM_STR_OBJ_T *s;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate #if SM_RPOOL
2287c478bd9Sstevel@tonic-gate 	s = sm_rpool_malloc_x(rpool, sizeof(SM_STR_OBJ_T));
2297c478bd9Sstevel@tonic-gate #else /* SM_RPOOL */
2307c478bd9Sstevel@tonic-gate 	s = sm_malloc(sizeof(SM_STR_OBJ_T));
2317c478bd9Sstevel@tonic-gate 	if (s == NULL)
2327c478bd9Sstevel@tonic-gate 		return -1;
2337c478bd9Sstevel@tonic-gate #endif /* SM_RPOOL */
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	fp->f_cookie = s;
2367c478bd9Sstevel@tonic-gate 	s->strio_rpool = rpool;
2377c478bd9Sstevel@tonic-gate 	s->strio_offset = 0;
2387c478bd9Sstevel@tonic-gate 	s->strio_size = 0;
2397c478bd9Sstevel@tonic-gate 	s->strio_base = NULL;
2407c478bd9Sstevel@tonic-gate 	s->strio_end = 0;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	switch (flags)
2437c478bd9Sstevel@tonic-gate 	{
2447c478bd9Sstevel@tonic-gate 	  case SM_IO_RDWR:
2457c478bd9Sstevel@tonic-gate 		s->strio_flags = SMRW;
2467c478bd9Sstevel@tonic-gate 		break;
2477c478bd9Sstevel@tonic-gate 	  case SM_IO_RDONLY:
2487c478bd9Sstevel@tonic-gate 		s->strio_flags = SMRD;
2497c478bd9Sstevel@tonic-gate 		break;
2507c478bd9Sstevel@tonic-gate 	  case SM_IO_WRONLY:
2517c478bd9Sstevel@tonic-gate 		s->strio_flags = SMWR;
2527c478bd9Sstevel@tonic-gate 		break;
2537c478bd9Sstevel@tonic-gate 	  case SM_IO_APPEND:
2547c478bd9Sstevel@tonic-gate 		if (s->strio_rpool == NULL)
2557c478bd9Sstevel@tonic-gate 			sm_free(s);
2567c478bd9Sstevel@tonic-gate 		errno = EINVAL;
2577c478bd9Sstevel@tonic-gate 		return -1;
2587c478bd9Sstevel@tonic-gate 	  default:
2597c478bd9Sstevel@tonic-gate 		if (s->strio_rpool == NULL)
2607c478bd9Sstevel@tonic-gate 			sm_free(s);
2617c478bd9Sstevel@tonic-gate 		errno = EINVAL;
2627c478bd9Sstevel@tonic-gate 		return -1;
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (info != NULL)
2667c478bd9Sstevel@tonic-gate 	{
2677c478bd9Sstevel@tonic-gate 		s->strio_base = sm_strdup_x(info);
2687c478bd9Sstevel@tonic-gate 		if (s->strio_base == NULL)
2697c478bd9Sstevel@tonic-gate 		{
2707c478bd9Sstevel@tonic-gate 			int save_errno = errno;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 			if (s->strio_rpool == NULL)
2737c478bd9Sstevel@tonic-gate 				sm_free(s);
2747c478bd9Sstevel@tonic-gate 			errno = save_errno;
2757c478bd9Sstevel@tonic-gate 			return -1;
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 		s->strio_size = strlen(info);
2787c478bd9Sstevel@tonic-gate 		s->strio_end = s->strio_base + s->strio_size;
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 	return 0;
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate **  SM_STRCLOSE -- close the string file type and free resources
2857c478bd9Sstevel@tonic-gate **
2867c478bd9Sstevel@tonic-gate **	Parameters:
2877c478bd9Sstevel@tonic-gate **		fp -- file pointer
2887c478bd9Sstevel@tonic-gate **
2897c478bd9Sstevel@tonic-gate **	Results:
2907c478bd9Sstevel@tonic-gate **		Success: 0 (zero)
2917c478bd9Sstevel@tonic-gate */
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate int
sm_strclose(fp)2947c478bd9Sstevel@tonic-gate sm_strclose(fp)
2957c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 	SM_STR_OBJ_T *s = fp->f_cookie;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate #if !SM_RPOOL
3007c478bd9Sstevel@tonic-gate 	sm_free(s->strio_base);
3017c478bd9Sstevel@tonic-gate 	s->strio_base = NULL;
3027c478bd9Sstevel@tonic-gate #endif /* !SM_RPOOL */
3037c478bd9Sstevel@tonic-gate 	return 0;
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate /*
3077c478bd9Sstevel@tonic-gate **  SM_STRSETMODE -- set mode info for the file
3087c478bd9Sstevel@tonic-gate **
3097c478bd9Sstevel@tonic-gate **	 Note: changing the mode can be a safe way to have the "parent"
3107c478bd9Sstevel@tonic-gate **	 set up a string that the "child" is not to modify
3117c478bd9Sstevel@tonic-gate **
3127c478bd9Sstevel@tonic-gate **	Parameters:
3137c478bd9Sstevel@tonic-gate **		fp -- the file pointer
3147c478bd9Sstevel@tonic-gate **		mode -- location of new mode to set
3157c478bd9Sstevel@tonic-gate **
3167c478bd9Sstevel@tonic-gate **	Results:
3177c478bd9Sstevel@tonic-gate **		Success: 0 (zero)
3187c478bd9Sstevel@tonic-gate **		Failure: -1 and sets errno
3197c478bd9Sstevel@tonic-gate */
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate static int
sm_strsetmode(fp,mode)3227c478bd9Sstevel@tonic-gate sm_strsetmode(fp, mode)
3237c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
3247c478bd9Sstevel@tonic-gate 	const int *mode;
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	register SM_STR_OBJ_T *s = fp->f_cookie;
3277c478bd9Sstevel@tonic-gate 	int flags;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	switch (*mode)
3307c478bd9Sstevel@tonic-gate 	{
3317c478bd9Sstevel@tonic-gate 	  case SM_IO_RDWR:
3327c478bd9Sstevel@tonic-gate 		flags = SMRW;
3337c478bd9Sstevel@tonic-gate 		break;
3347c478bd9Sstevel@tonic-gate 	  case SM_IO_RDONLY:
3357c478bd9Sstevel@tonic-gate 		flags = SMRD;
3367c478bd9Sstevel@tonic-gate 		break;
3377c478bd9Sstevel@tonic-gate 	  case SM_IO_WRONLY:
3387c478bd9Sstevel@tonic-gate 		flags = SMWR;
3397c478bd9Sstevel@tonic-gate 		break;
3407c478bd9Sstevel@tonic-gate 	  case SM_IO_APPEND:
3417c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3427c478bd9Sstevel@tonic-gate 		return -1;
3437c478bd9Sstevel@tonic-gate 	  default:
3447c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3457c478bd9Sstevel@tonic-gate 		return -1;
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 	s->strio_flags &= ~SMMODEMASK;
3487c478bd9Sstevel@tonic-gate 	s->strio_flags |= flags;
3497c478bd9Sstevel@tonic-gate 	return 0;
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate /*
3537c478bd9Sstevel@tonic-gate **  SM_STRGETMODE -- get mode info for the file
3547c478bd9Sstevel@tonic-gate **
3557c478bd9Sstevel@tonic-gate **	Parameters:
3567c478bd9Sstevel@tonic-gate **		fp -- the file pointer
3577c478bd9Sstevel@tonic-gate **		mode -- location to store current mode
3587c478bd9Sstevel@tonic-gate **
3597c478bd9Sstevel@tonic-gate **	Results:
3607c478bd9Sstevel@tonic-gate **		Success: 0 (zero)
3617c478bd9Sstevel@tonic-gate **		Failure: -1 and sets errno
3627c478bd9Sstevel@tonic-gate */
3637c478bd9Sstevel@tonic-gate 
364*49218d4fSjbeck static int
sm_strgetmode(fp,mode)3657c478bd9Sstevel@tonic-gate sm_strgetmode(fp, mode)
3667c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
3677c478bd9Sstevel@tonic-gate 	int *mode;
3687c478bd9Sstevel@tonic-gate {
3697c478bd9Sstevel@tonic-gate 	register SM_STR_OBJ_T *s = fp->f_cookie;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	switch (s->strio_flags & SMMODEMASK)
3727c478bd9Sstevel@tonic-gate 	{
3737c478bd9Sstevel@tonic-gate 	  case SMRW:
3747c478bd9Sstevel@tonic-gate 		*mode = SM_IO_RDWR;
3757c478bd9Sstevel@tonic-gate 		break;
3767c478bd9Sstevel@tonic-gate 	  case SMRD:
3777c478bd9Sstevel@tonic-gate 		*mode = SM_IO_RDONLY;
3787c478bd9Sstevel@tonic-gate 		break;
3797c478bd9Sstevel@tonic-gate 	  case SMWR:
3807c478bd9Sstevel@tonic-gate 		*mode = SM_IO_WRONLY;
3817c478bd9Sstevel@tonic-gate 		break;
3827c478bd9Sstevel@tonic-gate 	  default:
3837c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3847c478bd9Sstevel@tonic-gate 		return -1;
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 	return 0;
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate **  SM_STRSETINFO -- set info for the file
3917c478bd9Sstevel@tonic-gate **
3927c478bd9Sstevel@tonic-gate **	Currently only SM_IO_WHAT_MODE is supported for 'what'.
3937c478bd9Sstevel@tonic-gate **
3947c478bd9Sstevel@tonic-gate **	Parameters:
3957c478bd9Sstevel@tonic-gate **		fp -- the file pointer
3967c478bd9Sstevel@tonic-gate **		what -- type of information to set
3977c478bd9Sstevel@tonic-gate **		valp -- location to data for doing set
3987c478bd9Sstevel@tonic-gate **
3997c478bd9Sstevel@tonic-gate **	Results:
4007c478bd9Sstevel@tonic-gate **		Failure: -1 and sets errno
4017c478bd9Sstevel@tonic-gate **		Success: sm_strsetmode() return [0 (zero)]
4027c478bd9Sstevel@tonic-gate */
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate int
sm_strsetinfo(fp,what,valp)4057c478bd9Sstevel@tonic-gate sm_strsetinfo(fp, what, valp)
4067c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
4077c478bd9Sstevel@tonic-gate 	int what;
4087c478bd9Sstevel@tonic-gate 	void *valp;
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	switch(what)
4117c478bd9Sstevel@tonic-gate 	{
4127c478bd9Sstevel@tonic-gate 	  case SM_IO_WHAT_MODE:
4137c478bd9Sstevel@tonic-gate 		return sm_strsetmode(fp, (int *) valp);
4147c478bd9Sstevel@tonic-gate 	  default:
4157c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4167c478bd9Sstevel@tonic-gate 		return -1;
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate /*
4217c478bd9Sstevel@tonic-gate **  SM_STRGETINFO -- get info for the file
4227c478bd9Sstevel@tonic-gate **
4237c478bd9Sstevel@tonic-gate **	Currently only SM_IO_WHAT_MODE is supported for 'what'.
4247c478bd9Sstevel@tonic-gate **
4257c478bd9Sstevel@tonic-gate **	Parameters:
4267c478bd9Sstevel@tonic-gate **		fp -- the file pointer
4277c478bd9Sstevel@tonic-gate **		what -- type of information requested
4287c478bd9Sstevel@tonic-gate **		valp -- location to return information in
4297c478bd9Sstevel@tonic-gate **
4307c478bd9Sstevel@tonic-gate **	Results:
4317c478bd9Sstevel@tonic-gate **		Failure: -1 and sets errno
4327c478bd9Sstevel@tonic-gate **		Success: sm_strgetmode() return [0 (zero)]
4337c478bd9Sstevel@tonic-gate */
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate int
sm_strgetinfo(fp,what,valp)4367c478bd9Sstevel@tonic-gate sm_strgetinfo(fp, what, valp)
4377c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
4387c478bd9Sstevel@tonic-gate 	int what;
4397c478bd9Sstevel@tonic-gate 	void *valp;
4407c478bd9Sstevel@tonic-gate {
4417c478bd9Sstevel@tonic-gate 	switch(what)
4427c478bd9Sstevel@tonic-gate 	{
4437c478bd9Sstevel@tonic-gate 	  case SM_IO_WHAT_MODE:
4447c478bd9Sstevel@tonic-gate 		return sm_strgetmode(fp, (int *) valp);
4457c478bd9Sstevel@tonic-gate 	  default:
4467c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4477c478bd9Sstevel@tonic-gate 		return -1;
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate /*
4527c478bd9Sstevel@tonic-gate **  SM_STRIO_INIT -- initializes a write-only string type
4537c478bd9Sstevel@tonic-gate **
4547c478bd9Sstevel@tonic-gate **  Original comments below. This function does not appear to be used anywhere.
4557c478bd9Sstevel@tonic-gate **  The same functionality can be done by changing the mode of the file.
4567c478bd9Sstevel@tonic-gate **  ------------
4577c478bd9Sstevel@tonic-gate ** sm_strio_init initializes an SM_FILE_T structure as a write-only file
4587c478bd9Sstevel@tonic-gate ** that writes into the specified buffer:
4597c478bd9Sstevel@tonic-gate ** - Use sm_io_putc, sm_io_fprintf, etc, to write into the buffer.
4607c478bd9Sstevel@tonic-gate **   Attempts to write more than size-1 characters into the buffer will fail
4617c478bd9Sstevel@tonic-gate **   silently (no error is reported).
4627c478bd9Sstevel@tonic-gate ** - Use sm_io_fflush to nul terminate the string in the buffer
4637c478bd9Sstevel@tonic-gate **   (the write pointer is not advanced).
4647c478bd9Sstevel@tonic-gate ** No memory is allocated either by sm_strio_init or by sm_io_{putc,write} etc.
4657c478bd9Sstevel@tonic-gate **
4667c478bd9Sstevel@tonic-gate **	Parameters:
4677c478bd9Sstevel@tonic-gate **		fp -- file pointer
4687c478bd9Sstevel@tonic-gate **		buf -- memory location for stored data
4697c478bd9Sstevel@tonic-gate **		size -- size of 'buf'
4707c478bd9Sstevel@tonic-gate **
4717c478bd9Sstevel@tonic-gate **	Results:
4727c478bd9Sstevel@tonic-gate **		none.
4737c478bd9Sstevel@tonic-gate */
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate void
sm_strio_init(fp,buf,size)4767c478bd9Sstevel@tonic-gate sm_strio_init(fp, buf, size)
4777c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
4787c478bd9Sstevel@tonic-gate 	char *buf;
4797c478bd9Sstevel@tonic-gate 	size_t size;
4807c478bd9Sstevel@tonic-gate {
4817c478bd9Sstevel@tonic-gate 	fp->sm_magic = SmFileMagic;
4827c478bd9Sstevel@tonic-gate 	fp->f_flags = SMWR | SMSTR;
4837c478bd9Sstevel@tonic-gate 	fp->f_file = -1;
4847c478bd9Sstevel@tonic-gate 	fp->f_bf.smb_base = fp->f_p = (unsigned char *) buf;
4857c478bd9Sstevel@tonic-gate 	fp->f_bf.smb_size = fp->f_w = (size ? size - 1 : 0);
4867c478bd9Sstevel@tonic-gate 	fp->f_lbfsize = 0;
4877c478bd9Sstevel@tonic-gate 	fp->f_r = 0;
4887c478bd9Sstevel@tonic-gate 	fp->f_read = NULL;
4897c478bd9Sstevel@tonic-gate 	fp->f_seek = NULL;
4907c478bd9Sstevel@tonic-gate 	fp->f_getinfo = NULL;
4917c478bd9Sstevel@tonic-gate 	fp->f_setinfo = NULL;
4927c478bd9Sstevel@tonic-gate }
493