home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ARC521-2.ZIP / DIR_OS2.C < prev    next >
C/C++ Source or Header  |  1989-12-30  |  4KB  |  183 lines

  1. /*
  2.  * @(#)msd_dir.c 1.4 87/11/06    Public Domain.
  3.  *
  4.  *  A public domain implementation of BSD directory routines for
  5.  *  MS-DOS.  Written by Michael Rendell ({uunet,utai}michael@garfield),
  6.  *  August 1897
  7.  *  Ported to OS/2 by Kai Uwe Rommel
  8.  *  December 1989
  9.  */
  10.  
  11. #include    <sys/types.h>
  12. #include    <sys/stat.h>
  13. #include    <sys/dir.h>
  14. #include    <malloc.h>
  15. #include    <string.h>
  16.  
  17. #define INCL_NOPM
  18. #include <os2.h>
  19.  
  20. #ifndef    NULL
  21. # define    NULL    0
  22. #endif    /* NULL */
  23.  
  24. #ifndef    MAXPATHLEN
  25. # define    MAXPATHLEN    255
  26. #endif    /* MAXPATHLEN */
  27.  
  28. /* attribute stuff */
  29. #define    A_RONLY        0x01
  30. #define    A_HIDDEN    0x02
  31. #define    A_SYSTEM    0x04
  32. #define    A_LABEL        0x08
  33. #define    A_DIR        0x10
  34. #define    A_ARCHIVE    0x20
  35.  
  36.  
  37. #define Newisnull(a, t) ((a = (t *) malloc(sizeof(t))) == (t *) NULL)
  38.  
  39. #define ATTRIBUTES      0
  40. /* #define ATTRIBUTES      (A_DIR | A_HIDDEN | A_SYSTEM) */
  41. /* #define ATTRIBUTES      (A_RONLY | A_SYSTEM | A_DIR) */
  42.  
  43. static  char    *getdirent(char *);
  44. static    void    free_dircontents(struct _dircontents *);
  45.  
  46. static HDIR hdir;
  47. static USHORT count;
  48. static FILEFINDBUF find;
  49.  
  50.  
  51. DIR    *
  52. opendir(name)
  53.     char    *name;
  54. {
  55.     struct    stat        statb;
  56.     DIR            *dirp;
  57.     char            c;
  58.     char            *s;
  59.     struct _dircontents    *dp;
  60.     char            nbuf[MAXPATHLEN + 1];
  61.     
  62.     if (stat(name, &statb) < 0 || (statb.st_mode & S_IFMT) != S_IFDIR)
  63.         return (DIR *) NULL;
  64.     if (Newisnull(dirp, DIR))
  65.         return (DIR *) NULL;
  66.     if (*name && (c = name[strlen(name) - 1]) != '\\' && c != '/')
  67.         (void) strcat(strcpy(nbuf, name), "\\*.*");
  68.     else
  69.         (void) strcat(strcpy(nbuf, name), "*.*");
  70.     dirp->dd_loc = 0;
  71.         dirp->dd_contents = dirp->dd_cp = (struct _dircontents *) NULL;
  72.  
  73.     if ((s = getdirent(nbuf)) == (char *) NULL)
  74.         return dirp;
  75.     do {
  76.         if (Newisnull(dp, struct _dircontents) || (dp->_d_entry =
  77.             malloc((unsigned) (strlen(s) + 1))) == (char *) NULL)
  78.         {
  79.             if (dp)
  80.                 free((char *) dp);
  81.             free_dircontents(dirp->dd_contents);
  82.             return (DIR *) NULL;
  83.         }
  84.         if (dirp->dd_contents)
  85.             dirp->dd_cp = dirp->dd_cp->_d_next = dp;
  86.         else
  87.             dirp->dd_contents = dirp->dd_cp = dp;
  88.         (void) strcpy(dp->_d_entry, s);
  89.         dp->_d_next = (struct _dircontents *) NULL;
  90.         } while ((s = getdirent((char *) NULL)) != (char *) NULL);
  91.  
  92.     dirp->dd_cp = dirp->dd_contents;
  93.  
  94.     return dirp;
  95. }
  96.  
  97. void
  98. closedir(dirp)
  99.     DIR    *dirp;
  100. {
  101.     free_dircontents(dirp->dd_contents);
  102.     free((char *) dirp);
  103. }
  104.  
  105. struct direct    *
  106. readdir(dirp)
  107.     DIR    *dirp;
  108. {
  109.     static    struct direct    dp;
  110.     
  111.     if (dirp->dd_cp == (struct _dircontents *) NULL)
  112.         return (struct direct *) NULL;
  113.     dp.d_namlen = dp.d_reclen =
  114.         strlen(strcpy(dp.d_name, dirp->dd_cp->_d_entry));
  115.     strlwr(dp.d_name);        /* JF */
  116.     dp.d_ino = 0;
  117.     dirp->dd_cp = dirp->dd_cp->_d_next;
  118.     dirp->dd_loc++;
  119.  
  120.     return &dp;
  121. }
  122.  
  123. void
  124. seekdir(dirp, off)
  125.     DIR    *dirp;
  126.     long    off;
  127. {
  128.     long            i = off;
  129.     struct _dircontents    *dp;
  130.  
  131.     if (off < 0)
  132.         return;
  133.     for (dp = dirp->dd_contents ; --i >= 0 && dp ; dp = dp->_d_next)
  134.         ;
  135.     dirp->dd_loc = off - (i + 1);
  136.     dirp->dd_cp = dp;
  137. }
  138.  
  139. long
  140. telldir(dirp)
  141.     DIR    *dirp;
  142. {
  143.     return dirp->dd_loc;
  144. }
  145.  
  146. static    void
  147. free_dircontents(dp)
  148.     struct    _dircontents    *dp;
  149. {
  150.     struct _dircontents    *odp;
  151.  
  152.     while (dp) {
  153.         if (dp->_d_entry)
  154.             free(dp->_d_entry);
  155.         dp = (odp = dp)->_d_next;
  156.         free((char *) odp);
  157.     }
  158. }
  159.  
  160. static char *getdirent(dir)
  161. char *dir;
  162. {
  163.   int done;
  164.  
  165.   if (dir != (char *) NULL)
  166.   {                                     /* get first entry */
  167.     hdir = HDIR_CREATE;
  168.     count = 1;
  169.     done = DosFindFirst(dir, &hdir, ATTRIBUTES,
  170.                         &find, sizeof(find), &count, 0L);
  171.   }
  172.   else                                  /* get next entry */
  173.     done = DosFindNext(hdir, &find, sizeof(find), &count);
  174.  
  175.   if (done==0)
  176.     return find.achName;
  177.   else
  178.   {
  179.     DosFindClose(hdir);
  180.     return (char *) NULL;
  181.   }
  182. }
  183.