(root)/
gettext-0.22.4/
gettext-tools/
gnulib-lib/
opendir.c
       1  /* Start reading the entries of a directory.
       2     Copyright (C) 2006-2023 Free Software Foundation, Inc.
       3  
       4     This file is free software: you can redistribute it and/or modify
       5     it under the terms of the GNU Lesser General Public License as
       6     published by the Free Software Foundation; either version 2.1 of the
       7     License, or (at your option) any later version.
       8  
       9     This file is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12     GNU Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public License
      15     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      16  
      17  #include <config.h>
      18  
      19  /* Specification.  */
      20  #include <dirent.h>
      21  
      22  #include <errno.h>
      23  #include <stddef.h>
      24  
      25  #if HAVE_OPENDIR
      26  
      27  /* Override opendir(), to keep track of the open file descriptors.
      28     Needed because there is a function dirfd().  */
      29  
      30  #else
      31  
      32  # include "filename.h"
      33  
      34  #endif
      35  
      36  #include <stdlib.h>
      37  #include <string.h>
      38  
      39  #if GNULIB_defined_DIR
      40  # include "dirent-private.h"
      41  #endif
      42  
      43  #if REPLACE_FCHDIR
      44  # include <unistd.h>
      45  #endif
      46  
      47  #if defined _WIN32 && ! defined __CYGWIN__
      48  /* Don't assume that UNICODE is not defined.  */
      49  # undef WIN32_FIND_DATA
      50  # define WIN32_FIND_DATA WIN32_FIND_DATAA
      51  # undef GetFullPathName
      52  # define GetFullPathName GetFullPathNameA
      53  # undef FindFirstFile
      54  # define FindFirstFile FindFirstFileA
      55  #endif
      56  
      57  DIR *
      58  opendir (const char *dir_name)
      59  #undef opendir
      60  {
      61  #if HAVE_DIRENT_H                       /* equivalent to HAVE_OPENDIR */
      62    DIR *dirp;
      63  
      64  # if GNULIB_defined_DIR
      65  #  undef DIR
      66  
      67    dirp = (struct gl_directory *) malloc (sizeof (struct gl_directory));
      68    if (dirp == NULL)
      69      {
      70        errno = ENOMEM;
      71        return NULL;
      72      }
      73  
      74    DIR *real_dirp = opendir (dir_name);
      75    if (real_dirp == NULL)
      76      {
      77        int saved_errno = errno;
      78        free (dirp);
      79        errno = saved_errno;
      80        return NULL;
      81      }
      82  
      83    dirp->fd_to_close = -1;
      84    dirp->real_dirp = real_dirp;
      85  # else
      86    dirp = opendir (dir_name);
      87    if (dirp == NULL)
      88      return NULL;
      89  # endif
      90  
      91  #else
      92  
      93    char dir_name_mask[MAX_PATH + 1 + 1 + 1];
      94    int status;
      95    HANDLE current;
      96    WIN32_FIND_DATA entry;
      97    struct gl_directory *dirp;
      98  
      99    if (dir_name[0] == '\0')
     100      {
     101        errno = ENOENT;
     102        return NULL;
     103      }
     104  
     105    /* Make the dir_name absolute, so that we continue reading the same
     106       directory if the current directory changed between this opendir()
     107       call and a subsequent rewinddir() call.  */
     108    if (!GetFullPathName (dir_name, MAX_PATH, dir_name_mask, NULL))
     109      {
     110        errno = EINVAL;
     111        return NULL;
     112      }
     113  
     114    /* Append the mask.
     115       "*" and "*.*" appear to be equivalent.  */
     116    {
     117      char *p;
     118  
     119      p = dir_name_mask + strlen (dir_name_mask);
     120      if (p > dir_name_mask && !ISSLASH (p[-1]))
     121        *p++ = '\\';
     122      *p++ = '*';
     123      *p = '\0';
     124    }
     125  
     126    /* Start searching the directory.  */
     127    status = -1;
     128    current = FindFirstFile (dir_name_mask, &entry);
     129    if (current == INVALID_HANDLE_VALUE)
     130      {
     131        switch (GetLastError ())
     132          {
     133          case ERROR_FILE_NOT_FOUND:
     134            status = -2;
     135            break;
     136          case ERROR_PATH_NOT_FOUND:
     137            errno = ENOENT;
     138            return NULL;
     139          case ERROR_DIRECTORY:
     140            errno = ENOTDIR;
     141            return NULL;
     142          case ERROR_ACCESS_DENIED:
     143            errno = EACCES;
     144            return NULL;
     145          default:
     146            errno = EIO;
     147            return NULL;
     148          }
     149      }
     150  
     151    /* Allocate the result.  */
     152    dirp =
     153      (struct gl_directory *)
     154      malloc (offsetof (struct gl_directory, dir_name_mask[0])
     155              + strlen (dir_name_mask) + 1);
     156    if (dirp == NULL)
     157      {
     158        if (current != INVALID_HANDLE_VALUE)
     159          FindClose (current);
     160        errno = ENOMEM;
     161        return NULL;
     162      }
     163    dirp->fd_to_close = -1;
     164    dirp->status = status;
     165    dirp->current = current;
     166    if (status == -1)
     167      memcpy (&dirp->entry, &entry, sizeof (WIN32_FIND_DATA));
     168    strcpy (dirp->dir_name_mask, dir_name_mask);
     169  
     170  #endif
     171  
     172  #if REPLACE_FCHDIR
     173    {
     174      int fd = dirfd (dirp);
     175      if (0 <= fd && _gl_register_fd (fd, dir_name) != fd)
     176        {
     177          int saved_errno = errno;
     178          closedir (dirp);
     179          errno = saved_errno;
     180          return NULL;
     181        }
     182    }
     183  #endif
     184  
     185    return dirp;
     186  }