Import upstream version 1.28
[debian/tar] / gnu / readdir.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Read the next entry of a directory.
4    Copyright (C) 2011-2014 Free Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include <dirent.h>
23
24 #include <errno.h>
25 #include <stddef.h>
26
27 #include "dirent-private.h"
28
29 struct dirent *
30 readdir (DIR *dirp)
31 {
32   char type;
33   struct dirent *result;
34
35   /* There is no need to add code to produce entries for "." and "..".
36      According to the POSIX:2008 section "4.12 Pathname Resolution"
37      <http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html>
38      "." and ".." are syntactic entities.
39      POSIX also says:
40        "If entries for dot or dot-dot exist, one entry shall be returned
41         for dot and one entry shall be returned for dot-dot; otherwise,
42         they shall not be returned."  */
43
44   switch (dirp->status)
45     {
46     case -2:
47       /* End of directory already reached.  */
48       return NULL;
49     case -1:
50       break;
51     case 0:
52       if (!FindNextFile (dirp->current, &dirp->entry))
53         {
54           switch (GetLastError ())
55             {
56             case ERROR_NO_MORE_FILES:
57               dirp->status = -2;
58               return NULL;
59             default:
60               errno = EIO;
61               return NULL;
62             }
63         }
64       break;
65     default:
66       errno = dirp->status;
67       return NULL;
68     }
69
70   dirp->status = 0;
71
72   if (dirp->entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
73     type = DT_DIR;
74   else if (dirp->entry.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
75     type = DT_LNK;
76   else if ((dirp->entry.dwFileAttributes
77             & ~(FILE_ATTRIBUTE_READONLY
78                 | FILE_ATTRIBUTE_HIDDEN
79                 | FILE_ATTRIBUTE_SYSTEM
80                 | FILE_ATTRIBUTE_ARCHIVE
81                 | FILE_ATTRIBUTE_NORMAL
82                 | FILE_ATTRIBUTE_TEMPORARY
83                 | FILE_ATTRIBUTE_SPARSE_FILE
84                 | FILE_ATTRIBUTE_COMPRESSED
85                 | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
86                 | FILE_ATTRIBUTE_ENCRYPTED)) == 0)
87     /* Devices like COM1, LPT1, NUL would also have the attributes 0x20 but
88        they cannot occur here.  */
89     type = DT_REG;
90   else
91     type = DT_UNKNOWN;
92
93   /* Reuse the memory of dirp->entry for the result.  */
94   result =
95     (struct dirent *)
96     ((char *) dirp->entry.cFileName - offsetof (struct dirent, d_name[0]));
97   result->d_type = type;
98
99   return result;
100 }