Blame lib/opendir.c

Packit 8f70b4
/* Start reading the entries of a directory.
Packit 8f70b4
   Copyright (C) 2006-2018 Free Software Foundation, Inc.
Packit 8f70b4
Packit 8f70b4
   This program is free software: you can redistribute it and/or modify
Packit 8f70b4
   it under the terms of the GNU General Public License as published by
Packit 8f70b4
   the Free Software Foundation; either version 3 of the License, or
Packit 8f70b4
   (at your option) any later version.
Packit 8f70b4
Packit 8f70b4
   This program is distributed in the hope that it will be useful,
Packit 8f70b4
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
   GNU General Public License for more details.
Packit 8f70b4
Packit 8f70b4
   You should have received a copy of the GNU General Public License
Packit 8f70b4
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 8f70b4
Packit 8f70b4
#include <config.h>
Packit 8f70b4
Packit 8f70b4
/* Specification.  */
Packit 8f70b4
#include <dirent.h>
Packit 8f70b4
Packit 8f70b4
#include <errno.h>
Packit 8f70b4
#include <stddef.h>
Packit 8f70b4
Packit 8f70b4
#if HAVE_OPENDIR
Packit 8f70b4
Packit 8f70b4
/* Override opendir(), to keep track of the open file descriptors.
Packit 8f70b4
   Needed because there is a function dirfd().  */
