File:  [WindowsNT SDKs] / mstools / samples / filer / walk.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 18:22:14 2018 UTC (7 years, 9 months ago) by root
Branches: msft, MAIN
CVS tags: ntsdk-oct-1992, HEAD
Microsoft Windows NT Build 328 10-12-1992

/****************************************************************************
*
*
*    Microsoft Developer Support
*    Copyright (c) 1992 Microsoft Corporation
*
*
*    PROGRAM: WALK.C
*
*    PURPOSE: Uses recursion to walk through all subdirectories in the
*             current working directory
*
*    FUNCTIONS:
*
*        FindFirstDirectory() - finds the first directory in the current
*                 working directory
*        Walk() - finds the subdirectories in the current working directory,
*                 changes the current working directory to this subdirectory,
*                 recusively calls itself until there are no more
*                 subdirectories
*
*    COMMENTS:
*
****************************************************************************/
#define  STRICT
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <process.h>
#include "globals.h"
#include "filer.h"
#include "walk.h"

extern CRITICAL_SECTION     gSetDirCS;

/****************************************************************************
*
*    FUNCTION: FindFirstDirectory(LPTSTR, LPWIN32_FIND_DATA)
*
*    PURPOSE: finds the first directory in the current working directory
*
*    COMMENTS:
*
*       This function is called by Walk() each time a new subdirectory is
*       entered.  Since only directory entries are of interest, this call
*       provides a simple means to bypass non-directory filenames.
*
*    INPUT: lpszSearchFile -> "*"
*           lpffd - pointer to the file find data structure of type
*                   WIN32_FIND_DATA
*
*    OUTPUT: Returns a file handle if a directory is found
*            Returns a -1 if no directory is found        
*
****************************************************************************/

HANDLE FindFirstDirectory(LPTSTR lpszSearchFile, LPWIN32_FIND_DATA lpffd)
{
  BOOL     fRC;
  DWORD    dwRC;
  HANDLE   hFile;
  int      iRC;

  hFile=FindFirstFile(lpszSearchFile,lpffd);
  if (hFile == (HANDLE) -1)
     return (hFile);
  for(;;){
     dwRC=GetFileAttributes(lpffd->cFileName);
     if (dwRC & FILE_ATTRIBUTE_DIRECTORY){
        iRC=strcmp(lpffd->cFileName,".");
        if (iRC){
           iRC=strcmp(lpffd->cFileName,"..");
           if (iRC)
              return (hFile);
        }
     }
     fRC=FindNextFile(hFile,lpffd);
     if (fRC == FALSE){
        FindClose(hFile);
        return ((HANDLE) -1);
     }
  }
}

/****************************************************************************
*
*    FUNCTION: FindNextDirectory(LPTSTR, LPWIN32_FIND_DATA)
*
*    PURPOSE: finds the next directory in the current working directory
*
*    COMMENTS:
*
*       This function is called by Walk() each time a new subdirectory is
*       entered.  Since only directory entries are of interest, this call
*       provides a simple means to bypass non-directory filenames.
*
*    INPUT: lpszSearchFile -> "*"
*           lpffd - pointer to the file find data structure of type
*                   WIN32_FIND_DATA
*
*    OUTPUT: Returns a file handle if a directory is found
*            Returns a -1 if no directory is found        
*
****************************************************************************/

BOOL FindNextDirectory(HANDLE hDir, LPWIN32_FIND_DATA lpffd)
{
  BOOL     fRC;
  DWORD    dwRC;

  for(;;){
     fRC=FindNextFile(hDir,lpffd);
     if (fRC == FALSE)
        return (FALSE);
     dwRC=GetFileAttributes(lpffd->cFileName);
     if (dwRC & FILE_ATTRIBUTE_DIRECTORY)
        return (TRUE);
  }
}

