fs/dirent/fs_readdir.c
struct dirent *readdir(DIR *dirp)
{
  int ret;
  int old_err = get_errno();
  int dirent_len = 0;
  struct dirent *de = NULL;
  struct fs_dirent_s *idir = (struct fs_dirent_s *)dirp;

  if (idir->cur_pos != 0 && idir->cur_pos < MAX_DIRENT_NUM && idir->cur_pos < idir->end_pos)
    {
      de = &(idir->fd_dir[idir->cur_pos]);

      if (idir->cur_pos == MAX_DIRENT_NUM)
        {
          idir->cur_pos = 0;
        }
      idir->cur_pos++;

      return de;
    }
  else
    {
      de = __readdir(dirp, &dirent_len);
      idir->end_pos = dirent_len / sizeof(struct dirent);
      idir->cur_pos = 1;

      if (de == NULL)
        {
          idir->cur_pos = 0;
          ret = get_errno();
          /* Special case: ret = -ENOENT is end of file */

          if (ret == ENOENT)
            {
              set_errno(old_err);
            }
        }
    }
  return de;
}
/****************************************************************************
 * Name: readdir
 *
 * Description:
 *   The readdir() function returns a pointer to a dirent structure
 *   representing the next directory entry in the directory stream pointed
 *   to by dir.  It returns NULL on reaching the end-of-file or if an error
 *   occurred.
 *
 * Inputs:
 *   dirp -- An instance of type DIR created by a previous call to opendir();
 *
 * Return:
 *   The readdir() function returns a pointer to a dirent structure, or NULL
 *   if an error occurs or end-of-file is reached.  On error, errno is set
 *   appropriately.
 *
 *   EBADF   - Invalid directory stream descriptor dir
 *
 ****************************************************************************/
readdir
Description:
The readdir() function returns a pointer to a dirent structure
representing the next directory entry in the directory stream pointed
to by dir.  It returns NULL on reaching the end-of-file or if an error
occurred.

- An instance of type DIR created by a previous call to opendir().

The readdir() function returns a pointer to a dirent structure, or NULL
if an error occurs or end-of-file is reached.  On error, errno is set
appropriately.

EBADF   - Invalid directory stream descriptor dir
readdir Description : The readdir () function returns a pointer to a dirent structure representing the next directory entry in the directory stream pointed to by dir . It returns NULL on reaching the end - of - file or if an error occurred . - An instance of type DIR created by a previous call to opendir () . The readdir () function returns a pointer to a dirent structure or NULL if an error occurs or end - of - file is reached . On error errno is set appropriately . EBADF - Invalid directory stream descriptor dir
struct dirent *readdir(DIR *dirp)
{
  int ret;
  int old_err = get_errno();
  int dirent_len = 0;
  struct dirent *de = NULL;
  struct fs_dirent_s *idir = (struct fs_dirent_s *)dirp;
  if (idir->cur_pos != 0 && idir->cur_pos < MAX_DIRENT_NUM && idir->cur_pos < idir->end_pos)
    {
      de = &(idir->fd_dir[idir->cur_pos]);
      if (idir->cur_pos == MAX_DIRENT_NUM)
        {
          idir->cur_pos = 0;
        }
      idir->cur_pos++;
      return de;
    }
  else
    {
      de = __readdir(dirp, &dirent_len);
      idir->end_pos = dirent_len / sizeof(struct dirent);
      idir->cur_pos = 1;
      if (de == NULL)
        {
          idir->cur_pos = 0;
          ret = get_errno();
          if (ret == ENOENT)
            {
              set_errno(old_err);
            }
        }
    }
  return de;
}
struct dirent * readdir ( DIR * dirp ) { int ret ; int old_err = get_errno ( ) ; int dirent_len = 0 ; struct dirent * de = NULL ; struct fs_dirent_s * idir = ( struct fs_dirent_s * ) dirp ; if ( idir -> cur_pos != 0 && idir -> cur_pos < MAX_DIRENT_NUM && idir -> cur_pos < idir -> end_pos ) { de = & ( idir -> fd_dir [ idir -> cur_pos ] ) ; if ( idir -> cur_pos == MAX_DIRENT_NUM ) { idir -> cur_pos = 0 ; } idir -> cur_pos ++ ; return de ; } else { de = __readdir ( dirp , & dirent_len ) ; idir -> end_pos = dirent_len / sizeof ( struct dirent ) ; idir -> cur_pos = 1 ; if ( de == NULL ) { idir -> cur_pos = 0 ; ret = get_errno ( ) ; if ( ret == ENOENT ) { set_errno ( old_err ) ; } } } return de ; }
Name: readdir
 Description:
   The readdir() function returns a pointer to a dirent structure
   representing the next directory entry in the directory stream pointed
   to by dir.
Name : readdir Description : The readdir () function returns a pointer to a dirent structure representing the next directory entry in the directory stream pointed to by dir .
/* Special case: ret = -ENOENT is end of file */