Blame gnulib-tests/ioctl.c

Packit 33f14e
/* ioctl.c --- wrappers for Windows ioctl function
Packit 33f14e
Packit 33f14e
   Copyright (C) 2008-2017 Free Software Foundation, Inc.
Packit 33f14e
Packit 33f14e
   This program is free software: you can redistribute it and/or modify
Packit 33f14e
   it under the terms of the GNU General Public License as published by
Packit 33f14e
   the Free Software Foundation; either version 3 of the License, or
Packit 33f14e
   (at your option) any later version.
Packit 33f14e
Packit 33f14e
   This program is distributed in the hope that it will be useful,
Packit 33f14e
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 33f14e
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 33f14e
   GNU General Public License for more details.
Packit 33f14e
Packit 33f14e
   You should have received a copy of the GNU General Public License
Packit 33f14e
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 33f14e
Packit 33f14e
/* Written by Paolo Bonzini */
Packit 33f14e
Packit 33f14e
#include <config.h>
Packit 33f14e
Packit 33f14e
#include <sys/ioctl.h>
Packit 33f14e
Packit 33f14e
#include <stdarg.h>
Packit 33f14e
Packit 33f14e
#if HAVE_IOCTL
Packit 33f14e
Packit 33f14e
/* Provide a wrapper with the POSIX prototype.  */
Packit 33f14e
# undef ioctl
Packit 33f14e
int
Packit 33f14e
rpl_ioctl (int fd, int request, ... /* {void *,char *} arg */)
Packit 33f14e
{
Packit 33f14e
  void *buf;
Packit 33f14e
  va_list args;
Packit 33f14e
Packit 33f14e
  va_start (args, request);
Packit 33f14e
  buf = va_arg (args, void *);
Packit 33f14e
  va_end (args);
Packit 33f14e
Packit 33f14e
  /* Cast 'request' so that when the system's ioctl function takes a 64-bit
Packit 33f14e
     request argument, the value gets zero-extended, not sign-extended.  */
Packit 33f14e
  return ioctl (fd, (unsigned int) request, buf);
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
#else /* mingw */
Packit 33f14e
Packit 33f14e
# include <errno.h>
Packit 33f14e
Packit 33f14e
/* Get HANDLE.  */
Packit 33f14e
# define WIN32_LEAN_AND_MEAN
Packit 33f14e
# include <windows.h>
Packit 33f14e
Packit 33f14e
# include "fd-hook.h"
Packit 33f14e
/* Get _get_osfhandle.  */
Packit 33f14e
# if GNULIB_MSVC_NOTHROW
Packit 33f14e
#  include "msvc-nothrow.h"
Packit 33f14e
# else
Packit 33f14e
#  include <io.h>
Packit 33f14e
# endif
Packit 33f14e
Packit 33f14e
static int
Packit 33f14e
primary_ioctl (int fd, int request, void *arg)
Packit 33f14e
{
Packit 33f14e
  /* We don't support FIONBIO on pipes here.  If you want to make pipe
Packit 33f14e
     fds non-blocking, use the gnulib 'nonblocking' module, until
Packit 33f14e
     gnulib implements fcntl F_GETFL / F_SETFL with O_NONBLOCK.  */
Packit 33f14e
Packit 33f14e
  if ((HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE)
Packit 33f14e
    errno = ENOSYS;
Packit 33f14e
  else
Packit 33f14e
    errno = EBADF;
Packit 33f14e
  return -1;
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
int
Packit 33f14e
ioctl (int fd, int request, ... /* {void *,char *} arg */)
Packit 33f14e
{
Packit 33f14e
  void *arg;
Packit 33f14e
  va_list args;
Packit 33f14e
Packit 33f14e
  va_start (args, request);
Packit 33f14e
  arg = va_arg (args, void *);
Packit 33f14e
  va_end (args);
Packit 33f14e
Packit 33f14e
# if WINDOWS_SOCKETS
Packit 33f14e
  return execute_all_ioctl_hooks (primary_ioctl, fd, request, arg);
Packit 33f14e
# else
Packit 33f14e
  return primary_ioctl (fd, request, arg);
Packit 33f14e
# endif
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
#endif