home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ARC521-2.ZIP / DIR_DOS.C < prev    next >
C/C++ Source or Header  |  1989-12-30  |  4KB  |  172 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.  *  Modified to use modern library functions 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. #include        <dos.h>
  18.  
  19. #ifndef    NULL
  20. # define    NULL    0
  21. #endif    /* NULL */
  22.  
  23. #ifndef    MAXPATHLEN
  24. # define    MAXPATHLEN    255
  25. #endif    /* MAXPATHLEN */
  26.  
  27. /* attribute stuff */
  28. #define    A_RONLY        0x01
  29. #define    A_HIDDEN    0x02
  30. #define    A_SYSTEM    0x04
  31. #define    A_LABEL        0x08
  32. #define    A_DIR        0x10
  33. #define    A_ARCHIVE    0x20
  34.  
  35.  
  36. #define Newisnull(a, t) ((a = (t *) malloc(sizeof(t))) == (t *) NULL)
  37.  
  38. #define ATTRIBUTES      0
  39. /* #define ATTRIBUTES      (A_DIR | A_HIDDEN | A_SYSTEM) */
  40. /* #define ATTRIBUTES      (A_RONLY | A_SYSTEM | A_DIR) */
  41.  
  42. static  char    *getdirent(char *);
  43. static    void    free_dircontents(struct _dircontents *);
  44.  
  45. static struct find_t find;
  46.  
  47.  
  48. DIR    *
  49. opendir(name)
  50.     char    *name;
  51. {
  52.     struct    stat        statb;
  53.     DIR            *dirp;
  54.     char            c;
  55.     char            *s;
  56.     struct _dircontents    *dp;
  57.     char            nbuf[MAXPATHLEN + 1];
  58.     
  59.     if (stat(name, &statb) < 0 || (statb.st_mode & S_IFMT) != S_IFDIR)
  60.         return (DIR *) NULL;
  61.     if (Newisnull(dirp, DIR))
  62.         return (DIR *) NULL;
  63.     if (*name && (c = name[strlen(name) - 1]) != '\\' && c != '/')
  64.         (void) strcat(strcpy(nbuf, name), "\\*.*");
  65.     else
  66.         (void) strcat(strcpy(nbuf, name), "*.*");
  67.     dirp->dd_loc = 0;
  68.         dirp->dd_contents = dirp->dd_cp = (struct _dircontents *) NULL;
  69.  
  70.     if ((s = getdirent(nbuf)) == (char *) NULL)
  71.         return dirp;
  72.     do {
  73.         if (Newisnull(dp, struct _dircontents) || (dp->_d_entry =
  74.             malloc((unsigned) (strlen(s) + 1))) == (char *) NULL)
  75.         {
  76.             if (dp)
  77.                 free((char *) dp);
  78.             free_dircontents(dirp->dd_contents);
  79.             return (DIR *) NULL;
  80.         }
  81.         if (dirp->dd_contents)
  82.             dirp->dd_cp = dirp->dd_cp->_d_next = dp;
  83.         else
  84.             dirp->dd_contents = dirp->dd_cp = dp;
  85.         (void) strcpy(dp->_d_entry, s);
  86.         dp->_d_next = (struct _dircontents *) NULL;
  87.         } while ((s = getdirent((char *) NULL)) != (char *) NULL);
  88.  
  89.     dirp->dd_cp = dirp->dd_contents;
  90.  
  91.     return dirp;
  92. }
  93.  
  94. void
  95. closedir(dirp)
  96.     DIR    *dirp;
  97. {
  98.     free_dircontents(dirp->dd_contents);
  99.     free((char *) dirp);
  100. }
  101.  
  102. struct direct    *
  103. readdir(dirp)
  104.     DIR    *dirp;
  105. {
  106.     static    struct direct    dp;
  107.     
  108.     if (dirp->dd_cp == (struct _dircontents *) NULL)
  109.         return (struct direct *) NULL;
  110.     dp.d_namlen = dp.d_reclen =
  111.         strlen(strcpy(dp.d_name, dirp->dd_cp->_d_entry));
  112.     strlwr(dp.d_name);        /* JF */
  113.     dp.d_ino = 0;
  114.     dirp->dd_cp = dirp->dd_cp->_d_next;
  115.     dirp->dd_loc++;
  116.  
  117.     return &dp;
  118. }
  119.  
  120. void
  121. seekdir(dirp, off)
  122.     DIR    *dirp;
  123.     long    off;
  124. {
  125.     long            i = off;
  126.     struct _dircontents    *dp;
  127.  
  128.     if (off < 0)
  129.         return;
  130.     for (dp = dirp->dd_contents ; --i >= 0 && dp ; dp = dp->_d_next)
  131.         ;
  132.     dirp->dd_loc = off - (i + 1);
  133.     dirp->dd_cp = dp;
  134. }
  135.  
  136. long
  137. telldir(dirp)
  138.     DIR    *dirp;
  139. {
  140.     return dirp->dd_loc;
  141. }
  142.  
  143. static    void
  144. free_dircontents(dp)
  145.     struct    _dircontents    *dp;
  146. {
  147.     struct _dircontents    *odp;
  148.  
  149.     while (dp) {
  150.         if (dp->_d_entry)
  151.             free(dp->_d_entry);
  152.         dp = (odp = dp)->_d_next;
  153.         free((char *) odp);
  154.     }
  155. }
  156.  
  157. static char *getdirent(dir)
  158. char *dir;
  159. {
  160.   int done;
  161.  
  162.   if (dir != (char *) NULL)
  163.     done = _dos_findfirst(dir, ATTRIBUTES, &find);
  164.   else                                  /* get next entry */
  165.     done = _dos_findnext(&find);
  166.  
  167.   if (done==0)
  168.     return find.name;
  169.   else
  170.     return (char *) NULL;
  171. }
  172.