1N/A/*
1N/A * Copyright (c) 2000-2002, 2004, 2005 Sendmail, Inc. and its suppliers.
1N/A * All rights reserved.
1N/A * Copyright (c) 1990, 1993
1N/A * The Regents of the University of California. All rights reserved.
1N/A *
1N/A * This code is derived from software contributed to Berkeley by
1N/A * Chris Torek.
1N/A *
1N/A * By using this file, you agree to the terms and conditions set
1N/A * forth in the LICENSE file which can be found at the top level of
1N/A * the sendmail distribution.
1N/A */
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A#include <sm/gen.h>
1N/ASM_IDSTR(id, "@(#)$Id: strio.c,v 1.44 2005/06/09 21:40:19 ca Exp $")
1N/A#include <stdlib.h>
1N/A#include <unistd.h>
1N/A#include <fcntl.h>
1N/A#include <string.h>
1N/A#include <errno.h>
1N/A#include <sm/rpool.h>
1N/A#include <sm/io.h>
1N/A#include <sm/heap.h>
1N/A#include <sm/conf.h>
1N/A#include "local.h"
1N/A
1N/Astatic int sm_strsetmode __P((SM_FILE_T*, const int *));
1N/Astatic int sm_strgetmode __P((SM_FILE_T*, int *));
1N/A
1N/A/*
1N/A** Cookie structure for the "strio" file type
1N/A*/
1N/A
1N/Astruct sm_str_obj
1N/A{
1N/A char *strio_base;
1N/A char *strio_end;
1N/A size_t strio_size;
1N/A size_t strio_offset;
1N/A int strio_flags;
1N/A const void *strio_rpool;
1N/A};
1N/A
1N/Atypedef struct sm_str_obj SM_STR_OBJ_T;
1N/A
1N/A/*
1N/A** SM_STRGROW -- increase storage space for string
1N/A**
1N/A** Parameters:
1N/A** s -- current cookie
1N/A** size -- new storage size request
1N/A**
1N/A** Returns:
1N/A** true iff successful.
1N/A*/
1N/A
1N/Astatic bool sm_strgrow __P((SM_STR_OBJ_T *, size_t));
1N/A
1N/Astatic bool
1N/Asm_strgrow(s, size)
1N/A SM_STR_OBJ_T *s;
1N/A size_t size;
1N/A{
1N/A register void *p;
1N/A
1N/A if (s->strio_size >= size)
1N/A return true;
1N/A p = sm_realloc(s->strio_base, size);
1N/A if (p == NULL)
1N/A return false;
1N/A s->strio_base = p;
1N/A s->strio_end = s->strio_base + size;
1N/A s->strio_size = size;
1N/A return true;
1N/A}
1N/A
1N/A/*
1N/A** SM_STRREAD -- read a portion of the string
1N/A**
1N/A** Parameters:
1N/A** fp -- the file pointer
1N/A** buf -- location to place read data
1N/A** n -- number of bytes to read
1N/A**
1N/A** Returns:
1N/A** Failure: -1 and sets errno
1N/A** Success: >=0, number of bytes read
1N/A*/
1N/A
1N/Assize_t
1N/Asm_strread(fp, buf, n)
1N/A SM_FILE_T *fp;
1N/A char *buf;
1N/A size_t n;
1N/A{
1N/A register SM_STR_OBJ_T *s = fp->f_cookie;
1N/A int len;
1N/A
1N/A if (!(s->strio_flags & SMRD) && !(s->strio_flags & SMRW))
1N/A {
1N/A errno = EBADF;
1N/A return -1;
1N/A }
1N/A len = SM_MIN(s->strio_size - s->strio_offset, n);
1N/A (void) memmove(buf, s->strio_base + s->strio_offset, len);
1N/A s->strio_offset += len;
1N/A return len;
1N/A}
1N/A
1N/A/*
1N/A** SM_STRWRITE -- write a portion of the string
1N/A**
1N/A** Parameters:
1N/A** fp -- the file pointer
1N/A** buf -- location of data for writing
1N/A** n -- number of bytes to write
1N/A**
1N/A** Returns:
1N/A** Failure: -1 and sets errno
1N/A** Success: >=0, number of bytes written
1N/A*/
1N/A
1N/Assize_t
1N/Asm_strwrite(fp, buf, n)
1N/A SM_FILE_T *fp;
1N/A char const *buf;
1N/A size_t n;
1N/A{
1N/A register SM_STR_OBJ_T *s = fp->f_cookie;
1N/A
1N/A if (!(s->strio_flags & SMWR) && !(s->strio_flags & SMRW))
1N/A {
1N/A errno = EBADF;
1N/A return -1;
1N/A }
1N/A if (n + s->strio_offset > s->strio_size)
1N/A {
1N/A if (!sm_strgrow(s, n + s->strio_offset))
1N/A return 0;
1N/A }
1N/A (void) memmove(s->strio_base + s->strio_offset, buf, n);
1N/A s->strio_offset += n;
1N/A return n;
1N/A}
1N/A
1N/A/*
1N/A** SM_STRSEEK -- position the offset pointer for the string
1N/A**
1N/A** Only SM_IO_SEEK_SET, SM_IO_SEEK_CUR and SM_IO_SEEK_END are valid
1N/A** values for whence.
1N/A**
1N/A** Parameters:
1N/A** fp -- the file pointer
1N/A** offset -- number of bytes offset from "base"
1N/A** whence -- determines "base" for 'offset'
1N/A**
1N/A** Returns:
1N/A** Failure: -1 and sets errno
1N/A** Success: >=0, number of bytes read
1N/A*/
1N/A
1N/Aoff_t
1N/Asm_strseek(fp, offset, whence)
1N/A SM_FILE_T *fp;
1N/A off_t offset;
1N/A int whence;
1N/A{
1N/A register off_t ret;
1N/A register SM_STR_OBJ_T *s = fp->f_cookie;
1N/A
1N/Areseek:
1N/A switch (whence)
1N/A {
1N/A case SM_IO_SEEK_SET:
1N/A ret = offset;
1N/A break;
1N/A case SM_IO_SEEK_CUR:
1N/A ret = s->strio_offset + offset;
1N/A break;
1N/A case SM_IO_SEEK_END:
1N/A ret = s->strio_size;
1N/A break;
1N/A default:
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A if (ret < 0 || ret > (off_t)(size_t)(-1)) /* XXX ugly */
1N/A return -1;
1N/A if ((size_t) ret > s->strio_size)
1N/A {
1N/A if (sm_strgrow(s, (size_t)ret))
1N/A goto reseek;
1N/A
1N/A /* errno set by sm_strgrow */
1N/A return -1;
1N/A }
1N/A s->strio_offset = (size_t) ret;
1N/A return ret;
1N/A}
1N/A
1N/A/*
1N/A** SM_STROPEN -- open a string file type
1N/A**
1N/A** Parameters:
1N/A** fp -- file pointer open to be associated with
1N/A** info -- initial contents (NULL for none)
1N/A** flags -- flags for methods of access (was mode)
1N/A** rpool -- resource pool to use memory from (if applicable)
1N/A**
1N/A** Results:
1N/A** Success: 0 (zero)
1N/A** Failure: -1 and sets errno
1N/A*/
1N/A
1N/Aint
1N/Asm_stropen(fp, info, flags, rpool)
1N/A SM_FILE_T *fp;
1N/A const void *info;
1N/A int flags;
1N/A const void *rpool;
1N/A{
1N/A register SM_STR_OBJ_T *s;
1N/A
1N/A#if SM_RPOOL
1N/A s = sm_rpool_malloc_x(rpool, sizeof(SM_STR_OBJ_T));
1N/A#else /* SM_RPOOL */
1N/A s = sm_malloc(sizeof(SM_STR_OBJ_T));
1N/A if (s == NULL)
1N/A return -1;
1N/A#endif /* SM_RPOOL */
1N/A
1N/A fp->f_cookie = s;
1N/A s->strio_rpool = rpool;
1N/A s->strio_offset = 0;
1N/A s->strio_size = 0;
1N/A s->strio_base = NULL;
1N/A s->strio_end = 0;
1N/A
1N/A switch (flags)
1N/A {
1N/A case SM_IO_RDWR:
1N/A s->strio_flags = SMRW;
1N/A break;
1N/A case SM_IO_RDONLY:
1N/A s->strio_flags = SMRD;
1N/A break;
1N/A case SM_IO_WRONLY:
1N/A s->strio_flags = SMWR;
1N/A break;
1N/A case SM_IO_APPEND:
1N/A if (s->strio_rpool == NULL)
1N/A sm_free(s);
1N/A errno = EINVAL;
1N/A return -1;
1N/A default:
1N/A if (s->strio_rpool == NULL)
1N/A sm_free(s);
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A
1N/A if (info != NULL)
1N/A {
1N/A s->strio_base = sm_strdup_x(info);
1N/A if (s->strio_base == NULL)
1N/A {
1N/A int save_errno = errno;
1N/A
1N/A if (s->strio_rpool == NULL)
1N/A sm_free(s);
1N/A errno = save_errno;
1N/A return -1;
1N/A }
1N/A s->strio_size = strlen(info);
1N/A s->strio_end = s->strio_base + s->strio_size;
1N/A }
1N/A return 0;
1N/A}
1N/A
1N/A/*
1N/A** SM_STRCLOSE -- close the string file type and free resources
1N/A**
1N/A** Parameters:
1N/A** fp -- file pointer
1N/A**
1N/A** Results:
1N/A** Success: 0 (zero)
1N/A*/
1N/A
1N/Aint
1N/Asm_strclose(fp)
1N/A SM_FILE_T *fp;
1N/A{
1N/A SM_STR_OBJ_T *s = fp->f_cookie;
1N/A
1N/A#if !SM_RPOOL
1N/A sm_free(s->strio_base);
1N/A s->strio_base = NULL;
1N/A#endif /* !SM_RPOOL */
1N/A return 0;
1N/A}
1N/A
1N/A/*
1N/A** SM_STRSETMODE -- set mode info for the file
1N/A**
1N/A** Note: changing the mode can be a safe way to have the "parent"
1N/A** set up a string that the "child" is not to modify
1N/A**
1N/A** Parameters:
1N/A** fp -- the file pointer
1N/A** mode -- location of new mode to set
1N/A**
1N/A** Results:
1N/A** Success: 0 (zero)
1N/A** Failure: -1 and sets errno
1N/A*/
1N/A
1N/Astatic int
1N/Asm_strsetmode(fp, mode)
1N/A SM_FILE_T *fp;
1N/A const int *mode;
1N/A{
1N/A register SM_STR_OBJ_T *s = fp->f_cookie;
1N/A int flags;
1N/A
1N/A switch (*mode)
1N/A {
1N/A case SM_IO_RDWR:
1N/A flags = SMRW;
1N/A break;
1N/A case SM_IO_RDONLY:
1N/A flags = SMRD;
1N/A break;
1N/A case SM_IO_WRONLY:
1N/A flags = SMWR;
1N/A break;
1N/A case SM_IO_APPEND:
1N/A errno = EINVAL;
1N/A return -1;
1N/A default:
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A s->strio_flags &= ~SMMODEMASK;
1N/A s->strio_flags |= flags;
1N/A return 0;
1N/A}
1N/A
1N/A/*
1N/A** SM_STRGETMODE -- get mode info for the file
1N/A**
1N/A** Parameters:
1N/A** fp -- the file pointer
1N/A** mode -- location to store current mode
1N/A**
1N/A** Results:
1N/A** Success: 0 (zero)
1N/A** Failure: -1 and sets errno
1N/A*/
1N/A
1N/Astatic int
1N/Asm_strgetmode(fp, mode)
1N/A SM_FILE_T *fp;
1N/A int *mode;
1N/A{
1N/A register SM_STR_OBJ_T *s = fp->f_cookie;
1N/A
1N/A switch (s->strio_flags & SMMODEMASK)
1N/A {
1N/A case SMRW:
1N/A *mode = SM_IO_RDWR;
1N/A break;
1N/A case SMRD:
1N/A *mode = SM_IO_RDONLY;
1N/A break;
1N/A case SMWR:
1N/A *mode = SM_IO_WRONLY;
1N/A break;
1N/A default:
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A return 0;
1N/A}
1N/A
1N/A/*
1N/A** SM_STRSETINFO -- set info for the file
1N/A**
1N/A** Currently only SM_IO_WHAT_MODE is supported for 'what'.
1N/A**
1N/A** Parameters:
1N/A** fp -- the file pointer
1N/A** what -- type of information to set
1N/A** valp -- location to data for doing set
1N/A**
1N/A** Results:
1N/A** Failure: -1 and sets errno
1N/A** Success: sm_strsetmode() return [0 (zero)]
1N/A*/
1N/A
1N/Aint
1N/Asm_strsetinfo(fp, what, valp)
1N/A SM_FILE_T *fp;
1N/A int what;
1N/A void *valp;
1N/A{
1N/A switch(what)
1N/A {
1N/A case SM_IO_WHAT_MODE:
1N/A return sm_strsetmode(fp, (int *) valp);
1N/A default:
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A}
1N/A
1N/A/*
1N/A** SM_STRGETINFO -- get info for the file
1N/A**
1N/A** Currently only SM_IO_WHAT_MODE is supported for 'what'.
1N/A**
1N/A** Parameters:
1N/A** fp -- the file pointer
1N/A** what -- type of information requested
1N/A** valp -- location to return information in
1N/A**
1N/A** Results:
1N/A** Failure: -1 and sets errno
1N/A** Success: sm_strgetmode() return [0 (zero)]
1N/A*/
1N/A
1N/Aint
1N/Asm_strgetinfo(fp, what, valp)
1N/A SM_FILE_T *fp;
1N/A int what;
1N/A void *valp;
1N/A{
1N/A switch(what)
1N/A {
1N/A case SM_IO_WHAT_MODE:
1N/A return sm_strgetmode(fp, (int *) valp);
1N/A default:
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A}
1N/A
1N/A/*
1N/A** SM_STRIO_INIT -- initializes a write-only string type
1N/A**
1N/A** Original comments below. This function does not appear to be used anywhere.
1N/A** The same functionality can be done by changing the mode of the file.
1N/A** ------------
1N/A** sm_strio_init initializes an SM_FILE_T structure as a write-only file
1N/A** that writes into the specified buffer:
1N/A** - Use sm_io_putc, sm_io_fprintf, etc, to write into the buffer.
1N/A** Attempts to write more than size-1 characters into the buffer will fail
1N/A** silently (no error is reported).
1N/A** - Use sm_io_fflush to nul terminate the string in the buffer
1N/A** (the write pointer is not advanced).
1N/A** No memory is allocated either by sm_strio_init or by sm_io_{putc,write} etc.
1N/A**
1N/A** Parameters:
1N/A** fp -- file pointer
1N/A** buf -- memory location for stored data
1N/A** size -- size of 'buf'
1N/A**
1N/A** Results:
1N/A** none.
1N/A*/
1N/A
1N/Avoid
1N/Asm_strio_init(fp, buf, size)
1N/A SM_FILE_T *fp;
1N/A char *buf;
1N/A size_t size;
1N/A{
1N/A fp->sm_magic = SmFileMagic;
1N/A fp->f_flags = SMWR | SMSTR;
1N/A fp->f_file = -1;
1N/A fp->f_bf.smb_base = fp->f_p = (unsigned char *) buf;
1N/A fp->f_bf.smb_size = fp->f_w = (size ? size - 1 : 0);
1N/A fp->f_lbfsize = 0;
1N/A fp->f_r = 0;
1N/A fp->f_read = NULL;
1N/A fp->f_seek = NULL;
1N/A fp->f_getinfo = NULL;
1N/A fp->f_setinfo = NULL;
1N/A}