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