home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / PAX20.ZIP / DIR_OS2.C < prev    next >
C/C++ Source or Header  |  1990-11-12  |  4KB  |  187 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      (A_DIR | A_HIDDEN)
  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.         strcpy(nbuf, name);
  63.         if ( (c = nbuf[strlen(nbuf) - 1]) == '\\' || c == '/')
  64.           nbuf[strlen(nbuf) - 1] = 0;
  65.  
  66.         if (stat(nbuf, &statb) < 0 || (statb.st_mode & S_IFMT) != S_IFDIR)
  67.         return (DIR *) NULL;
  68.     if (Newisnull(dirp, DIR))
  69.         return (DIR *) NULL;
  70.     if (*name && (c = name[strlen(name) - 1]) != '\\' && c != '/')
  71.         (void) strcat(strcpy(nbuf, name), "\\*.*");
  72.     else
  73.                 (void) strcat(strcpy(nbuf, name), "*.*");
  74.     dirp->dd_loc = 0;
  75.         dirp->dd_contents = dirp->dd_cp = (struct _dircontents *) NULL;
  76.  
  77.     if ((s = getdirent(nbuf)) == (char *) NULL)
  78.         return dirp;
  79.     do {
  80.         if (Newisnull(dp, struct _dircontents) || (dp->_d_entry =
  81.             malloc((unsigned) (strlen(s) + 1))) == (char *) NULL)
  82.         {
  83.             if (dp)
  84.                 free((char *) dp);
  85.             free_dircontents(dirp->dd_contents);
  86.             return (DIR *) NULL;
  87.         }
  88.         if (dirp->dd_contents)
  89.             dirp->dd_cp = dirp->dd_cp->_d_next = dp;
  90.         else
  91.             dirp->dd_contents = dirp->dd_cp = dp;
  92.         (void) strcpy(dp->_d_entry, s);
  93.         dp->_d_next = (struct _dircontents *) NULL;
  94.         } while ((s = getdirent((char *) NULL)) != (char *) NULL);
  95.  
  96.     dirp->dd_cp = dirp->dd_contents;
  97.  
  98.     return dirp;
  99. }
  100.  
  101. void
  102. closedir(dirp)
  103.     DIR    *dirp;
  104. {
  105.     free_dircontents(dirp->dd_contents);
  106.     free((char *) dirp);
  107. }
  108.  
  109. struct direct    *
  110. readdir(dirp)
  111.     DIR    *dirp;
  112. {
  113.     static    struct direct    dp;
  114.     
  115.     if (dirp->dd_cp == (struct _dircontents *) NULL)
  116.         return (struct direct *) NULL;
  117.     dp.d_namlen = dp.d_reclen =
  118.         strlen(strcpy(dp.d_name, dirp->dd_cp->_d_entry));
  119.     strlwr(dp.d_name);        /* JF */
  120.     dp.d_ino = 0;
  121.     dirp->dd_cp = dirp->dd_cp->_d_next;
  122.     dirp->dd_loc++;
  123.  
  124.     return &dp;
  125. }
  126.  
  127. void
  128. seekdir(dirp, off)
  129.     DIR    *dirp;
  130.     long    off;
  131. {
  132.     long            i = off;
  133.     struct _dircontents    *dp;
  134.  
  135.     if (off < 0)
  136.         return;
  137.     for (dp = dirp->dd_contents ; --i >= 0 && dp ; dp = dp->_d_next)
  138.         ;
  139.     dirp->dd_loc = off - (i + 1);
  140.     dirp->dd_cp = dp;
  141. }
  142.  
  143. long
  144. telldir(dirp)
  145.     DIR    *dirp;
  146. {
  147.     return dirp->dd_loc;
  148. }
  149.  
  150. static    void
  151. free_dircontents(dp)
  152.     struct    _dircontents    *dp;
  153. {
  154.     struct _dircontents    *odp;
  155.  
  156.     while (dp) {
  157.         if (dp->_d_entry)
  158.             free(dp->_d_entry);
  159.         dp = (odp = dp)->_d_next;
  160.         free((char *) odp);
  161.     }
  162. }
  163.  
  164. static char *getdirent(dir)
  165. char *dir;
  166. {
  167.   int done;
  168.  
  169.   if (dir != (char *) NULL)
  170.   {                                     /* get first entry */
  171.     hdir = HDIR_CREATE;
  172.     count = 1;
  173.     done = DosFindFirst(dir, &hdir, ATTRIBUTES,
  174.                         &find, sizeof(find), &count, 0L);
  175.   }
  176.   else                                  /* get next entry */
  177.     done = DosFindNext(hdir, &find, sizeof(find), &count);
  178.  
  179.   if (done==0)
  180.     return find.achName;
  181.   else
  182.   {
  183.     DosFindClose(hdir);
  184.     return (char *) NULL;
  185.   }
  186. }
  187.