]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/PosixLib/Gen/readdir.c
StdLib: Add isDirSep character classification macro and function. Implement several...
[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
d711a486 57/*\r
58 * get next entry in a directory.\r
59 */\r
60struct dirent *\r
61_readdir_unlocked(DIR *dirp, int skipdeleted)\r
62{\r
63 struct dirent *dp;\r
64\r
65\r
66 for (;;) {\r
67 if (dirp->dd_loc >= dirp->dd_size) {\r
68 if (dirp->dd_flags & __DTF_READALL)\r
69 return (NULL);\r
70 dirp->dd_loc = 0;\r
71 }\r
72 if (dirp->dd_loc == 0 && !(dirp->dd_flags & __DTF_READALL)) {\r
73 dirp->dd_size = (long)read(dirp->dd_fd, dirp->dd_buf, (size_t)dirp->dd_len);\r
74 if (dirp->dd_size <= 0)\r
75 return (NULL);\r
76 }\r
77 dp = (struct dirent *) (void *)(dirp->dd_buf + (size_t)dirp->dd_loc);\r
78 if ((intptr_t)dp & _DIRENT_ALIGN(dp))/* bogus pointer check */\r
79 return (NULL);\r
80 dirp->dd_loc += (long)dp->Size;\r
81 if ((dp->Attribute & DT_HIDDEN) && (dirp->dd_flags & DTF_HIDEW))\r
82 continue;\r
83 return (dp);\r
84 }\r
85}\r
86\r
87struct dirent *\r
88readdir(DIR *dirp)\r
89{\r
90 struct dirent *dp;\r
91\r
92#ifdef _REENTRANT\r
93 if (__isthreaded) {\r
94 mutex_lock((mutex_t *)dirp->dd_lock);\r
95 dp = _readdir_unlocked(dirp, 1);\r
96 mutex_unlock((mutex_t *)dirp->dd_lock);\r
97 }\r
98 else\r
99#endif\r
100 dp = _readdir_unlocked(dirp, 1);\r
101 return (dp);\r
102}\r
103\r
104int\r
105readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)\r
106{\r
107 struct dirent *dp;\r
108 int saved_errno;\r
109\r
110 saved_errno = errno;\r
111 errno = 0;\r
112#ifdef _REENTRANT\r
113 if (__isthreaded) {\r
114 mutex_lock((mutex_t *)dirp->dd_lock);\r
115 if ((dp = _readdir_unlocked(dirp, 1)) != NULL)\r
116 memcpy(entry, dp, (size_t)_DIRENT_SIZE(dp));\r
117 mutex_unlock((mutex_t *)dirp->dd_lock);\r
118 }\r
119 else\r
120#endif\r
121 if ((dp = _readdir_unlocked(dirp, 1)) != NULL)\r
122 memcpy(entry, dp, (size_t)_DIRENT_SIZE(dp));\r
123\r
124 if (errno != 0) {\r
125 if (dp == NULL)\r
126 return (errno);\r
127 } else\r
128 errno = saved_errno;\r
129\r
130 if (dp != NULL)\r
131 *result = entry;\r
132 else\r
133 *result = NULL;\r
134\r
135 return (0);\r
136}\r