2018-08-26 16:34:53 +00:00
|
|
|
/* -*-comment-start: "//";comment-end:""-*-
|
|
|
|
* GNU Mes --- Maxwell Equations of Software
|
|
|
|
* Copyright (C) 1991,92,93,94,95,96,97,99,2000 Free Software Foundation, Inc.
|
|
|
|
* Copyright © 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
|
|
|
*
|
|
|
|
* This file is part of GNU Mes.
|
|
|
|
*
|
|
|
|
* GNU Mes is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* GNU Mes is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Taken from GNU C Library 2.2.5
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <dirstream.h>
|
|
|
|
|
|
|
|
/* Read a directory entry from DIRP. */
|
|
|
|
struct dirent *
|
2019-05-18 11:27:42 +00:00
|
|
|
readdir (DIR * dirp)
|
2018-08-26 16:34:53 +00:00
|
|
|
{
|
|
|
|
struct dirent *dp;
|
|
|
|
int saved_errno = errno;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
size_t reclen;
|
|
|
|
|
|
|
|
if (dirp->offset >= dirp->size)
|
2019-05-18 11:27:42 +00:00
|
|
|
{
|
|
|
|
/* We've emptied out our buffer. Refill it. */
|
2018-08-26 16:34:53 +00:00
|
|
|
|
2019-05-18 11:27:42 +00:00
|
|
|
size_t maxread;
|
|
|
|
ssize_t bytes;
|
2018-08-26 16:34:53 +00:00
|
|
|
|
2019-05-18 11:27:42 +00:00
|
|
|
maxread = dirp->allocation;
|
2018-08-26 16:34:53 +00:00
|
|
|
|
|
|
|
#if 0
|
2019-05-18 11:27:42 +00:00
|
|
|
off_t base;
|
|
|
|
bytes = __getdirentries (dirp->fd, dirp->data, maxread, &base);
|
2018-08-26 16:34:53 +00:00
|
|
|
#else
|
|
|
|
bytes = getdents (dirp->fd, dirp->data, maxread);
|
|
|
|
#endif
|
2019-05-18 11:27:42 +00:00
|
|
|
if (bytes <= 0)
|
|
|
|
{
|
|
|
|
/* Don't modifiy errno when reaching EOF. */
|
|
|
|
if (bytes == 0)
|
2018-08-26 16:34:53 +00:00
|
|
|
errno = saved_errno;
|
2019-05-18 11:27:42 +00:00
|
|
|
dp = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dirp->size = (size_t) bytes;
|
2018-08-26 16:34:53 +00:00
|
|
|
|
2019-05-18 11:27:42 +00:00
|
|
|
/* Reset the offset into the buffer. */
|
|
|
|
dirp->offset = 0;
|
|
|
|
}
|
2018-08-26 16:34:53 +00:00
|
|
|
|
|
|
|
dp = (struct dirent *) &dirp->data[dirp->offset];
|
|
|
|
|
|
|
|
reclen = dp->d_reclen;
|
|
|
|
dirp->offset += reclen;
|
|
|
|
dirp->filepos = dp->d_off;
|
|
|
|
|
|
|
|
/* Skip deleted files. */
|
2019-05-18 11:27:42 +00:00
|
|
|
}
|
|
|
|
while (dp->d_ino == 0);
|
2018-08-26 16:34:53 +00:00
|
|
|
|
|
|
|
return dp;
|
|
|
|
}
|