home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / UUPC11XS.ZIP / LIB / NDIRNT.C < prev    next >
C/C++ Source or Header  |  1992-11-27  |  5KB  |  159 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. /*    ndir.c for Windows/NT by Tom Loebach (loebach@mips.com),        */
  6. /*           April/92                                                 */
  7. /*                                                                    */
  8. /*         Berkeley-style directory reading routine on Windows NT     */
  9. /*--------------------------------------------------------------------*/
  10.  
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <assert.h>
  16.  
  17. /*--------------------------------------------------------------------*/
  18. /*                         Windows/NT include files                   */
  19. /*--------------------------------------------------------------------*/
  20.  
  21. #define INCL_BASE
  22.  
  23. #include <WINDOWS.h>
  24.  
  25.  
  26. /*--------------------------------------------------------------------*/
  27. /*                    UUPC/extended include files                     */
  28. /*--------------------------------------------------------------------*/
  29.  
  30. #include "lib.h"
  31. #include "ndir.h"
  32.  
  33.  
  34. static char *pathname = NULL;
  35. static HANDLE dirHandle;
  36. static WIN32_FIND_DATA dirData;
  37.  
  38. /*--------------------------------------------------------------------*/
  39. /*    o p e n d i r                                                   */
  40. /*                                                                    */
  41. /*    Open a directory                                                */
  42. /*--------------------------------------------------------------------*/
  43.  
  44. extern DIR *opendirx( char *dirname, char *pattern)
  45. {
  46.  
  47.    DIR *dirp;
  48.  
  49.    pathname = malloc( strlen( dirname ) + strlen( pattern ) + 2 );
  50.    strcpy(pathname, dirname);
  51.  
  52.  
  53.    if ((*pattern != '\\') || (dirname[ strlen(dirname) - 1] != '\\'))
  54.       strcat(pathname,"\\");
  55.  
  56.  
  57.    strcat(pathname,pattern);
  58.    printmsg(5,"opendir: Opening directory %s", pathname );
  59.  
  60. /*--------------------------------------------------------------------*/
  61. /*                Read the first file in the directory                */
  62. /*--------------------------------------------------------------------*/
  63.  
  64.  
  65.    dirHandle = FindFirstFile(pathname, &dirData);
  66.  
  67.    printmsg(5, "dirhandle = %d\n",dirHandle);
  68.    printmsg(5, "file, = %s\n", dirData.cFileName);
  69.  
  70.    if ((int)dirHandle == -1) {
  71.       printmsg(2,"opendir: Error on directory %s",pathname );
  72.       return NULL;
  73.    }
  74.    else {
  75.       dirp = malloc( sizeof( DIR ));
  76.       dirp->dirfirst = 1;
  77.       strcpy(dirp->dirid, "DIR");
  78.       return dirp;
  79.    }
  80.  
  81.  
  82. } /*opendir*/
  83.  
  84. /*--------------------------------------------------------------------*/
  85. /*    r e a d d i r                                                   */
  86. /*                                                                    */
  87. /*    Get next entry in a directory                                   */
  88. /*--------------------------------------------------------------------*/
  89.  
  90. struct direct *readdir(DIR *dirp)
  91. {
  92.  
  93.    BOOL rc;
  94.  
  95.    assert(strcmp(dirp->dirid, "DIR") == 0);
  96.    if (dirp->dirfirst)
  97.    {
  98.       printmsg(5,"readdir: Opening directory %s", pathname );
  99.       dirp->dirfirst = 0;
  100.    }else {
  101.       printmsg(5, "dirhandle = %d\n",dirHandle);
  102.       rc = FindNextFile(dirHandle, &dirData);
  103.    }
  104.  
  105.    if (!strcmp(dirData.cFileName,"."))
  106.       rc = FindNextFile(dirHandle, &dirData);
  107.  
  108.    printmsg(9, "readdir: file = %s\n", dirData.cFileName);
  109.  
  110.    if (!strcmp(dirData.cFileName,".."))
  111.       rc = FindNextFile(dirHandle, &dirData);
  112.  
  113.     printmsg(9, "file = %s\n", dirData.cFileName);
  114.  
  115.    if ( rc )
  116.    {
  117.       printmsg(9, "file = %s\n", dirData.cFileName);
  118.  
  119.       dirp->dirent.d_ino = -1;   /* no inode information */
  120.       strlwr(strcpy(dirp->dirent.d_name, dirData.cFileName));
  121.       dirp->dirent.d_namlen = strlen(dirData.cFileName);
  122.  
  123.       printmsg(9, "%d \n",dirp->dirent.d_namlen);
  124.  
  125.       dirp->dirent.d_reclen = sizeof(struct direct) - (MAXNAMLEN + 1) +
  126.          ((((dirp->dirent.d_namlen + 1) + 3) / 4) * 4);
  127.       return &(dirp->dirent);
  128.    } else {
  129.       printmsg(5,"readdir: Error on directory %s",pathname );
  130.       return NULL;
  131.    }
  132. } /*readdir*/
  133.  
  134. /*--------------------------------------------------------------------*/
  135. /*    c l o s e d i r                                                 */
  136. /*                                                                    */
  137. /*    Close a directory                                               */
  138. /*--------------------------------------------------------------------*/
  139.  
  140. void closedir(DIR *dirp)
  141. {
  142.  
  143.    BOOL rc;
  144.    
  145.    assert(strcmp(dirp->dirid, "DIR") == 0);
  146.  
  147.    printmsg(5,"closedir: Closing directory %s", pathname );
  148.  
  149.    rc = FindClose(dirHandle);
  150.    if (rc == 0)
  151.      printmsg(0,"closedir: Error %d on directory %s",
  152.               (int) rc, pathname );
  153.  
  154.    free( dirp );
  155.    dirp = NULL;
  156.    free( pathname );
  157.    pathname = NULL;
  158. } /*closedir*/
  159.