Packit 8f70b4
Packit 8f70b4
#else
Packit 8f70b4
Packit 8f70b4
# include <stdlib.h>
Packit 8f70b4
Packit 8f70b4
# include "dirent-private.h"
Packit 8f70b4
# include "filename.h"
Packit 8f70b4
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
#if REPLACE_FCHDIR
Packit 8f70b4
# include <unistd.h>
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
#ifdef __KLIBC__
Packit 8f70b4
# include <io.h>
Packit 8f70b4
# include <fcntl.h>
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
DIR *
Packit 8f70b4
opendir (const char *dir_name)
Packit 8f70b4
{
Packit 8f70b4
#if HAVE_OPENDIR
Packit 8f70b4
# undef opendir
Packit 8f70b4
  DIR *dirp;
Packit 8f70b4
Packit 8f70b4
  dirp = opendir (dir_name);
Packit 8f70b4
  if (dirp == NULL)
Packit 8f70b4
    return NULL;
Packit 8f70b4
Packit 8f70b4
# ifdef __KLIBC__
Packit 8f70b4
  {
Packit 8f70b4
    int fd = open (dir_name, O_RDONLY);
Packit 8f70b4
    if (fd == -1 || _gl_register_dirp_fd (fd, dirp))
Packit 8f70b4
      {
Packit 8f70b4
        int saved_errno = errno;
Packit 8f70b4
Packit 8f70b4
        close (fd);
Packit 8f70b4
        closedir (dirp);
Packit 8f70b4
Packit 8f70b4
        errno = saved_errno;
Packit 8f70b4
Packit 8f70b4
        return NULL;
Packit 8f70b4
      }
Packit 8f70b4
  }
Packit 8f70b4
# endif
Packit 8f70b4
#else
Packit 8f70b4
Packit 8f70b4
  char dir_name_mask[MAX_PATH + 1 + 1 + 1];
Packit 8f70b4
  int status;
Packit 8f70b4
  HANDLE current;
Packit 8f70b4
  WIN32_FIND_DATA entry;
Packit 8f70b4
  struct gl_directory *dirp;
Packit 8f70b4
Packit 8f70b4
  if (dir_name[0] == '\0')
Packit 8f70b4
    {
Packit 8f70b4
      errno = ENOENT;
Packit 8f70b4
      return NULL;
Packit 8f70b4
    }
Packit 8f70b4
Packit 8f70b4
  /* Make the dir_name absolute, so that we continue reading the same
Packit 8f70b4
     directory if the current directory changed between this opendir()
Packit 8f70b4
     call and a subsequent rewinddir() call.  */
Packit 8f70b4
  if (!GetFullPathName (dir_name, MAX_PATH, dir_name_mask, NULL))
Packit 8f70b4
    {
Packit 8f70b4
      errno = EINVAL;
Packit 8f70b4
      return NULL;
Packit 8f70b4
    }
Packit 8f70b4
Packit 8f70b4
  /* Append the mask.
Packit 8f70b4
     "*" and "*.*" appear to be equivalent.  */
Packit 8f70b4
  {
Packit 8f70b4
    char *p;
Packit 8f70b4
Packit 8f70b4
    p = dir_name_mask + strlen (dir_name_mask);
Packit 8f70b4
    if (p > dir_name_mask && !ISSLASH (p[-1]))
Packit 8f70b4
      *p++ = '\\';
Packit 8f70b4
    *p++ = '*';
Packit 8f70b4
    *p = '\0';
Packit 8f70b4
  }
Packit 8f70b4
Packit 8f70b4
  /* Start searching the directory.  */
Packit 8f70b4
  status = -1;
Packit 8f70b4
  current = FindFirstFile (dir_name_mask, &entry);
Packit 8f70b4
  if (current == INVALID_HANDLE_VALUE)
Packit 8f70b4
    {
Packit 8f70b4
      switch (GetLastError ())
Packit 8f70b4
        {
Packit 8f70b4
        case ERROR_FILE_NOT_FOUND:
Packit 8f70b4
          status = -2;
Packit 8f70b4
          break;
Packit 8f70b4
        case ERROR_PATH_NOT_FOUND:
Packit 8f70b4
          errno = ENOENT;
Packit 8f70b4
          return NULL;
Packit 8f70b4
        case ERROR_DIRECTORY:
Packit 8f70b4
          errno = ENOTDIR;
Packit 8f70b4
          return NULL;
Packit 8f70b4
        case ERROR_ACCESS_DENIED:
Packit 8f70b4
          errno = EACCES;
Packit 8f70b4
          return NULL;
Packit 8f70b4
        default:
Packit 8f70b4
          errno = EIO;
Packit 8f70b4
          return NULL;
Packit 8f70b4
        }
Packit 8f70b4
    }
Packit 8f70b4
Packit 8f70b4
  /* Allocate the result.  */
Packit 8f70b4
  dirp =
Packit 8f70b4
    (struct gl_directory *)
Packit 8f70b4
    malloc (offsetof (struct gl_directory, dir_name_mask[0])
Packit 8f70b4
            + strlen (dir_name_mask) + 1);
Packit 8f70b4
  if (dirp == NULL)
Packit 8f70b4
    {
Packit 8f70b4
      if (current != INVALID_HANDLE_VALUE)
Packit 8f70b4
        FindClose (current);
Packit 8f70b4
      errno = ENOMEM;
Packit 8f70b4
      return NULL;
Packit 8f70b4
    }
Packit 8f70b4
  dirp->status = status;
Packit 8f70b4
  dirp->current = current;
Packit 8f70b4
  if (status == -1)
Packit 8f70b4
    memcpy (&dirp->entry, &entry, sizeof (WIN32_FIND_DATA));
Packit 8f70b4
  strcpy (dirp->dir_name_mask, dir_name_mask);
Packit 8f70b4
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
#if REPLACE_FCHDIR
Packit 8f70b4
  {
Packit 8f70b4
    int fd = dirfd (dirp);
Packit 8f70b4
    if (0 <= fd && _gl_register_fd (fd, dir_name) != fd)
Packit 8f70b4
      {
Packit 8f70b4
        int saved_errno = errno;
Packit 8f70b4
        closedir (dirp);
Packit 8f70b4
        errno = saved_errno;
Packit 8f70b4
        return NULL;
Packit 8f70b4
      }
Packit 8f70b4
  }
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
  return dirp;
Packit 8f70b4
}