Blame lib/opendir.c

Packit 709fb3
/* Start reading the entries of a directory.
Packit 709fb3
   Copyright (C) 2006-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
#include <config.h>
Packit 709fb3
Packit 709fb3
/* Specification.  */
Packit 709fb3
#include <dirent.h>
Packit 709fb3
Packit 709fb3
#include <errno.h>
Packit 709fb3
#include <stddef.h>
Packit 709fb3
Packit 709fb3
#if HAVE_OPENDIR
Packit 709fb3
Packit 709fb3
/* Override opendir(), to keep track of the open file descriptors.
Packit 709fb3
   Needed because there is a function dirfd().  */
Packit 709fb3
Packit 709fb3
#else
Packit 709fb3
Packit 709fb3
# include <stdlib.h>
Packit 709fb3
Packit 709fb3
# include "dirent-private.h"
Packit 709fb3
# include "filename.h"
Packit 709fb3
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#if REPLACE_FCHDIR
Packit 709fb3
# include <unistd.h>
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#ifdef __KLIBC__
Packit 709fb3
# include <io.h>
Packit 709fb3
# include <fcntl.h>
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
DIR *
Packit 709fb3
opendir (const char *dir_name)
Packit 709fb3
{
Packit 709fb3
#if HAVE_OPENDIR
Packit 709fb3
# undef opendir
Packit 709fb3
  DIR *dirp;
Packit 709fb3
Packit 709fb3
  dirp = opendir (dir_name);
Packit 709fb3
  if (dirp == NULL)
Packit 709fb3
    return NULL;
Packit 709fb3
Packit 709fb3
# ifdef __KLIBC__
Packit 709fb3
  {
Packit 709fb3
    int fd = open (dir_name, O_RDONLY);
Packit 709fb3
    if (fd == -1 || _gl_register_dirp_fd (fd, dirp))
Packit 709fb3
      {
Packit 709fb3
        int saved_errno = errno;
Packit 709fb3
Packit 709fb3
        close (fd);
Packit 709fb3
        closedir (dirp);
Packit 709fb3
Packit 709fb3
        errno = saved_errno;
Packit 709fb3
Packit 709fb3
        return NULL;
Packit 709fb3
      }
Packit 709fb3
  }
Packit 709fb3
# endif
Packit 709fb3
#else
Packit 709fb3
Packit 709fb3
  char dir_name_mask[MAX_PATH + 1 + 1 + 1];
Packit 709fb3
  int status;
Packit 709fb3
  HANDLE current;
Packit 709fb3
  WIN32_FIND_DATA entry;
Packit 709fb3
  struct gl_directory *dirp;
Packit 709fb3
Packit 709fb3
  if (dir_name[0] == '\0')
Packit 709fb3
    {
Packit 709fb3
      errno = ENOENT;
Packit 709fb3
      return NULL;
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  /* Make the dir_name absolute, so that we continue reading the same
Packit 709fb3
     directory if the current directory changed between this opendir()
Packit 709fb3
     call and a subsequent rewinddir() call.  */
Packit 709fb3
  if (!GetFullPathName (dir_name, MAX_PATH, dir_name_mask, NULL))
Packit 709fb3
    {
Packit 709fb3
      errno = EINVAL;
Packit 709fb3
      return NULL;
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  /* Append the mask.
Packit 709fb3
     "*" and "*.*" appear to be equivalent.  */
Packit 709fb3
  {
Packit 709fb3
    char *p;
Packit 709fb3
Packit 709fb3
    p = dir_name_mask + strlen (dir_name_mask);
Packit 709fb3
    if (p > dir_name_mask && !ISSLASH (p[-1]))
Packit 709fb3
      *p++ = '\\';
Packit 709fb3
    *p++ = '*';
Packit 709fb3
    *p = '\0';
Packit 709fb3
  }
Packit 709fb3
Packit 709fb3
  /* Start searching the directory.  */
Packit 709fb3
  status = -1;
Packit 709fb3
  current = FindFirstFile (dir_name_mask, &entry);
Packit 709fb3
  if (current == INVALID_HANDLE_VALUE)
Packit 709fb3
    {
Packit 709fb3
      switch (GetLastError ())
Packit 709fb3
        {
Packit 709fb3
        case ERROR_FILE_NOT_FOUND:
Packit 709fb3
          status = -2;
Packit 709fb3
          break;
Packit 709fb3
        case ERROR_PATH_NOT_FOUND:
Packit 709fb3
          errno = ENOENT;
Packit 709fb3
          return NULL;
Packit 709fb3
        case ERROR_DIRECTORY:
Packit 709fb3
          errno = ENOTDIR;
Packit 709fb3
          return NULL;
Packit 709fb3
        case ERROR_ACCESS_DENIED:
Packit 709fb3
          errno = EACCES;
Packit 709fb3
          return NULL;
Packit 709fb3
        default:
Packit 709fb3
          errno = EIO;
Packit 709fb3
          return NULL;
Packit 709fb3
        }
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  /* Allocate the result.  */
Packit 709fb3
  dirp =
Packit 709fb3
    (struct gl_directory *)
Packit 709fb3
    malloc (offsetof (struct gl_directory, dir_name_mask[0])
Packit 709fb3
            + strlen (dir_name_mask) + 1);
Packit 709fb3
  if (dirp == NULL)
Packit 709fb3
    {
Packit 709fb3
      if (current != INVALID_HANDLE_VALUE)
Packit 709fb3
        FindClose (current);
Packit 709fb3
      errno = ENOMEM;
Packit 709fb3
      return NULL;
Packit 709fb3
    }
Packit 709fb3
  dirp->status = status;
Packit 709fb3
  dirp->current = current;
Packit 709fb3
  if (status == -1)
Packit 709fb3
    memcpy (&dirp->entry, &entry, sizeof (WIN32_FIND_DATA));
Packit 709fb3
  strcpy (dirp->dir_name_mask, dir_name_mask);
Packit 709fb3
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#if REPLACE_FCHDIR
Packit 709fb3
  {
Packit 709fb3
    int fd = dirfd (dirp);
Packit 709fb3
    if (0 <= fd && _gl_register_fd (fd, dir_name) != fd)
Packit 709fb3
      {
Packit 709fb3
        int saved_errno = errno;
Packit 709fb3
        closedir (dirp);
Packit 709fb3
        errno = saved_errno;
Packit 709fb3
        return NULL;
Packit 709fb3
      }
Packit 709fb3
  }
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
  return dirp;
Packit 709fb3
}