/****************************************************************************
*
*    FUNCTION: Walk(WORD)
*
*    PURPOSE: finds a subdirectory in the current working directory,
*             changes the current working directory to this subdirectory,
*             and recusively calls itself until there are no more
*             subdirectories
*
*    COMMENTS:When a new directory is entered from above, a handle for
*             the new directory is obtained using FindFirstDirectory.  Once
*             the first directory is found, the current working directory
*             is changed to this first directory and Walk() is recursively
*             called again.  At this point, the next available directory
*             is searched for using FindNextFile, entered and a recursive
*             call is made to Walk().  When each directory has been searched,
*             until no more directories exist, the current working directory
*             is changed to the parent directory (..).  This continues until
*             the current working directory is equal to the original
*             directory.
*
*    INPUT: wLevel - bookmark, when wLevel is greater than 0, then the
*             current working directory is a subdirectory of the original
*             directory.  If wLevel is equal to 0, then the directory is the
*             original directory and the recursive calls stop
*
*    OUTPUT: 
*
****************************************************************************/

VOID Walk(HWND hwnd, LPSTR lpszPathName, WORD wLevel, LPBOOL lpfAlive)
{
    BOOL              fRC=TRUE;
    DWORD             dwCurrent;
    HANDLE            hDir;
    WIN32_FIND_DATA   w32FindBuf;

    if( *lpfAlive == FALSE )
        return;

    dwCurrent=GetCurrentDirectory(DIRECTORY_STRING_SIZE, lpszPathName);
    if( dwCurrent >= (DIRECTORY_STRING_SIZE-1) ){
        ErrorMsg("Walk: Buffer Size error.");
        return;
    }

    SendMessage(hwnd, LB_ADDSTRING, NULL, (LPARAM)lpszPathName);

    hDir=FindFirstDirectory("*",&w32FindBuf);
    if (hDir == (HANDLE) -1){

        if (wLevel)
            SetCurrentDirectory("..");

        return;
    }

    for (;;){
        SetCurrentDirectory(w32FindBuf.cFileName);
        Walk(hwnd, lpszPathName, ++wLevel, lpfAlive);
        fRC=FindNextDirectory(hDir,&w32FindBuf);

        if (fRC == FALSE){
            SetCurrentDirectory("..");
            FindClose(hDir);
            return;
        }
    }
}

BOOL EnumDir(HANDLE hwnd, LPCINFO lpCInfo)
{
    char                szPathName[DIRECTORY_STRING_SIZE];
    LPCRITICAL_SECTION  lpCS;
    BOOL                fExitCode = TRUE;

    if( hwnd == lpCInfo->hwndLL)
        lpCS = &(lpCInfo->CritSecL);
    else
        lpCS = &(lpCInfo->CritSecR);

    EnterCriticalSection(lpCS);
    EnterCriticalSection(&gSetDirCS);

    lpCInfo->fAlive = TRUE;

    if( SetCurrentDirectory( lpCInfo->lpDriveInfo->DriveName ) ){

        //
        // Empty the ListBox of its contents,
        // Clear the redraw flag, and build the directory tree.
        //
        SendMessage(hwnd, LB_RESETCONTENT, (WPARAM)NULL, (LPARAM)NULL);
//        SendMessage(hwnd, WM_SETREDRAW, (WPARAM)0, (LPARAM)NULL);

        Walk(hwnd, (LPSTR)szPathName, 0, &(lpCInfo->fAlive));

        if( lpCInfo->fAlive == FALSE )
            fExitCode = TRUE;
        else{
            //
            // set the redraw flag, then add and remove a blank string,
            // in order to update the window.
            //
            SendMessage(hwnd, WM_SETREDRAW, (WPARAM)1, (LPARAM)NULL);
            SendMessage(hwnd, LB_DELETESTRING,
                SendMessage(hwnd, LB_ADDSTRING, (WPARAM)NULL, (LPARAM)" "),
                (LPARAM)NULL);
        }
    }
    else
        fExitCode = FALSE;

    lpCInfo->fAlive = FALSE;

    LeaveCriticalSection(lpCS);
    LeaveCriticalSection(&gSetDirCS);

    return(fExitCode);
}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.