1N/A/*
1N/A * Copyright (c) 2000-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_RCSID("@(#)$Id: stdio.c,v 1.71 2005/06/14 23:07:20 ca Exp $")
1N/A#include <unistd.h>
1N/A#include <errno.h>
1N/A#include <fcntl.h>
1N/A#include <string.h> /* FreeBSD: FD_ZERO needs <string.h> */
1N/A#include <sys/stat.h>
1N/A#include <sm/time.h>
1N/A#include <sm/heap.h>
1N/A#include <sm/assert.h>
1N/A#include <sm/varargs.h>
1N/A#include <sm/io.h>
1N/A#include <sm/setjmp.h>
1N/A#include <sm/conf.h>
1N/A#include <sm/fdset.h>
1N/A#include "local.h"
1N/A
1N/Astatic int sm_stdsetmode __P((SM_FILE_T *, const int *));
1N/Astatic int sm_stdgetmode __P((SM_FILE_T *, int *));
1N/A
1N/A/*
1N/A** Overall:
1N/A** Small standard I/O/seek/close functions.
1N/A** These maintain the `known seek offset' for seek optimization.
1N/A*/
1N/A
1N/A/*
1N/A** SM_STDOPEN -- open a file with stdio behavior
1N/A**
1N/A** Not associated with the system's stdio in libc.
1N/A**
1N/A** Parameters:
1N/A** fp -- file pointer to be associated with the open
1N/A** info -- pathname of the file to be opened
1N/A** flags -- indicates type of access methods
1N/A** rpool -- ignored
1N/A**
1N/A** Returns:
1N/A** Failure: -1 and set errno
1N/A** Success: 0 or greater (fd of file from open(2)).
1N/A**
1N/A*/
1N/A
1N/A/* ARGSUSED3 */
1N/Aint
1N/Asm_stdopen(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 char *path = (char *) info;
1N/A int oflags;
1N/A
1N/A switch (SM_IO_MODE(flags))
1N/A {
1N/A case SM_IO_RDWR:
1N/A oflags = O_RDWR;
1N/A break;
1N/A case SM_IO_RDWRTR:
1N/A oflags = O_RDWR | O_CREAT | O_TRUNC;
1N/A break;
1N/A case SM_IO_RDONLY:
1N/A oflags = O_RDONLY;
1N/A break;
1N/A case SM_IO_WRONLY:
1N/A oflags = O_WRONLY | O_CREAT | O_TRUNC;
1N/A break;
1N/A case SM_IO_APPEND:
1N/A oflags = O_APPEND | O_WRONLY | O_CREAT;
1N/A break;
1N/A case SM_IO_APPENDRW:
1N/A oflags = O_APPEND | O_RDWR | O_CREAT;
1N/A break;
1N/A default:
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A#ifdef O_BINARY
1N/A if (SM_IS_BINARY(flags))
1N/A oflags |= O_BINARY;
1N/A#endif /* O_BINARY */
1N/A fp->f_file = open(path, oflags,
1N/A S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
1N/A if (fp->f_file < 0)
1N/A return -1; /* errno set by open() */
1N/A
1N/A if (oflags & O_APPEND)
1N/A (void) (*fp->f_seek)((void *)fp, (off_t)0, SEEK_END);
1N/A
1N/A return fp->f_file;
1N/A}
1N/A
1N/A/*
1N/A** SM_STDREAD -- read from the file
1N/A**
1N/A** Parameters:
1N/A** fp -- file pointer to read from
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: number of bytes read
1N/A**
1N/A** Side Effects:
1N/A** Updates internal offset into file.
1N/A*/
1N/A
1N/Assize_t
1N/Asm_stdread(fp, buf, n)
1N/A SM_FILE_T *fp;
1N/A char *buf;
1N/A size_t n;
1N/A{
1N/A register int ret;
1N/A
1N/A ret = read(fp->f_file, buf, n);
1N/A
1N/A /* if the read succeeded, update the current offset */
1N/A if (ret > 0)
1N/A fp->f_lseekoff += ret;
1N/A return ret;
1N/A}
1N/A
1N/A/*
1N/A** SM_STDWRITE -- write to the file
1N/A**
1N/A** Parameters:
1N/A** fp -- file pointer ro write to
1N/A** buf -- location of data to be written
1N/A** n - number of bytes to write
1N/A**
1N/A** Returns:
1N/A** Failure: -1 and sets errno
1N/A** Success: number of bytes written
1N/A*/
1N/A
1N/Assize_t
1N/Asm_stdwrite(fp, buf, n)
1N/A SM_FILE_T *fp;
1N/A char const *buf;
1N/A size_t n;
1N/A{
1N/A return write(fp->f_file, buf, n);
1N/A}
1N/A
1N/A/*
1N/A** SM_STDSEEK -- set the file offset position
1N/A**
1N/A** Parmeters:
1N/A** fp -- file pointer to position
1N/A** offset -- how far to position from "base" (set by 'whence')
1N/A** whence -- indicates where the "base" of the 'offset' to start
1N/A**
1N/A** Results:
1N/A** Failure: -1 and sets errno
1N/A** Success: the current offset
1N/A**
1N/A** Side Effects:
1N/A** Updates the internal value of the offset.
1N/A*/
1N/A
1N/Aoff_t
1N/Asm_stdseek(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
1N/A ret = lseek(fp->f_file, (off_t) offset, whence);
1N/A if (ret != (off_t) -1)
1N/A fp->f_lseekoff = ret;
1N/A return ret;
1N/A}
1N/A
1N/A/*
1N/A** SM_STDCLOSE -- close the file
1N/A**
1N/A** Parameters:
1N/A** fp -- the file pointer to close
1N/A**
1N/A** Returns:
1N/A** Success: 0 (zero)
1N/A** Failure: -1 and sets errno
1N/A*/
1N/A
1N/Aint
1N/Asm_stdclose(fp)
1N/A SM_FILE_T *fp;
1N/A{
1N/A return close(fp->f_file);
1N/A}
1N/A
1N/A/*
1N/A** SM_STDSETMODE -- set the access mode for the file
1N/A**
1N/A** Called by sm_stdsetinfo().
1N/A**
1N/A** Parameters:
1N/A** fp -- file pointer
1N/A** mode -- new mode to set the file access to
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_stdsetmode(fp, mode)
1N/A SM_FILE_T *fp;
1N/A const int *mode;
1N/A{
1N/A int flags = 0;
1N/A
1N/A switch (SM_IO_MODE(*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 default:
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A fp->f_flags = fp->f_flags & ~SMMODEMASK;
1N/A fp->f_flags |= flags;
1N/A return 0;
1N/A}
1N/A
1N/A/*
1N/A** SM_STDGETMODE -- for getinfo determine open mode
1N/A**
1N/A** Called by sm_stdgetinfo().
1N/A**
1N/A** Parameters:
1N/A** fp -- the file mode being determined
1N/A** mode -- internal mode to map to external value
1N/A**
1N/A** Results:
1N/A** Failure: -1 and sets errno
1N/A** Success: external mode value
1N/A*/
1N/A
1N/Astatic int
1N/Asm_stdgetmode(fp, mode)
1N/A SM_FILE_T *fp;
1N/A int *mode;
1N/A{
1N/A switch (fp->f_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_STDSETINFO -- set/modify information for a file
1N/A**
1N/A** Parameters:
1N/A** fp -- file to set info for
1N/A** what -- type of info to set
1N/A** valp -- location of data used for setting
1N/A**
1N/A** Returns:
1N/A** Failure: -1 and sets errno
1N/A** Success: >=0
1N/A*/
1N/A
1N/Aint
1N/Asm_stdsetinfo(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_stdsetmode(fp, (const int *)valp);
1N/A
1N/A default:
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A}
1N/A
1N/A/*
1N/A** SM_GETINFO -- get information about the open file
1N/A**
1N/A** Parameters:
1N/A** fp -- file to get info for
1N/A** what -- type of info to get
1N/A** valp -- location to place found info
1N/A**
1N/A** Returns:
1N/A** Success: may or may not place info in 'valp' depending
1N/A** on 'what' value, and returns values >=0. Return
1N/A** value may be the obtained info
1N/A** Failure: -1 and sets errno
1N/A*/
1N/A
1N/Aint
1N/Asm_stdgetinfo(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_stdgetmode(fp, (int *)valp);
1N/A
1N/A case SM_IO_WHAT_FD:
1N/A return fp->f_file;
1N/A
1N/A case SM_IO_WHAT_SIZE:
1N/A {
1N/A struct stat st;
1N/A
1N/A if (fstat(fp->f_file, &st) == 0)
1N/A return st.st_size;
1N/A else
1N/A return -1;
1N/A }
1N/A
1N/A case SM_IO_IS_READABLE:
1N/A {
1N/A fd_set readfds;
1N/A struct timeval timeout;
1N/A
1N/A if (SM_FD_SETSIZE > 0 && fp->f_file >= SM_FD_SETSIZE)
1N/A {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A FD_ZERO(&readfds);
1N/A SM_FD_SET(fp->f_file, &readfds);
1N/A timeout.tv_sec = 0;
1N/A timeout.tv_usec = 0;
1N/A if (select(fp->f_file + 1, FDSET_CAST &readfds,
1N/A NULL, NULL, &timeout) > 0 &&
1N/A SM_FD_ISSET(fp->f_file, &readfds))
1N/A return 1;
1N/A return 0;
1N/A }
1N/A
1N/A default:
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A}
1N/A
1N/A/*
1N/A** SM_STDFDOPEN -- open file by primitive 'fd' rather than pathname
1N/A**
1N/A** I/O function to handle fdopen() stdio equivalence. The rest of
1N/A** the functions are the same as the sm_stdopen() above.
1N/A**
1N/A** Parameters:
1N/A** fp -- the file pointer to be associated with the open
1N/A** name -- the primitive file descriptor for association
1N/A** flags -- indicates type of access methods
1N/A** rpool -- ignored
1N/A**
1N/A** Results:
1N/A** Success: primitive file descriptor value
1N/A** Failure: -1 and sets errno
1N/A*/
1N/A
1N/A/* ARGSUSED3 */
1N/Aint
1N/Asm_stdfdopen(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 int oflags, tmp, fdflags, fd = *((int *) info);
1N/A
1N/A switch (SM_IO_MODE(flags))
1N/A {
1N/A case SM_IO_RDWR:
1N/A oflags = O_RDWR | O_CREAT;
1N/A break;
1N/A case SM_IO_RDONLY:
1N/A oflags = O_RDONLY;
1N/A break;
1N/A case SM_IO_WRONLY:
1N/A oflags = O_WRONLY | O_CREAT | O_TRUNC;
1N/A break;
1N/A case SM_IO_APPEND:
1N/A oflags = O_APPEND | O_WRONLY | O_CREAT;
1N/A break;
1N/A case SM_IO_APPENDRW:
1N/A oflags = O_APPEND | O_RDWR | O_CREAT;
1N/A break;
1N/A default:
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A#ifdef O_BINARY
1N/A if (SM_IS_BINARY(flags))
1N/A oflags |= O_BINARY;
1N/A#endif /* O_BINARY */
1N/A
1N/A /* Make sure the mode the user wants is a subset of the actual mode. */
1N/A if ((fdflags = fcntl(fd, F_GETFL, 0)) < 0)
1N/A return -1;
1N/A tmp = fdflags & O_ACCMODE;
1N/A if (tmp != O_RDWR && (tmp != (oflags & O_ACCMODE)))
1N/A {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A fp->f_file = fd;
1N/A if (oflags & O_APPEND)
1N/A (void) (*fp->f_seek)(fp, (off_t)0, SEEK_END);
1N/A return fp->f_file;
1N/A}
1N/A
1N/A/*
1N/A** SM_IO_FOPEN -- open a file
1N/A**
1N/A** Same interface and semantics as the open() system call,
1N/A** except that it returns SM_FILE_T* instead of a file descriptor.
1N/A**
1N/A** Parameters:
1N/A** pathname -- path of file to open
1N/A** flags -- flags controlling the open
1N/A** ... -- option "mode" for opening the file
1N/A**
1N/A** Returns:
1N/A** Raises an exception on heap exhaustion.
1N/A** Returns NULL and sets errno if open() fails.
1N/A** Returns an SM_FILE_T pointer on success.
1N/A*/
1N/A
1N/ASM_FILE_T *
1N/A#if SM_VA_STD
1N/Asm_io_fopen(char *pathname, int flags, ...)
1N/A#else /* SM_VA_STD */
1N/Asm_io_fopen(pathname, flags, va_alist)
1N/A char *pathname;
1N/A int flags;
1N/A va_dcl
1N/A#endif /* SM_VA_STD */
1N/A{
1N/A MODE_T mode;
1N/A SM_FILE_T *fp;
1N/A int ioflags;
1N/A
1N/A if (flags & O_CREAT)
1N/A {
1N/A SM_VA_LOCAL_DECL
1N/A
1N/A SM_VA_START(ap, flags);
1N/A mode = (MODE_T) SM_VA_ARG(ap, int);
1N/A SM_VA_END(ap);
1N/A }
1N/A else
1N/A mode = 0;
1N/A
1N/A switch (flags & O_ACCMODE)
1N/A {
1N/A case O_RDONLY:
1N/A ioflags = SMRD;
1N/A break;
1N/A case O_WRONLY:
1N/A ioflags = SMWR;
1N/A break;
1N/A case O_RDWR:
1N/A ioflags = SMRW;
1N/A break;
1N/A default:
1N/A sm_abort("sm_io_fopen: bad flags 0%o", flags);
1N/A }
1N/A
1N/A fp = sm_fp(SmFtStdio, ioflags, NULL);
1N/A fp->f_file = open(pathname, flags, mode);
1N/A if (fp->f_file == -1)
1N/A {
1N/A fp->f_flags = 0;
1N/A fp->sm_magic = NULL;
1N/A return NULL;
1N/A }
1N/A return fp;
1N/A}