2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #if defined(__GLIBC__) && !defined(REDSTORM)
29 #include "sysio-symbols.h"
32 #define _READDIR SYSIO_INTERFACE_NAME(readdir)
33 #define _SCANDIR SYSIO_INTERFACE_NAME(scandir)
34 #define _GETDIRENTRIES SYSIO_INTERFACE_NAME(getdirentries)
35 #define _DIRENT_T struct dirent
52 SYSIO_INTERFACE_DISPLAY_BLOCK;
54 SYSIO_INTERFACE_ENTER;
56 /* need to read new data? */
58 if (dir->cur >= dir->effective) {
60 dbase = (_OFF_T )dir->base;
61 if (sizeof(dbase) != sizeof(dir->base) &&
64 SYSIO_INTERFACE_RETURN(NULL, -EOVERFLOW);
66 rc = _GETDIRENTRIES(dir->fd,
73 (long *) __restrict dbase);
75 dir->base = (_SYSIO_OFF_T )dbase;
77 /* error or end-of-file */
82 SYSIO_INTERFACE_RETURN(NULL, rc);
86 dp = (_DIRENT_T *)(dir->buf + dir->cur);
88 #ifdef _DIRENT_HAVE_D_RECLEN
89 dir->cur += dp->d_reclen;
91 dir->cur += sizeof(_DIRENT_T);
93 #ifdef _DIRENT_HAVE_D_OFF
94 dir->filepos = dp->d_off;
96 dir->filepos = dir->cur;
99 SYSIO_INTERFACE_RETURN(dp, 0);
102 sysio_sym_weak_alias(_READDIR, PREPEND(__,_READDIR))
105 _SCANDIR(const char *dirname,
106 _DIRENT_T ***namelist,
107 int (*filter) (const _DIRENT_T *),
108 #ifdef HAVE_POSIX2008_SCANDIR
109 int(*compar)(const _DIRENT_T **, const _DIRENT_T **)
111 int(*compar)(const void *, const void *)
116 _DIRENT_T *de = NULL,
121 SYSIO_INTERFACE_DISPLAY_BLOCK;
123 SYSIO_INTERFACE_ENTER;
125 if ((dir = SYSIO_INTERFACE_NAME(opendir)(dirname)) == NULL)
126 SYSIO_INTERFACE_RETURN(-1, -errno);
128 while ((de = _READDIR(dir)) != NULL) {
129 if ((filter == NULL) || filter(de)) {
130 if (i == 0 || i >= n) {
132 s = (_DIRENT_T **)realloc(s,
133 (size_t )(n * sizeof(_DIRENT_T *)));
135 SYSIO_INTERFACE_RETURN(-1, -ENOMEM);
137 desize = &de->d_name[_D_ALLOC_NAMLEN(de)] - (char * )de;
138 nextde = (_DIRENT_T *)malloc(desize);
140 SYSIO_INTERFACE_RETURN(-1, -ENOMEM);
142 s[i++] = (_DIRENT_T *)memcpy(nextde, de, desize);
149 (int (*)(const void *, const void *))compar);
153 SYSIO_INTERFACE_NAME(closedir)(dir);
155 SYSIO_INTERFACE_RETURN(i, 0);
158 sysio_sym_weak_alias(_SCANDIR, PREPEND(__,_SCANDIR))