home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / UUPC11XS.ZIP / LIB / NDIROS2.C < prev    next >
C/C++ Source or Header  |  1992-11-27  |  5KB  |  164 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    ndir.c for MS-DOS by Samuel Lam <skl@van-bc.UUCP>, June/87      */
  3. /*    ndir.c for MS-OS2 by Drew Derbyshire (help@kendra.kew.com>,     */
  4. /*           April/91                                                 */
  5. /*                                                                    */
  6. /*         Berkeley-style directory reading routine on MS-OS2         */
  7. /*--------------------------------------------------------------------*/
  8.  
  9. #include <stdio.h>
  10. #include <ctype.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <assert.h>
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                         OS/2 include files                         */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. #define INCL_BASE
  20. #include <os2.h>
  21.  
  22. /*--------------------------------------------------------------------*/
  23. /*                    UUPC/extended include files                     */
  24. /*--------------------------------------------------------------------*/
  25.  
  26. #include "lib.h"
  27. #include "ndir.h"
  28.  
  29. static HDIR dir_handle;
  30. static char *pathname = NULL;
  31. static struct _FILEFINDBUF findbuf;
  32.  
  33. currentfile();
  34.  
  35. /*--------------------------------------------------------------------*/
  36. /*    o p e n d i r                                                   */
  37. /*                                                                    */
  38. /*    Open a directory                                                */
  39. /*--------------------------------------------------------------------*/
  40.  
  41. extern DIR *opendirx( const char *dirname, char *pattern)
  42. {
  43.  
  44.    DIR *dirp;
  45.    USHORT rc;
  46.    USHORT count = 1;
  47.  
  48.    pathname = malloc( strlen( dirname ) + strlen( pattern ) + 2 );
  49.    strcpy(pathname, dirname);
  50.  
  51.    if ((*pattern != '/') && (dirname[ strlen(dirname) - 1] != '/'))
  52.       strcat(pathname,"/");
  53.    strcat(pathname,pattern);
  54.    printmsg(5,"opendir: Opening directory %s", pathname );
  55.  
  56. /*--------------------------------------------------------------------*/
  57. /*                Read the first file in the directory                */
  58. /*--------------------------------------------------------------------*/
  59.  
  60.    dir_handle = HDIR_CREATE;
  61.    rc = DosFindFirst( pathname,
  62.             &dir_handle,
  63.             FILE_NORMAL,
  64.             &findbuf,
  65.             sizeof( findbuf ),
  66.             &count,
  67.             0L );
  68.  
  69. /*--------------------------------------------------------------------*/
  70. /*            Process the return code from the first file             */
  71. /*--------------------------------------------------------------------*/
  72.  
  73.    if ( rc == 0 )
  74.    {
  75.       dirp = malloc( sizeof( DIR ));
  76.       dirp->dirfirst = 1;
  77.       strcpy(dirp->dirid, "DIR");
  78.       return dirp;
  79.    }
  80.    else {
  81.       if (( rc != ERROR_NO_MORE_FILES ) &&
  82.           ( rc != ERROR_PATH_NOT_FOUND))
  83.          printmsg(4,"opendir: Error %d on directory %s",
  84.                   (int) rc, pathname );
  85.       return NULL;
  86.    } /* else */
  87.  
  88. } /*opendir*/
  89.  
  90. /*--------------------------------------------------------------------*/
  91. /*    r e a d d i r                                                   */
  92. /*                                                                    */
  93. /*    Get next entry in a directory                                   */
  94. /*--------------------------------------------------------------------*/
  95.  
  96. struct direct *readdir(DIR *dirp)
  97. {
  98.    USHORT rc = 0;
  99.    USHORT count = 1;
  100.  
  101.    if ( ! equal(dirp->dirid, "DIR" ))
  102.    {
  103.       printmsg(0,"readdir: No directory open to read");
  104.       panic();
  105.    }
  106.  
  107.    if (dirp->dirfirst)
  108.    {
  109.       printmsg(5,"readdir: Opening directory %s", pathname );
  110.       dirp->dirfirst = 0;
  111.    }
  112.    else
  113.       rc = DosFindNext( dir_handle,
  114.                &findbuf,
  115.                sizeof( findbuf ) ,
  116.                &count );
  117.  
  118.    if ( rc == 0 )
  119.    {
  120.       dirp->dirent.d_ino = -1;   /* no inode information */
  121.       strlwr(strcpy(dirp->dirent.d_name, findbuf.achName ));
  122.       dirp->dirent.d_namlen = findbuf.cchName;
  123.       dirp->dirent.d_reclen = sizeof(struct direct) - (MAXNAMLEN + 1) +
  124.          ((((dirp->dirent.d_namlen + 1) + 3) / 4) * 4);
  125.       printmsg(4,"readdir: Returning \"%s\"", dirp->dirent.d_name);
  126.       return &(dirp->dirent);
  127.    }
  128.    else {
  129.       if ( rc != ERROR_NO_MORE_FILES )
  130.          printmsg(0,"readdir: Error %d on directory %s",
  131.                   (int) rc, pathname );
  132.       return NULL;
  133.    } /* else */
  134.  
  135. } /*readdir*/
  136.  
  137. /*--------------------------------------------------------------------*/
  138. /*    c l o s e d i r                                                 */
  139. /*                                                                    */
  140. /*    Close a directory                                               */
  141. /*--------------------------------------------------------------------*/
  142.  
  143. void closedir(DIR *dirp)
  144. {
  145.    USHORT rc;
  146.  
  147.    if ( ! equal(dirp->dirid, "DIR" ))
  148.    {
  149.       printmsg(0,"closedir: No directory open");
  150.       panic();
  151.    }
  152.  
  153.    printmsg(5,"closedir: Closing directory %s", pathname );
  154.    rc = DosFindClose( dir_handle );
  155.    if ( rc != 0 )
  156.      printmsg(0,"closedir: Error %d on directory %s",
  157.               (int) rc, pathname );
  158.    free( dirp );
  159.    dirp = NULL;
  160.    free( pathname );
  161.    pathname = NULL;
  162.  
  163. } /*closedir*/
  164.