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