Blame gnulib-tests/ioctl.c

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