home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / posix / dirent / readdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-11  |  1.4 KB  |  59 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <string.h>
  4. #include <dirent.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include <ctype.h>
  8. #include "dirstruc.h"
  9.  
  10. struct dirent *
  11. readdir(DIR *dir)
  12. {
  13.   int done;
  14.   int oerrno = errno;
  15.  
  16.   if (dir->need_fake_dot_dotdot)
  17.   {
  18.     /* Fake out . and .. on /; see opendir for comments */
  19.     dir->need_fake_dot_dotdot --;
  20.     if (dir->need_fake_dot_dotdot)
  21.       strcpy(dir->de.d_name, ".");
  22.     else
  23.       strcpy(dir->de.d_name, "..");
  24.     dir->de.d_namlen = strlen(dir->de.d_name);
  25.     return &(dir->de);
  26.   }
  27.  
  28.   if (dir->num_read)
  29.     done = findnext(&dir->ff);
  30.   else
  31.   {
  32.     int ff_flags = FA_ARCH|FA_RDONLY|FA_DIREC|FA_SYSTEM;
  33.     if (dir->flags & __OPENDIR_FIND_HIDDEN)
  34.       ff_flags |= FA_HIDDEN;
  35.     if (dir->flags & __OPENDIR_FIND_LABEL)
  36.       ff_flags |= FA_LABEL;
  37.     done = findfirst(dir->name, &dir->ff, ff_flags);
  38.   }
  39.   if (done)
  40.   {
  41.     if (errno == ENMFILE)
  42.       errno = oerrno;
  43.     return 0;
  44.   }
  45.   dir->num_read ++;
  46.   if (!(dir->flags & __OPENDIR_PRESERVE_CASE))
  47.   {
  48.     char *cp, fsh[13];
  49.  
  50.     if (!strcmp(_lfn_gen_short_fname(dir->ff.ff_name, fsh), dir->ff.ff_name))
  51.       for (cp=dir->ff.ff_name; *cp; cp++)
  52.     if (*cp >= 'A' && *cp <= 'Z')
  53.       *cp += 'a' - 'A';
  54.   }
  55.   strcpy(dir->de.d_name, dir->ff.ff_name);
  56.   dir->de.d_namlen = strlen(dir->de.d_name);
  57.   return &dir->de;
  58. }
  59.