home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / LIBSRC.ZOO / libsrc / local / opendir.c < prev    next >
Text File  |  1992-04-06  |  1KB  |  63 lines

  1. #define INCL_DOSFILEMGR
  2. #include <os2.h>
  3.  
  4. #define __DIRENT_PRIVATE__
  5. #include <stdlib.h>
  6. #ifdef DEBUG
  7. #include <stdio.h>
  8. #endif
  9. #include <dirent.h>
  10.  
  11. ULONG Dos32FindFirst() asm ("Dos32FindFirst");
  12.  
  13. DIR *opendir(const char *dirname)
  14. {
  15.      DIR *dir;
  16.      ULONG ret;
  17.      FILEFINDBUF3 buf;
  18.      unsigned long int cnt;
  19.      int namelen;
  20.      
  21. #ifdef DEBUG
  22.      fprintf(stderr, "opendir: starting\n");
  23. #endif
  24.      dir = (DIR *) malloc(sizeof(DIR));
  25.      if (dir == NULL)
  26.       return NULL;
  27.  
  28.      /* Remember to allocate enough bytes to hold the maximum number */
  29.      /* of characters that may be appended to the file name, plus a NULL */
  30.      namelen = strlen(dirname);
  31.  
  32.      dir->searchname = (char *) malloc(namelen + 3);
  33.      if (dir->searchname == NULL) {
  34.       free(dir);
  35.       return NULL;
  36.      }
  37.  
  38.      strcpy(dir->searchname, dirname);
  39.      if (dir->searchname[namelen-1] != '\\') {
  40.       strcat(dir->searchname, "\\");
  41.       namelen += 1;
  42.      }
  43.  
  44.      strcat(dir->searchname, "*");
  45.      namelen += 1;
  46.      
  47. #ifdef DEBUG
  48.      fprintf(stderr, "opendir: calling findfirst, searchname = %s\n",
  49.          dir->searchname);
  50. #endif
  51.      dir->reset = 1;
  52.      dir->dirhandle = HDIR_CREATE;
  53.      cnt = 1;
  54.      ret = Dos32FindFirst(dir->searchname, &dir->dirhandle,
  55.             DIRENT_ALLFILES, &buf, sizeof(buf), &cnt, 1);
  56.      if (ret != 0) {
  57.       free(dir->searchname);
  58.       free(dir);
  59.       return NULL;
  60.      } else
  61.       return dir;
  62. }
  63.