ack/lang/cem/libcc.ansi/misc/readdir.c

45 lines
972 B
C
Raw Normal View History

1989-12-18 14:40:54 +00:00
/*
readdir -- read next entry from a directory stream
last edit: 25-Apr-1987 D A Gwyn
*/
2018-06-21 20:33:47 +00:00
#include <errno.h>
#include <sys/errno.h>
#include <sys/types.h>
#include <dirent.h>
1989-12-18 14:40:54 +00:00
/* SVR3 system call, or emulation for getdents() */
2018-06-21 20:33:47 +00:00
extern int getdents(int fildes, char* buf, unsigned nbyte);
1989-12-18 14:40:54 +00:00
#ifndef NULL
2018-06-21 20:33:47 +00:00
#define NULL 0
1989-12-18 14:40:54 +00:00
#endif
2018-06-21 20:33:47 +00:00
struct dirent*
readdir(register DIR* dirp)
1989-12-18 14:40:54 +00:00
{
2018-06-21 20:33:47 +00:00
register struct dirent* dp; /* -> directory data */
1989-12-18 14:40:54 +00:00
2018-06-21 20:33:47 +00:00
if (dirp == NULL || dirp->dd_buf == NULL)
{
1989-12-18 14:40:54 +00:00
errno = EFAULT;
2018-06-21 20:33:47 +00:00
return NULL; /* invalid pointer */
}
1989-12-18 14:40:54 +00:00
2018-06-21 20:33:47 +00:00
do
{
if (dirp->dd_loc >= dirp->dd_size) /* empty or obsolete */
1989-12-18 14:40:54 +00:00
dirp->dd_loc = dirp->dd_size = 0;
2018-06-21 20:33:47 +00:00
if (dirp->dd_size == 0 /* need to refill buffer */
&& (dirp->dd_size = getdents(dirp->dd_fd, dirp->dd_buf, (unsigned)DIRBUF)) <= 0)
return NULL; /* EOF or error */
1989-12-18 14:40:54 +00:00
2018-06-21 20:33:47 +00:00
dp = (struct dirent*)&dirp->dd_buf[dirp->dd_loc];
1989-12-18 14:40:54 +00:00
dirp->dd_loc += dp->d_reclen;
2018-06-21 20:33:47 +00:00
} while (dp->d_ino == 0L); /* don't rely on getdents() */
1989-12-18 14:40:54 +00:00
return dp;
}