Blame sysdeps/mach/hurd/opendir.c

Packit 6c4009
/* Copyright (C) 1993-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 <errno.h>
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <dirent.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <hurd/fd.h>
Packit 6c4009
#include <not-cancel.h>
Packit 6c4009
#include "dirstream.h"
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Open a directory stream on a file descriptor in Hurd internal form.
Packit 6c4009
   We do no checking here on the descriptor.  */
Packit 6c4009
DIR *
Packit 6c4009
_hurd_fd_opendir (struct hurd_fd *d)
Packit 6c4009
{
Packit 6c4009
  DIR *dirp;
Packit 6c4009
Packit 6c4009
  if (d == NULL)
Packit 6c4009
    {
Packit 6c4009
      errno = EBADF;
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  dirp = (DIR *) malloc (sizeof (DIR));
Packit 6c4009
  if (dirp == NULL)
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  /* Set the descriptor to close on exec. */
Packit 6c4009
  HURD_CRITICAL_BEGIN;
Packit 6c4009
  __spin_lock (&d->port.lock);
Packit 6c4009
  d->flags |= FD_CLOEXEC;
Packit 6c4009
  __spin_unlock (&d->port.lock);
Packit 6c4009
  HURD_CRITICAL_END;
Packit 6c4009
Packit 6c4009
  dirp->__fd = d;
Packit 6c4009
  dirp->__data = dirp->__ptr = NULL;
Packit 6c4009
  dirp->__entry_data = dirp->__entry_ptr = 0;
Packit 6c4009
  dirp->__allocation = 0;
Packit 6c4009
  dirp->__size = 0;
Packit 6c4009
Packit 6c4009
  __libc_lock_init (dirp->__lock);
Packit 6c4009
Packit 6c4009
  return dirp;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
DIR *
Packit 6c4009
__opendirat (int dfd, const char *name)
Packit 6c4009
{
Packit 6c4009
  if (name[0] == '\0')
Packit 6c4009
    {
Packit 6c4009
      /* POSIX.1-1990 says an empty name gets ENOENT;
Packit 6c4009
	 but `open' might like it fine.  */
Packit 6c4009
      __set_errno (ENOENT);
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int flags = O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC;
Packit 6c4009
  int fd;
Packit 6c4009
#if IS_IN (rtld)
Packit 6c4009
  assert (dfd == AT_FDCWD);
Packit 6c4009
  fd = __open_nocancel (name, flags);
Packit 6c4009
#else
Packit 6c4009
  fd = __openat_nocancel (dfd, name, flags);
Packit 6c4009
#endif
Packit 6c4009
  if (fd < 0)
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  /* Extract the pointer to the descriptor structure.  */
Packit 6c4009
  DIR *dirp = _hurd_fd_opendir (_hurd_fd_get (fd));
Packit 6c4009
  if (dirp == NULL)
Packit 6c4009
    __close (fd);
Packit 6c4009
Packit 6c4009
  return dirp;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Open a directory stream on NAME.  */
Packit 6c4009
DIR *
Packit 6c4009
__opendir (const char *name)
Packit 6c4009
{
Packit 6c4009
#if 0 /* TODO.  */
Packit 6c4009
  return __opendirat (AT_FDCWD, name);
Packit 6c4009
#else
Packit 6c4009
  if (name[0] == '\0')
Packit 6c4009
    {
Packit 6c4009
      /* POSIX.1-1990 says an empty name gets ENOENT;
Packit 6c4009
	 but `open' might like it fine.  */
Packit 6c4009
      __set_errno (ENOENT);
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int fd = __open (name, O_RDONLY | O_NONBLOCK | O_DIRECTORY);
Packit 6c4009
  if (fd < 0)
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  /* Extract the pointer to the descriptor structure.  */
Packit 6c4009
  DIR *dirp = _hurd_fd_opendir (_hurd_fd_get (fd));
Packit 6c4009
  if (dirp == NULL)
Packit 6c4009
    __close (fd);
Packit 6c4009
Packit 6c4009
  return dirp;
Packit 6c4009
#endif
Packit 6c4009
}
Packit 6c4009
weak_alias (__opendir, opendir)