io.h revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* Copyright (c) 2000-2002, 2004 Sendmail, Inc. and its suppliers.
* All rights reserved.
* Copyright (c) 1990
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Chris Torek.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*
* $Id: io.h,v 1.24 2004/03/03 19:14:49 ca Exp $
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*-
* @(#)stdio.h 5.17 (Berkeley) 6/3/91
*/
#ifndef SM_IO_H
#define SM_IO_H
#include <stdio.h>
/* mode for sm io (exposed) */
/* for sm_io_fseek, et al api's (exposed) */
#define SM_IO_SEEK_SET 0
#define SM_IO_SEEK_CUR 1
#define SM_IO_SEEK_END 2
/* flags for info what's with different types (exposed) */
#define SM_IO_WHAT_MODE 1
#define SM_IO_WHAT_VECTORS 2
#define SM_IO_WHAT_FD 3
#define SM_IO_WHAT_TYPE 4
#define SM_IO_WHAT_ISTYPE 5
#define SM_IO_IS_READABLE 6
#define SM_IO_WHAT_TIMEOUT 7
#define SM_IO_WHAT_SIZE 8
/* info flags (exposed) */
#define SM_IO_FTYPE_CREATE 1
#define SM_IO_FTYPE_MODIFY 2
#define SM_IO_FTYPE_DELETE 3
#define SM_IO_SL_PRIO 1
#define SM_IO_OPEN_MAX 20
/* for internal buffers */
struct smbuf
{
unsigned char *smb_base;
int smb_size;
};
/*
** sm I/O state variables (internal only).
**
** The following always hold:
**
** if (flags&(SMLBF|SMWR)) == (SMLBF|SMWR),
** lbfsize is -bf.size, else lbfsize is 0
** if flags&SMRD, w is 0
** if flags&SMWR, r is 0
**
** This ensures that the getc and putc macros (or inline functions) never
** try to write or read from a file that is in `read' or `write' mode.
** (Moreover, they can, and do, automatically switch from read mode to
** write mode, and back, on "r+" and "w+" files.)
**
** lbfsize is used only to make the inline line-buffered output stream
** code as compact as possible.
**
** ub, up, and ur are used when ungetc() pushes back more characters
** than fit in the current bf, or when ungetc() pushes back a character
** that does not match the previous one in bf. When this happens,
** ub.base becomes non-nil (i.e., a stream has ungetc() data iff
** ub.base!=NULL) and up and ur save the current values of p and r.
*/
struct sm_file
{
const char *sm_magic; /* This SM_FILE_T is free when NULL */
unsigned char *f_p; /* current position in (some) buffer */
int f_r; /* read space left for getc() */
int f_w; /* write space left for putc() */
long f_flags; /* flags, below */
short f_file; /* fileno, if Unix fd, else -1 */
/* These can be used for any purpose by a file type implementation: */
void *f_cookie;
int f_ival;
/* operations */
const void *));
int f_timeout;
int f_timeoutstate; /* either blocking or non-blocking */
char *f_type; /* for by-type lookups */
/* separate buffer for long sequences of ungetc() */
unsigned char *f_up; /* saved f_p when f_p is doing ungetc */
int f_ur; /* saved f_r when f_r is counting ungetc */
/* tricks to meet minimum requirements even when malloc() fails */
/* Unix stdio files get aligned to block boundaries on fseek() */
int f_dup_cnt; /* count file dup'd */
};
extern const char SmFileMagic[];
extern SM_FILE_T SmFtStdio_def;
extern SM_FILE_T SmFtStdiofd_def;
extern SM_FILE_T SmFtString_def;
extern SM_FILE_T SmFtSyslog_def;
extern SM_FILE_T SmFtRealStdio_def;
#define SMIOIN_FILENO 0
#define SMIOOUT_FILENO 1
#define SMIOERR_FILENO 2
#define SMIOSTDIN_FILENO 3
#define SMIOSTDOUT_FILENO 4
#define SMIOSTDERR_FILENO 5
/* Common predefined and already (usually) open files (exposed) */
#define SmFtStdio (&SmFtStdio_def)
#define SmFtStdiofd (&SmFtStdiofd_def)
#define SmFtString (&SmFtString_def)
#define SmFtSyslog (&SmFtSyslog_def)
#define SmFtRealStdio (&SmFtRealStdio_def)
#ifdef __STDC__
(f) = {SmFileMagic, (unsigned char *) 0, 0, 0, 0L, -1, {0}, 0, (void *) 0,\
0, (name)}
#else /* __STDC__ */
(f).sm_magic = SmFileMagic; \
(f).f_p = (unsigned char *) 0; \
(f).f_r = 0; \
(f).f_w = 0; \
(f).f_flags = 0L; \
(f).f_file = 0; \
(f).f_lbfsize = 0; \
(f).f_cookie = (void *) 0; \
(f).f_ival = 0; \
(f).f_timeoutstate = 0; \
#endif /* __STDC__ */
/* Internal flags */
/* RD and WR are never simultaneously asserted */
/* defines for timeout constants */
#define SM_TIME_IMMEDIATE (0)
#define SM_TIME_FOREVER (-1)
#define SM_TIME_DEFAULT (-2)
/* timeout state for blocking */
#define SM_TIME_BLOCK (0) /* XXX just bool? */
#define SM_TIME_NONBLOCK (1)
/* Exposed buffering type flags */
#define SM_IO_FBF 0 /* setvbuf should set fully buffered */
/* setvbuf buffered, but through at lower file type layers */
#define SM_IO_NOW 3
/*
** size of buffer used by setbuf.
** If underlying filesystem blocksize is discoverable that is used instead
*/
#define SM_IO_BUFSIZ 4096
#define SM_IO_EOF (-1)
/* Functions defined in ANSI C standard. */
int, const void *));
const void *, int, const void *, SM_FILE_T *));
int SM_NONVOLATILE));
sm_io_sscanf __P((const char *, char const *, ...));
extern SM_FILE_T *
char *_pathname,
int _flags,
...));
extern SM_FILE_T *
char *_mode));
extern int
char **_str,
const char *_fmt,
extern int
char *,
const char *,
va_list));
extern void
const char *));
/*
** Functions internal to the implementation.
*/
/*
** The macros are here so that we can
** define function versions in the library.
*/
#define sm_getc(f, t) \
(--(f)->f_r < 0 ? \
sm_rget(f, t) : \
(int)(*(f)->f_p++))
/*
** This has been tuned to generate reasonable code on the vax using pcc.
** (It also generates reasonable x86 code using gcc.)
*/
#define sm_putc(f, t, c) \
(--(f)->f_w < 0 ? \
(int)*(f)->f_p++ : \
sm_wbuf(f, t, '\n') : \
sm_wbuf(f, t, (int)(c)) : \
#define sm_io_error(p) sm_error(p)
#define sm_io_clearerr(p) sm_clearerr(p)
#ifndef lint
# ifndef _POSIX_SOURCE
# endif /* _POSIX_SOURCE */
#endif /* lint */
#endif /* SM_IO_H */