home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / POSIXDIR.LZH / POSIXDIR.C < prev    next >
C/C++ Source or Header  |  1993-03-17  |  3KB  |  172 lines

  1. /*
  2.     POSIX directory stuff for Cset/2
  3.  
  4.     by Joe DeRosa 3/22/93 (1:141/42@fidonet.org)
  5.  
  6.     released in the public domain
  7.  
  8.     if you use this code you accept ALL responsibility for anything that
  9.     happens.
  10. */
  11.  
  12. #define  INCL_DOSFILEMGR
  13. #include <os2.h>
  14. #include <dirent.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17.  
  18. #define DIRVALID 0x42
  19. #define ALLFILES FILE_ARCHIVED | FILE_DIRECTORY | FILE_SYSTEM | FILE_HIDDEN | FILE_READONLY
  20.  
  21. DIR *opendir (char *dirname)
  22. {
  23.    char *name,*cp;
  24.    DIR *dir;
  25.    int length;
  26.    ULONG count;
  27.  
  28.    length = strlen (dirname);
  29.    if ((name = malloc (length + 3)) == NULL)
  30.       return NULL;
  31.  
  32.    strcpy (name, dirname);
  33.  
  34.    if (length-- && name[length] != ':' && name[length] != '\\' && name[length] != '/')
  35.       strcat (name, "\\*");
  36.    else
  37.       strcat (name, "*");
  38.  
  39.  
  40.    if ((dir = malloc (sizeof (DIR))) == NULL) {
  41.       free (name);
  42.       return NULL;
  43.    }
  44.  
  45.    if ((dir->fb = malloc (sizeof (FILEFINDBUF3))) == NULL) {
  46.       free (name);
  47.       free (dir);
  48.       return NULL;
  49.    }
  50.  
  51.    dir->dh = HDIR_CREATE;
  52.    count = 1;
  53.  
  54.    if (DosFindFirst (name,&dir->dh,ALLFILES,dir->fb, sizeof (FILEFINDBUF3), &count, FIL_STANDARD) != 0) {
  55.       free (name);
  56.       free (dir->fb);
  57.       free (dir);
  58.       return NULL;
  59.    }
  60.    dir->d_dirname = name;
  61.    dir->d_entnum = 1;
  62.    dir->d_valid = DIRVALID;
  63.    dir->d_dirent.d_name = dir->fb->achName;
  64.  
  65.    return dir;
  66. }
  67.  
  68.  
  69.  
  70. void rewinddir (DIR * dir)
  71. {
  72.    ULONG count;
  73.  
  74.    if (dir->d_valid != DIRVALID)
  75.       return;
  76.  
  77.    DosFindClose (dir->dh);
  78.  
  79.    dir->dh = HDIR_CREATE;
  80.    count = 1;
  81.  
  82.    DosFindFirst (dir->d_dirname,&dir->dh,ALLFILES,
  83.          dir->fb, sizeof (FILEFINDBUF3), &count, FIL_STANDARD);
  84.  
  85.    dir->d_entnum = 1;
  86. }
  87.  
  88.  
  89.  
  90. struct dirent *readdir (DIR * dir)
  91. {
  92.  
  93.    ULONG count;
  94.  
  95.    if (dir->d_valid != DIRVALID)
  96.       return NULL;
  97.  
  98.    count = 1;
  99.  
  100.    if (dir->d_entnum != 1)
  101.       if (DosFindNext (dir->dh, dir->fb, sizeof (FILEFINDBUF3), &count) != 0)
  102.      return NULL;
  103.  
  104.  
  105.    dir->d_entnum++;
  106.  
  107.    return &dir->d_dirent;
  108. }
  109.  
  110.  
  111.  
  112. int closedir (DIR * dir)
  113. {
  114.    if (dir == NULL || dir->d_valid != DIRVALID)
  115.       return (-1);
  116.  
  117.  
  118.    dir->d_valid = 0;
  119.    DosFindClose (dir->dh);
  120.    free (dir->d_dirname);
  121.    free (dir->fb);
  122.    free (dir);
  123.  
  124.    return 0;
  125. }
  126.  
  127.  
  128.  
  129. long telldir (DIR * dir)
  130. {
  131.    if (dir->d_valid != DIRVALID)
  132.       return -1;
  133.  
  134.    return dir->d_entnum;
  135. }
  136.  
  137.  
  138.  
  139. void seekdir (DIR * dir, long pos)
  140. {
  141.    if (dir->d_valid != DIRVALID)
  142.       return;
  143.    rewinddir (dir);
  144.    while (readdir (dir) && dir->d_entnum != pos);
  145. }
  146.  
  147. #ifdef TEST
  148.  
  149. #include <stdio.h>
  150. main ()
  151. {
  152.    DIR *d;
  153.    struct dirent *de;
  154.  
  155.    d = opendir ("c:/Os2/.");
  156.    if (d) {
  157.       while ((de = readdir (d)) != 0)
  158.      printf ("Entry %ld is %s\n", telldir (d) - 1, de->d_name);
  159.  
  160.       seekdir (d, 15);
  161.       while ((de = readdir (d)) != 0)
  162.      printf ("Entry %ld is %s\n", telldir (d) - 1, de->d_name);
  163.  
  164.       rewinddir (d);
  165.       while ((de = readdir (d)) != 0)
  166.      printf ("Next is %s\n", de->d_name);
  167.  
  168.       closedir (d);
  169.    }
  170. }
  171. #endif /* TEST */
  172.