Blame hurd/fopenport.c

Packit 6c4009
/* Copyright (C) 1994-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
/* Read up to N chars into BUF from COOKIE.
Packit 6c4009
   Return how many chars were read, 0 for EOF or -1 for error.  */
Packit 6c4009
static ssize_t
Packit 6c4009
readio (void *cookie, char *buf, size_t n)
Packit 6c4009
{
Packit 6c4009
  mach_msg_type_number_t nread;
Packit 6c4009
  error_t err;
Packit 6c4009
  char *bufp = buf;
Packit 6c4009
Packit 6c4009
  nread = n;
Packit 6c4009
  if (err = __io_read ((io_t) cookie, &bufp, &nread, -1, n))
Packit 6c4009
    return __hurd_fail (err);
Packit 6c4009
Packit 6c4009
  if (bufp != buf)
Packit 6c4009
    {
Packit 6c4009
      memcpy (buf, bufp, nread);
Packit 6c4009
      __vm_deallocate (__mach_task_self (),
Packit 6c4009
		       (vm_address_t) bufp, (vm_size_t) nread);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return nread;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Write up to N chars from BUF to COOKIE.
Packit 6c4009
   Return how many chars were written or -1 for error.  */
Packit 6c4009
static ssize_t
Packit 6c4009
writeio (void *cookie, const char *buf, size_t n)
Packit 6c4009
{
Packit 6c4009
  mach_msg_type_number_t wrote;
Packit 6c4009
  error_t err;
Packit 6c4009
Packit 6c4009
  if (err = __io_write ((io_t) cookie, buf, n, -1, &wrote))
Packit 6c4009
    return __hurd_fail (err);
Packit 6c4009
Packit 6c4009
  return wrote;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Move COOKIE's file position *POS bytes, according to WHENCE.
Packit 6c4009
   The current file position is stored in *POS.
Packit 6c4009
   Returns zero if successful, nonzero if not.  */
Packit 6c4009
static int
Packit 6c4009
seekio (void *cookie,
Packit 6c4009
	off64_t *pos,
Packit 6c4009
	int whence)
Packit 6c4009
{
Packit 6c4009
  error_t err = __io_seek ((file_t) cookie, *pos, whence, pos);
Packit 6c4009
  return err ? __hurd_fail (err) : 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Close the file associated with COOKIE.
Packit 6c4009
   Return 0 for success or -1 for failure.  */
Packit 6c4009
static int
Packit 6c4009
closeio (void *cookie)
Packit 6c4009
{
Packit 6c4009
  error_t error = __mach_port_deallocate (__mach_task_self (),
Packit 6c4009
					  (mach_port_t) cookie);
Packit 6c4009
  if (error)
Packit 6c4009
    return __hurd_fail (error);
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include "../libio/libioP.h"
Packit 6c4009
#define fopencookie _IO_fopencookie
Packit 6c4009
static const cookie_io_functions_t funcsio =
Packit 6c4009
{ readio, writeio, seekio, closeio };
Packit 6c4009

Packit 6c4009
Packit 6c4009
/* Open a stream on PORT.  MODE is as for fopen.  */
Packit 6c4009
Packit 6c4009
FILE *
Packit 6c4009
__fopenport (mach_port_t port, const char *mode)
Packit 6c4009
{
Packit 6c4009
  int pflags;
Packit 6c4009
  int needflags;
Packit 6c4009
  error_t err;
Packit 6c4009
Packit 6c4009
  const char *m = mode;
Packit 6c4009
Packit 6c4009
  switch (*m++)
Packit 6c4009
    {
Packit 6c4009
    case 'r':
Packit 6c4009
      needflags = O_READ;
Packit 6c4009
      break;
Packit 6c4009
    case 'w':
Packit 6c4009
      needflags = O_WRITE;
Packit 6c4009
      break;
Packit 6c4009
    case 'a':
Packit 6c4009
      needflags = O_WRITE|O_APPEND;
Packit 6c4009
      break;
Packit 6c4009
    default:
Packit 6c4009
      return NULL;
Packit 6c4009
  }
Packit 6c4009
  if (m[0] == '+' || (m[0] == 'b' && m[1] == '+'))
Packit 6c4009
    needflags |= O_RDWR;
Packit 6c4009
Packit 6c4009
  /* Verify the PORT is valid allows the access MODE specifies.  */
Packit 6c4009
Packit 6c4009
  if (err = __io_get_openmodes (port, &pflags))
Packit 6c4009
    return __hurd_fail (err), NULL;
Packit 6c4009
Packit 6c4009
  /* Check the access mode.  */
Packit 6c4009
  if ((pflags & needflags) != needflags)
Packit 6c4009
    {
Packit 6c4009
      errno = EBADF;
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return fopencookie ((void *) port, mode, funcsio);
Packit 6c4009
}
Packit 6c4009
weak_alias (__fopenport, fopenport)