home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / INTERNET / UPC2S1.ZIP / NDIRNT.C < prev    next >
C/C++ Source or Header  |  1993-04-10  |  7KB  |  197 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. /*    ndir.c for NT extended to include timestamp information by      */
  8. /*           Dave Watt, April/93                                      */
  9. /*                                                                    */
  10. /*         Berkeley-style directory reading routine on Windows NT     */
  11. /*--------------------------------------------------------------------*/
  12.  
  13. /*--------------------------------------------------------------------*/
  14. /*    Changes Copyright (c) 1990-1993 by Kendra Electronic            */
  15. /*    Wonderworks.                                                    */
  16. /*                                                                    */
  17. /*    All rights reserved except those explicitly granted by the      */
  18. /*    UUPC/extended license agreement.                                */
  19. /*--------------------------------------------------------------------*/
  20.  
  21. /*
  22.  *       $Id: NDIRNT.C 1.4 1993/04/11 00:33:38 dmwatt Exp $
  23.  *
  24.  *       $Log: NDIRNT.C $
  25.  *     Revision 1.4  1993/04/11  00:33:38  dmwatt
  26.  *     Global edits for year, TEXT, etc.
  27.  *
  28.  *     Revision 1.3  1993/04/10  21:22:29  dmwatt
  29.  *     Windows/NT fixes
  30.  *
  31.  *     Revision 1.2  1993/01/01  01:21:29  dmwatt
  32.  *     Add currentfile() to support strpool memory handling
  33.  *
  34.  *     Revision 1.2  1993/01/01  01:21:29  dmwatt
  35.  *     Add currentfile() to support strpool memory handling
  36.  *
  37.  */
  38.  
  39. #include <stdio.h>
  40. #include <ctype.h>
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include <assert.h>
  44.  
  45. /*--------------------------------------------------------------------*/
  46. /*                         Windows/NT include files                   */
  47. /*--------------------------------------------------------------------*/
  48.  
  49. #define INCL_BASE
  50.  
  51. #include <WINDOWS.h>
  52.  
  53.  
  54. /*--------------------------------------------------------------------*/
  55. /*                    UUPC/extended include files                     */
  56. /*--------------------------------------------------------------------*/
  57.  
  58. #include "lib.h"
  59. #include "uundir.h"
  60. #include "dos2unix.h"
  61.  
  62. static char *pathname = NULL;
  63. static HANDLE dirHandle;
  64. static WIN32_FIND_DATA dirData;
  65. currentfile();
  66.  
  67. /*--------------------------------------------------------------------*/
  68. /*    o p e n d i r                                                   */
  69. /*                                                                    */
  70. /*    Open a directory                                                */
  71. /*--------------------------------------------------------------------*/
  72.  
  73. extern DIR *opendirx( const char *dirname, char *pattern)
  74. {
  75.  
  76.    DIR *dirp;
  77.  
  78.    pathname = malloc( strlen( dirname ) + strlen( pattern ) + 2 );
  79.    strcpy(pathname, dirname);
  80.  
  81.  
  82.    if ((*pattern != '\\') || (dirname[ strlen(dirname) - 1] != '\\'))
  83.       strcat(pathname,"\\");
  84.  
  85.  
  86.    strcat(pathname,pattern);
  87.    printmsg(5,"opendir: Opening directory %s", pathname );
  88.  
  89. /*--------------------------------------------------------------------*/
  90. /*                Read the first file in the directory                */
  91. /*--------------------------------------------------------------------*/
  92.  
  93.  
  94.    dirHandle = FindFirstFile(pathname, &dirData);
  95.  
  96.    printmsg(5, "dirhandle = %d\n",dirHandle);
  97.    printmsg(5, "file, = %s\n", dirData.cFileName);
  98.  
  99.    if ((int)dirHandle == -1) {
  100.       printmsg(2,"opendir: Error on directory %s",pathname );
  101.       return NULL;
  102.    }
  103.    else {
  104.       dirp = malloc( sizeof( DIR ));
  105.       dirp->dirfirst = 1;
  106.       strcpy(dirp->dirid, "DIR");
  107.       return dirp;
  108.    }
  109.  
  110.  
  111. } /*opendir*/
  112.  
  113. /*--------------------------------------------------------------------*/
  114. /*    r e a d d i r                                                   */
  115. /*                                                                    */
  116. /*    Get next entry in a directory                                   */
  117. /*--------------------------------------------------------------------*/
  118.  
  119. struct direct *readdir(DIR *dirp)
  120. {
  121.  
  122.    BOOL rc;
  123.  
  124.    assert(strcmp(dirp->dirid, "DIR") == 0);
  125.    if (dirp->dirfirst)
  126.    {
  127.       printmsg(5,"readdir: Opening directory %s", pathname );
  128.       dirp->dirfirst = 0;
  129.    } else {
  130.       printmsg(5, "dirhandle = %d\n",dirHandle);
  131.       rc = FindNextFile(dirHandle, &dirData);
  132.    }
  133.  
  134.    if (!strcmp(dirData.cFileName,"."))
  135.       rc = FindNextFile(dirHandle, &dirData);
  136.  
  137.    printmsg(9, "readdir: file = %s\n", dirData.cFileName);
  138.  
  139.    if (!strcmp(dirData.cFileName,".."))
  140.       rc = FindNextFile(dirHandle, &dirData);
  141.  
  142.         printmsg(9, "file = %s\n", dirData.cFileName);
  143.  
  144.    if ( rc )
  145.    {
  146.       printmsg(9, "file = %s\n", dirData.cFileName);
  147.  
  148.       dirp->dirent.d_ino = -1;   /* no inode information */
  149.       strlwr(strcpy(dirp->dirent.d_name, dirData.cFileName));
  150.       dirp->dirent.d_namlen = strlen(dirData.cFileName);
  151.  
  152.       printmsg(9, "%d \n",dirp->dirent.d_namlen);
  153.       dirp->dirent.d_modified = nt2unix(&dirData.ftLastWriteTime);
  154.  
  155.       if (dirData.nFileSizeHigh > 0) {
  156.          printmsg(0, "readdir:  File %s larger than 2^32 bits?!",
  157.             dirData.cFileName);
  158.          panic();
  159.       }
  160.  
  161.       dirp->dirent.d_size = dirData.nFileSizeLow;
  162.       dirp->dirent.d_reclen = sizeof(struct direct) - (MAXNAMLEN + 1) +
  163.          ((((dirp->dirent.d_namlen + 1) + 3) / 4) * 4);
  164.       return &(dirp->dirent);
  165.    } else {
  166.  
  167.       printmsg(5,"readdir: Error on directory %s",pathname );
  168.       return NULL;
  169.    }
  170. } /*readdir*/
  171.  
  172. /*--------------------------------------------------------------------*/
  173. /*    c l o s e d i r                                                 */
  174. /*                                                                    */
  175. /*    Close a directory                                               */
  176. /*--------------------------------------------------------------------*/
  177.  
  178. void closedir(DIR *dirp)
  179. {
  180.  
  181.    BOOL rc;
  182.  
  183.    assert(strcmp(dirp->dirid, "DIR") == 0);
  184.  
  185.    printmsg(5,"closedir: Closing directory %s", pathname );
  186.  
  187.    rc = FindClose(dirHandle);
  188.    if (rc == 0)
  189.      printmsg(0,"closedir: Error %d on directory %s",
  190.               (int) rc, pathname );
  191.  
  192.    free( dirp );
  193.    dirp = NULL;
  194.    free( pathname );
  195.    pathname = NULL;
  196. } /*closedir*/
  197.