io.c 2.69 KB
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 1999, 2000 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Library Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include <io0.h>

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>

int
stream_create (stream_t *pstream, void *owner)
{
  stream_t stream;
  if (pstream == NULL || owner == NULL)
    return EINVAL;
  stream = calloc (1, sizeof (*stream));
  if (stream == NULL)
    return ENOMEM;
  stream->owner = owner;
  *pstream = stream;
  return 0;
}

void
stream_destroy (stream_t *pstream, void *owner)
{
  if (pstream && *pstream)
    {
      stream_t stream = *pstream;
      if (stream->owner == owner)
	free (stream);
      *pstream = NULL;
    }
}

int
stream_set_fd (stream_t stream, int (*_get_fd) (stream_t, int *), void *owner)
{
  if (stream == NULL)
    return EINVAL;
  if (owner == stream->owner)
    {
      stream->_get_fd = _get_fd;
      return 0;
    }
  return EACCES;
}

int
stream_set_read (stream_t stream, int (*_read)
		 (stream_t, char *, size_t, off_t, size_t *),
		 void *owner)
{
  if (stream == NULL)
    return EINVAL;
  if (owner == stream->owner)
    {
      stream->_read = _read;
      return 0;
    }
  return EACCES;
}

int
stream_set_write (stream_t stream, int (*_write)
		  __P ((stream_t, const char *, size_t, off_t, size_t *)),
		  void *owner)
{
  if (stream == NULL)
    return EINVAL;
  if (stream->owner == owner)
    {
      stream->_write = _write;
      return 0;
    }
  return EACCES;
}

int
stream_read (stream_t is, char *buf, size_t count,
	     off_t offset, size_t *pnread)
{
  if (is == NULL || is->_read == NULL)
    return EINVAL;
  return is->_read (is, buf, count, offset, pnread);
}

int
stream_write (stream_t os, const char *buf, size_t count,
	      off_t offset, size_t *pnwrite)
{
  if (os == NULL || os->_write == NULL)
      return EINVAL;
  return os->_write (os, buf, count, offset, pnwrite);
}

int
stream_get_fd (stream_t stream, int *pfd)
{
  if (stream == NULL || stream->_get_fd == NULL)
    return EINVAL;
  return stream->_get_fd (stream, pfd);
}