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

  1. /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
  2. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  3. #include <libc/stubs.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <dirent.h>
  8. #include <errno.h>
  9. #include <limits.h>
  10. #include <unistd.h>
  11. #include <sys/stat.h>
  12. #include <fcntl.h>
  13. #include "dirstruc.h"
  14.  
  15. DIR *
  16. opendir(const char *name)
  17. {
  18.   int length;
  19.   DIR *dir = (DIR *)malloc(sizeof(DIR));
  20.   if (dir == 0)
  21.     return 0;
  22.   dir->num_read = 0;
  23.   dir->name = (char *)malloc(PATH_MAX);
  24.   if (dir->name == 0)
  25.   {
  26.     free(dir);
  27.     return 0;
  28.   }
  29.  
  30.   dir->flags = __opendir_flags;
  31.   if (!(__opendir_flags & __OPENDIR_PRESERVE_CASE) && _preserve_fncase())
  32.     dir->flags |= __OPENDIR_PRESERVE_CASE;
  33.  
  34.   /* Make absolute path */
  35.   _fixpath(name, dir->name);
  36.  
  37.   /* If we're doing opendir of the root directory, we need to
  38.      fake out the . and .. entries, as some unix programs (like
  39.      mkisofs) expect them and fail if they don't exist */
  40.   dir->need_fake_dot_dotdot = 0;
  41.   if (dir->name[1] == ':' && dir->name[2] == '/' && dir->name[3] == 0)
  42.   {
  43.     /* see if findfirst finds "." anyway */
  44.     int done = findfirst(dir->name, &dir->ff, FA_ARCH|FA_RDONLY|FA_DIREC|FA_SYSTEM);
  45.     if (done || strcmp(dir->ff.ff_name, "."))
  46.       dir->need_fake_dot_dotdot = 2;
  47.   }
  48.  
  49.   /* Ensure that directory to be accessed exists */
  50.   if (access(dir->name, D_OK))
  51.   {
  52.     free(dir->name);
  53.     free(dir);
  54.     return 0;
  55.   }
  56.  
  57.   /* Strip trailing slashes, so we can append "/ *.*" */
  58.   length = strlen(dir->name);
  59.   while (1)
  60.   {
  61.     if (length == 0) break;
  62.     length--;
  63.     if (dir->name[length] == '/' ||
  64.     dir->name[length] == '\\')
  65.       dir->name[length] = '\0';
  66.     else
  67.     {
  68.       length++;
  69.       break;
  70.     }
  71.   }
  72.  
  73.   dir->name[length++] = '/';
  74.   dir->name[length++] = '*';
  75.   dir->name[length++] = '.';
  76.   dir->name[length++] = '*';
  77.   dir->name[length++] = 0;
  78.   return dir;
  79. }
  80.