/*
* Copyright (c) 2000-2002, 2006 Sendmail, Inc. and its suppliers.
* All rights reserved.
* Copyright (c) 1990, 1993
* 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.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <syslog.h>
#include "local.h"
#include "glue.h"
/* An open type to map to fopen()-like behavior */
SM_TIME_BLOCK, "stdio" };
/* An open type to map to fdopen()-like behavior */
SM_TIME_BLOCK, "stdiofd" };
/* A string file type */
SM_TIME_BLOCK, "string" };
#if 0
/* A file type for syslog communications */
SM_TIME_BLOCK, "syslog" };
#endif /* 0 */
/* sm_magic p r w flags file bf lbfsize cookie ival */
/* A file type for interfacing to stdio FILE* streams. */
/* the usual - (stdin + stdout + stderr) */
/* List of builtin automagically already open file pointers */
{
};
/* Structure containing list of currently open file pointers */
/*
** SM_MOREGLUE -- adds more space for open file pointers
**
** Parameters:
** n -- number of new spaces for file pointers
**
** Returns:
** Raises an exception if no more memory.
** Otherwise, returns a pointer to new spaces.
*/
static struct sm_glue *
register int n;
{
register struct sm_glue *g;
register SM_FILE_T *p;
n * sizeof(SM_FILE_T));
g->gl_niobs = n;
g->gl_iobs = p;
while (--n >= 0)
*p++ = empty;
return g;
}
/*
** SM_FP -- allocate and initialize an SM_FILE structure
**
** Parameters:
** t -- file type requested to be opened.
** flags -- control flags for file type behavior
** oldfp -- file pointer to reuse if available (optional)
**
** Returns:
** Raises exception on memory exhaustion.
** Aborts if type is invalid.
** Otherwise, returns file pointer for requested file type.
*/
const SM_FILE_T *t;
const int flags;
{
register int n;
register struct sm_glue *g;
if (!Sm_IO_DidInit)
sm_init();
{
goto found; /* for opening reusing an 'fp' */
}
{
goto found;
}
else
return fp;
}
/*
** SM_CLEANUP -- cleanup function when exit called.
**
** This function is registered via atexit().
**
** Parameters:
** none
**
** Returns:
** nothing.
**
** Side Effects:
** flushes open files before they are forced closed
*/
void
{
}
/*
** SM_INIT -- called whenever sm_io's internal variables must be set up.
**
** Parameters:
** none
**
** Returns:
** none
**
** Side Effects:
** Registers sm_cleanup() using atexit().
*/
void
sm_init()
{
if (Sm_IO_DidInit) /* paranoia */
return;
/* more paranoia: initialize pointers in a static variable */
/* make sure we clean up on exit */
Sm_IO_DidInit = true;
}
/*
** SM_IO_SETINFO -- change info for an open file type (fp)
**
** The generic SM_IO_WHAT_VECTORS is auto supplied for all file types.
** If the request is to set info other than SM_IO_WHAT_VECTORS then
** the request is passed on to the file type's specific setinfo vector.
**
** Parameters:
** fp -- file to make the setting on
** what -- type of information to set
** valp -- structure to obtain info from
**
** Returns:
** 0 on success
** -1 on error and sets errno:
** - when what != SM_IO_WHAT_VECTORS and setinfo vector
** not set
** - when vectored setinfo returns -1
*/
int
int what;
void *valp;
{
switch (what)
{
case SM_IO_WHAT_VECTORS:
/*
** This is the "generic" available for all.
** This allows the function vectors to be replaced
** while the file type is active.
*/
return 0;
case SM_IO_WHAT_TIMEOUT:
return 0;
}
/* Otherwise the vector will check it out */
{
return -1;
}
else
}
/*
** SM_IO_GETINFO -- get information for an active file type (fp)
**
** This function supplies for all file types the answers for the
** three requests SM_IO_WHAT_VECTORS, SM_IO_WHAT_TYPE and
** SM_IO_WHAT_ISTYPE. Other requests are handled by the getinfo
** vector if available for the open file type.
** SM_IO_WHAT_VECTORS returns information for the file pointer vectors.
** SM_IO_WHAT_TYPE returns the type identifier for the file pointer
** SM_IO_WHAT_ISTYPE returns >0 if the passed in type matches the
** file pointer's type.
** SM_IO_IS_READABLE returns 1 if there is data available for reading,
** 0 otherwise.
**
** Parameters:
** fp -- file pointer for active file type
** what -- type of information request
** valp -- structure to place obtained info into
**
** Returns:
** -1 on error and sets errno:
** - when valp==NULL and request expects otherwise
** - when request is not SM_IO_WHAT_VECTORS and not
** SM_IO_WHAT_TYPE and not SM_IO_WHAT_ISTYPE
** and getinfo vector is NULL
** - when getinfo type vector returns -1
** >=0 on success
*/
int
int what;
void *valp;
{
switch (what)
{
case SM_IO_WHAT_VECTORS:
{
return -1;
}
/* This is the "generic" available for all */
return 0;
case SM_IO_WHAT_TYPE:
{
return -1;
}
return 0;
case SM_IO_WHAT_ISTYPE:
{
return -1;
}
case SM_IO_IS_READABLE:
/* if there is data in the buffer, it must be readable */
return 1;
/* otherwise query the underlying file */
break;
case SM_IO_WHAT_TIMEOUT:
return 0;
case SM_IO_WHAT_FD:
/* try the file type specific getinfo to see if it knows */
break;
}
/* Otherwise the vector will check it out */
{
return -1;
}
}