Blame lib/readdir.c

Packit 8f70b4
/* Read the next entry of a directory.
Packit 8f70b4
   Copyright (C) 2011-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
#include "dirent-private.h"
Packit 8f70b4
Packit 8f70b4
struct dirent *
Packit 8f70b4
readdir (DIR *dirp)
Packit 8f70b4
{
Packit 8f70b4
  char type;
Packit 8f70b4
  struct dirent *result;
Packit 8f70b4
Packit 8f70b4
  /* There is no need to add code to produce entries for "." and "..".
Packit 8f70b4
     According to the POSIX:2008 section "4.12 Pathname Resolution"
Packit 8f70b4
     <http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html>
Packit 8f70b4
     "." and ".." are syntactic entities.
Packit 8f70b4
     POSIX also says:
Packit 8f70b4
       "If entries for dot or dot-dot exist, one entry shall be returned
Packit 8f70b4
        for dot and one entry shall be returned for dot-dot; otherwise,
Packit 8f70b4
        they shall not be returned."  */
Packit 8f70b4
Packit 8f70b4
  switch (dirp->status)
Packit 8f70b4
    {
Packit 8f70b4
    case -2:
Packit 8f70b4
      /* End of directory already reached.  */
Packit 8f70b4
      return NULL;
Packit 8f70b4
    case -1:
Packit 8f70b4
      break;
Packit 8f70b4
    case 0:
Packit 8f70b4
      if (!FindNextFile (dirp->current, &dirp->entry))
Packit 8f70b4
        {
Packit 8f70b4
          switch (GetLastError ())
Packit 8f70b4
            {
Packit 8f70b4
            case ERROR_NO_MORE_FILES:
Packit 8f70b4
              dirp->status = -2;
Packit 8f70b4
              return NULL;
Packit 8f70b4
            default:
Packit 8f70b4
              errno = EIO;
Packit 8f70b4
              return NULL;
Packit 8f70b4
            }
Packit 8f70b4
        }
Packit 8f70b4
      break;
Packit 8f70b4
    default:
Packit 8f70b4
      errno = dirp->status;
Packit 8f70b4
      return NULL;
Packit 8f70b4
    }
Packit 8f70b4
Packit 8f70b4
  dirp->status = 0;
Packit 8f70b4
Packit 8f70b4
  if (dirp->entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
Packit 8f70b4
    type = DT_DIR;
Packit 8f70b4
  else if (dirp->entry.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
Packit 8f70b4
    type = DT_LNK;
Packit 8f70b4
  else if ((dirp->entry.dwFileAttributes
Packit 8f70b4
            & ~(FILE_ATTRIBUTE_READONLY
Packit 8f70b4
                | FILE_ATTRIBUTE_HIDDEN
Packit 8f70b4
                | FILE_ATTRIBUTE_SYSTEM
Packit 8f70b4
                | FILE_ATTRIBUTE_ARCHIVE
Packit 8f70b4
                | FILE_ATTRIBUTE_NORMAL
Packit 8f70b4
                | FILE_ATTRIBUTE_TEMPORARY
Packit 8f70b4
                | FILE_ATTRIBUTE_SPARSE_FILE
Packit 8f70b4
                | FILE_ATTRIBUTE_COMPRESSED
Packit 8f70b4
                | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
Packit 8f70b4
                | FILE_ATTRIBUTE_ENCRYPTED)) == 0)
Packit 8f70b4
    /* Devices like COM1, LPT1, NUL would also have the attributes 0x20 but
Packit 8f70b4
       they cannot occur here.  */
Packit 8f70b4
    type = DT_REG;
Packit 8f70b4
  else
Packit 8f70b4
    type = DT_UNKNOWN;
Packit 8f70b4
Packit 8f70b4
  /* Reuse the memory of dirp->entry for the result.  */
Packit 8f70b4
  result =
Packit 8f70b4
    (struct dirent *)
Packit 8f70b4
    ((char *) dirp->entry.cFileName - offsetof (struct dirent, d_name[0]));
Packit 8f70b4
  result->d_type = type;
Packit 8f70b4
Packit 8f70b4
  return result;
Packit 8f70b4
}