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

  1. /*--------------------------------------------------------------------*/
  2.  
  3. /*    ndir.c for MS-DOS by Samuel Lam <skl@van-bc.UUCP>, June/87      */
  4.  
  5. /*    ndir.c for MS-OS2 by Drew Derbyshire (help@kendra.kew.com>,     */
  6.  
  7. /*           April/91                                                 */
  8.  
  9. /*    ndir.c for Windows/NT by Tom Loebach (loebach@mips.com),        */
  10. /*           April/92                                                 */
  11. /*                                                                    */
  12.  
  13. /*         Berkeley-style directory reading routine on Windows NT     */
  14.  
  15. /*--------------------------------------------------------------------*/
  16.  
  17.  
  18.  
  19. #include <stdio.h>
  20.  
  21. #include <ctype.h>
  22.  
  23. #include <string.h>
  24.  
  25. #include <stdlib.h>
  26.  
  27. #include <assert.h>
  28.  
  29.  
  30.  
  31. /*--------------------------------------------------------------------*/
  32.  
  33. /*                         Windows/NT include files                   */
  34.  
  35. /*--------------------------------------------------------------------*/
  36.  
  37.  
  38.  
  39. #define INCL_BASE
  40.  
  41.  
  42. #include <WINDOWS.h>
  43.  
  44.  
  45.  
  46.  
  47. /*--------------------------------------------------------------------*/
  48.  
  49. /*                    UUPC/extended include files                     */
  50.  
  51. /*--------------------------------------------------------------------*/
  52.  
  53.  
  54.  
  55. #include "lib.h"
  56.  
  57. #include "ndir.h"
  58.  
  59.  
  60.  
  61.  
  62. static char *pathname = NULL;
  63.  
  64. static HANDLE dirHandle;
  65. static WIN32_FIND_DATA dirData;
  66.  
  67. /*--------------------------------------------------------------------*/
  68.  
  69. /*    o p e n d i r                                                   */
  70.  
  71. /*                                                                    */
  72.  
  73. /*    Open a directory                                                */
  74.  
  75. /*--------------------------------------------------------------------*/
  76.  
  77.  
  78.  
  79. extern DIR *opendirx( char *dirname, char *pattern)
  80.  
  81. {
  82.  
  83.  
  84.    DIR *dirp;
  85.  
  86.    pathname = malloc( strlen( dirname ) + strlen( pattern ) + 2 );
  87.  
  88.    strcpy(pathname, dirname);
  89.  
  90.  
  91.  
  92.  
  93.    if ((*pattern != '\\') || (dirname[ strlen(dirname) - 1] != '\\'))
  94.  
  95.       strcat(pathname,"\\");
  96.  
  97.  
  98.  
  99.    strcat(pathname,pattern);
  100.  
  101.    printmsg(5,"opendir: Opening directory %s", pathname );
  102.  
  103.  
  104.  
  105. /*--------------------------------------------------------------------*/
  106.  
  107. /*                Read the first file in the directory                */
  108.  
  109. /*--------------------------------------------------------------------*/
  110.  
  111.  
  112.  
  113.    dirHandle = FindFirstFile(pathname, &dirData);
  114.  
  115.    printmsg(5, "dirhandle = %d\n",dirHandle);
  116.    printmsg(5, "file, = %s\n", dirData.cFileName);
  117.  
  118.    if ((int)dirHandle == -1) {
  119.       printmsg(2,"opendir: Error on directory %s",pathname );
  120.  
  121.       return NULL;
  122.  
  123.    }
  124.    else {
  125.       dirp = malloc( sizeof( DIR ));
  126.  
  127.       dirp->dirfirst = 1;
  128.  
  129.       strcpy(dirp->dirid, "DIR");
  130.  
  131.       return dirp;
  132.  
  133.    }
  134.  
  135.  
  136. } /*opendir*/
  137.  
  138.  
  139.  
  140. /*--------------------------------------------------------------------*/
  141.  
  142. /*    r e a d d i r                                                   */
  143.  
  144. /*                                                                    */
  145.  
  146. /*    Get next entry in a directory                                   */
  147.  
  148. /*--------------------------------------------------------------------*/
  149.  
  150.  
  151.  
  152. struct direct *readdir(DIR *dirp)
  153.  
  154. {
  155.  
  156.  
  157.    BOOL rc;
  158.  
  159.    assert(strcmp(dirp->dirid, "DIR") == 0);
  160.  
  161.    if (dirp->dirfirst)
  162.  
  163.    {
  164.  
  165.       printmsg(5,"readdir: Opening directory %s", pathname );
  166.  
  167.       dirp->dirfirst = 0;
  168.  
  169.    }
  170. else {
  171.       printmsg(5, "dirhandle = %d\n",dirHandle);
  172.       rc = FindNextFile(dirHandle, &dirData);
  173.    }
  174.  
  175.    if (!strcmp(dirData.cFileName,"."))
  176.       rc = FindNextFile(dirHandle, &dirData);
  177.  
  178.    printmsg(9, "readdir: file = %s\n", dirData.cFileName);
  179.  
  180.    if (!strcmp(dirData.cFileName,".."))
  181.       rc = FindNextFile(dirHandle, &dirData);
  182.  
  183.     printmsg(9, "file = %s\n", dirData.cFileName);
  184.  
  185.    if ( rc )
  186.    {
  187.       printmsg(9, "file = %s\n", dirData.cFileName);
  188.  
  189.       dirp->dirent.d_ino = -1;   /* no inode information */
  190.  
  191.       strlwr(strcpy(dirp->dirent.d_name, dirData.cFileName));
  192.  
  193.       dirp->dirent.d_namlen = strlen(dirData.cFileName);
  194.  
  195.       printmsg(9, "%d \n",dirp->dirent.d_namlen);
  196.  
  197.       dirp->dirent.d_reclen = sizeof(struct direct) - (MAXNAMLEN + 1) +
  198.  
  199.          ((((dirp->dirent.d_namlen + 1) + 3) / 4) * 4);
  200.  
  201.       return &(dirp->dirent);
  202.  
  203.    } else {
  204.  
  205.  
  206.       printmsg(5,"readdir: Error on directory %s",pathname );
  207.  
  208.       return NULL;
  209.  
  210.    }
  211. } /*readdir*/
  212.  
  213.  
  214.  
  215. /*--------------------------------------------------------------------*/
  216.  
  217. /*    c l o s e d i r                                                 */
  218.  
  219. /*                                                                    */
  220.  
  221. /*    Close a directory                                               */
  222.  
  223. /*--------------------------------------------------------------------*/
  224.  
  225.  
  226.  
  227. void closedir(DIR *dirp)
  228.  
  229. {
  230.  
  231.  
  232.    BOOL rc;
  233.    
  234.  
  235.    assert(strcmp(dirp->dirid, "DIR") == 0);
  236.  
  237.  
  238.  
  239.    printmsg(5,"closedir: Closing directory %s", pathname );
  240.  
  241.  
  242.    rc = FindClose(dirHandle);
  243.    if (rc == 0)
  244.      printmsg(0,"closedir: Error %d on directory %s",
  245.  
  246.               (int) rc, pathname );
  247.  
  248.  
  249.    free( dirp );
  250.  
  251.    dirp = NULL;
  252.  
  253.    free( pathname );
  254.  
  255.    pathname = NULL;
  256.  
  257. } /*closedir*/
  258.  
  259.