Blame gnulib-tests/test-fdopendir.c

Packit 709fb3
/* Test opening a directory stream from a file descriptor.
Packit 709fb3
   Copyright (C) 2009-2017 Free Software Foundation, Inc.
Packit 709fb3
Packit 709fb3
   This program is free software: you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3 of the License, or
Packit 709fb3
   (at your option) any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
/* Written by Eric Blake <ebb9@byu.net>, 2009.  */
Packit 709fb3
Packit 709fb3
#include <config.h>
Packit 709fb3
Packit 709fb3
#include <dirent.h>
Packit 709fb3
Packit 709fb3
#include "signature.h"
Packit 709fb3
SIGNATURE_CHECK (fdopendir, DIR *, (int));
Packit 709fb3
Packit 709fb3
#include <errno.h>
Packit 709fb3
#include <fcntl.h>
Packit 709fb3
#include <unistd.h>
Packit 709fb3
Packit 709fb3
#include "macros.h"
Packit 709fb3
Packit 709fb3
int
Packit 709fb3
main (int argc _GL_UNUSED, char *argv[])
Packit 709fb3
{
Packit 709fb3
  DIR *d;
Packit 709fb3
  int fd;
Packit 709fb3
Packit 709fb3
  /* A non-directory cannot be turned into a directory stream.  */
Packit 709fb3
  fd = open ("test-fdopendir.tmp", O_RDONLY | O_CREAT, 0600);
Packit 709fb3
  ASSERT (0 <= fd);
Packit 709fb3
  errno = 0;
Packit 709fb3
  ASSERT (fdopendir (fd) == NULL);
Packit 709fb3
  ASSERT (errno == ENOTDIR);
Packit 709fb3
  ASSERT (close (fd) == 0);
Packit 709fb3
  ASSERT (unlink ("test-fdopendir.tmp") == 0);
Packit 709fb3
Packit 709fb3
  /* A bad fd cannot be turned into a stream.  */
Packit 709fb3
  {
Packit 709fb3
    errno = 0;
Packit 709fb3
    ASSERT (fdopendir (-1) == NULL);
Packit 709fb3
    ASSERT (errno == EBADF);
Packit 709fb3
  }
Packit 709fb3
  {
Packit 709fb3
    close (99);
Packit 709fb3
    errno = 0;
Packit 709fb3
    ASSERT (fdopendir (99) == NULL);
Packit 709fb3
    ASSERT (errno == EBADF);
Packit 709fb3
  }
Packit 709fb3
Packit 709fb3
  /* This should work.  */
Packit 709fb3
  fd = open (".", O_RDONLY);
Packit 709fb3
  ASSERT (0 <= fd);
Packit 709fb3
  d = fdopendir (fd);
Packit 709fb3
  ASSERT (d);
Packit 709fb3
  /* fdopendir should not close fd.  */
Packit 709fb3
  ASSERT (dup2 (fd, fd) == fd);
Packit 709fb3
Packit 709fb3
  /* Don't test dirfd here.  dirfd (d) must return fd on current POSIX
Packit 709fb3
     platforms, but on pre-2008 platforms or on non-POSIX platforms
Packit 709fb3
     dirfd (fd) might return some other descriptor, or -1, and gnulib
Packit 709fb3
     does not work around this porting problem.  */
Packit 709fb3
Packit 709fb3
  ASSERT (closedir (d) == 0);
Packit 709fb3
  /* Now we can guarantee that fd must be closed.  */
Packit 709fb3
  errno = 0;
Packit 709fb3
  ASSERT (dup2 (fd, fd) == -1);
Packit 709fb3
  ASSERT (errno == EBADF);
Packit 709fb3
Packit 709fb3
  return 0;
Packit 709fb3
}