]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/PosixLib/Gen/readdir.c
StdLib: Add directory access functions to PosixLib.
[mirror_edk2.git] / StdLib / PosixLib / Gen / readdir.c
CommitLineData
d711a486 1/** @file\r
2 Get next entry in a directory.\r
3\r
4 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13 Copyright (c) 1983, 1993\r
14 The Regents of the University of California. All rights reserved.\r
15\r
16 Redistribution and use in source and binary forms, with or without\r
17 modification, are permitted provided that the following conditions\r
18 are met:\r
19 1. Redistributions of source code must retain the above copyright\r
20 notice, this list of conditions and the following disclaimer.\r
21 2. Redistributions in binary form must reproduce the above copyright\r
22 notice, this list of conditions and the following disclaimer in the\r
23 documentation and/or other materials provided with the distribution.\r
24 3. Neither the name of the University nor the names of its contributors\r
25 may be used to endorse or promote products derived from this software\r
26 without specific prior written permission.\r
27\r
28 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r
29 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
30 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
31 ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r
32 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
33 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
34 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
35 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
36 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
37 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
38 SUCH DAMAGE.\r
39\r
40 NetBSD: readdir.c,v 1.24 2008/05/04 18:53:26 tonnerre Exp\r
41 readdir.c 8.3 (Berkeley) 9/29/94\r
42 */\r
43#include <sys/cdefs.h>\r
44\r
45#include <namespace.h>\r
46#include <reentrant.h>\r
47#include <extern.h>\r
48#include <sys/param.h>\r
49#include <sys/stdint.h>\r
50\r
51#include <stdio.h>\r
52#include <dirent.h>\r
53#include <errno.h>\r
54#include <string.h>\r
55#include <unistd.h>\r
56\r
57#include <sys/EfiSysCall.h>\r
58\r
59/*\r
60 * get next entry in a directory.\r
61 */\r
62struct dirent *\r
63_readdir_unlocked(DIR *dirp, int skipdeleted)\r
64{\r
65 struct dirent *dp;\r
66\r
67\r
68 for (;;) {\r
69 if (dirp->dd_loc >= dirp->dd_size) {\r
70 if (dirp->dd_flags & __DTF_READALL)\r
71 return (NULL);\r
72 dirp->dd_loc = 0;\r
73 }\r
74 if (dirp->dd_loc == 0 && !(dirp->dd_flags & __DTF_READALL)) {\r
75 dirp->dd_size = (long)read(dirp->dd_fd, dirp->dd_buf, (size_t)dirp->dd_len);\r
76 if (dirp->dd_size <= 0)\r
77 return (NULL);\r
78 }\r
79 dp = (struct dirent *) (void *)(dirp->dd_buf + (size_t)dirp->dd_loc);\r
80 if ((intptr_t)dp & _DIRENT_ALIGN(dp))/* bogus pointer check */\r
81 return (NULL);\r
82 dirp->dd_loc += (long)dp->Size;\r
83 if ((dp->Attribute & DT_HIDDEN) && (dirp->dd_flags & DTF_HIDEW))\r
84 continue;\r
85 return (dp);\r
86 }\r
87}\r
88\r
89struct dirent *\r
90readdir(DIR *dirp)\r
91{\r
92 struct dirent *dp;\r
93\r
94#ifdef _REENTRANT\r
95 if (__isthreaded) {\r
96 mutex_lock((mutex_t *)dirp->dd_lock);\r
97 dp = _readdir_unlocked(dirp, 1);\r
98 mutex_unlock((mutex_t *)dirp->dd_lock);\r
99 }\r
100 else\r
101#endif\r
102 dp = _readdir_unlocked(dirp, 1);\r
103 return (dp);\r
104}\r
105\r
106int\r
107readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)\r
108{\r
109 struct dirent *dp;\r
110 int saved_errno;\r
111\r
112 saved_errno = errno;\r
113 errno = 0;\r
114#ifdef _REENTRANT\r
115 if (__isthreaded) {\r
116 mutex_lock((mutex_t *)dirp->dd_lock);\r
117 if ((dp = _readdir_unlocked(dirp, 1)) != NULL)\r
118 memcpy(entry, dp, (size_t)_DIRENT_SIZE(dp));\r
119 mutex_unlock((mutex_t *)dirp->dd_lock);\r
120 }\r
121 else\r
122#endif\r
123 if ((dp = _readdir_unlocked(dirp, 1)) != NULL)\r
124 memcpy(entry, dp, (size_t)_DIRENT_SIZE(dp));\r
125\r
126 if (errno != 0) {\r
127 if (dp == NULL)\r
128 return (errno);\r
129 } else\r
130 errno = saved_errno;\r
131\r
132 if (dp != NULL)\r
133 *result = entry;\r
134 else\r
135 *result = NULL;\r
136\r
137 return (0);\r
138}\r