Index: CMakeLists.txt =================================================================== --- CMakeLists.txt (revision 0) +++ CMakeLists.txt (working copy) @@ -0,0 +1,5 @@ +add_executable(tree tree.c) +set_module_type(tree win32cui) +set_target_properties(tree PROPERTIES SUFFIX ".com") +add_importlibs(tree msvcrt kernel32) +add_cd_file(TARGET tree DESTINATION reactos/system32 FOR all) Index: stdafx.h =================================================================== --- stdafx.h (revision 0) +++ stdafx.h (working copy) @@ -0,0 +1,56 @@ +/* +* PROJECT: ReactOS Kernel +* LICENSE: GNU GPLv2 only as published by the Free Software Foundation +* PURPOSE: Implements tree.com functionality similar to Windows +* PROGRAMMERS: Asif Bahrainwala (asif_bahrainwala@hotmail.com) +*/ + +// stdafx.h : include file for standard system include files, +// or project specific include files that are used frequently, but +// are changed infrequently +// + +#pragma once + +//#include "targetver.h" + +#include +#include + + + +// TODO: reference additional headers your program requires here +#include +//#include +//#include +//using namespace std; + +#define STR_MAX 2048 + +/** +* @name: GetDirectoryStructure +* +* @param strPath +* Must specify folder name +* +* @param width +* specifies drawing distance for correct formating of tree structure being drawn on console screen +* +* @param prevLine +* specifies the previous line writtn on console, is used for correct formating +* @return +* void +*/ +static void GetDirectoryStructure(wchar_t* strPath,UINT width/*used internally for adding spaces*/,wchar_t *prevLine/*used internally for formating reasons*/); + +/** +* @name: HasSubFolder +* +* @param strPath +* Must specify folder name +* +* @return +* true if folder has sub folders, else will return false +*/ +static BOOL HasSubFolder(const wchar_t *strPath1); + Index: tree.c =================================================================== --- tree.c (revision 0) +++ tree.c (working copy) @@ -0,0 +1,300 @@ +/* +* PROJECT: ReactOS Kernel +* LICENSE: GNU GPLv2 only as published by the Free Software Foundation +* PURPOSE: Implements tree.com functionality similar to Windows +* PROGRAMMERS: Asif Bahrainwala (asif_bahrainwala@hotmail.com) +*/ + +// Tree.cpp : Defines the entry point for the console application. +// +#include "stdafx.h" + + +const char* HELP = "\nGraphically displays the folder structure of a drive or path. \n\nTREE [drive:][path] [/F] [/A]\n\n /F Display the names of the files in each folder.\n\n\n"; +const char *INVALID ="No subfolders exist"; + +BOOL bShowFiles=FALSE; //if this flag is set to true, files will also be listed + +/** +* @name: HasSubFolder +* +* @param strPath +* Must specify folder name +* +* @return +* true if folder has sub folders, else will return false +*/ +static BOOL HasSubFolder(const wchar_t *strPath1) +{ + BOOL ret=FALSE; + WIN32_FIND_DATA FindFileData; + HANDLE hFind = NULL; + static wchar_t strPath[STR_MAX]=L""; + ZeroMemory(strPath,sizeof(strPath)); + + wcscat(strPath,strPath1); + wcscat(strPath,L"\\*."); + + hFind=FindFirstFile(strPath, &FindFileData); + do + { + if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + { + if(wcscmp(FindFileData.cFileName,L".")==0 || + wcscmp(FindFileData.cFileName,L"..")==0 ){ + continue; + } + + ret=TRUE; //found subfolder + break; + } + } + while(FindNextFile(hFind,&FindFileData)); + + FindClose(hFind); + return ret; +} + +/** +* @name: DrawTree +* +* @param strPath +* Must specify folder name +* +* @param arrFolder +* must be a list of folder names to be drawn in tree format +* +* @param width +* specifies drawing distance for correct formating of tree structure being drawn on console screen +* +* @return +* void +*/ +static void DrawTree(const wchar_t* strPath,const WIN32_FIND_DATA *arrFolder,const size_t szArr,UINT width /*used internally for adding spaces*/,const wchar_t *prevLine/*used internally for formating reasons*/,BOOL drawfolder) +{ + BOOL bHasSubFolder=HasSubFolder(strPath); + UINT i=0; + + for(i=0;i arrFolder; //will fill up with names of all sub folders + WIN32_FIND_DATA *arrFolder=0;UINT arrFoldersz=0; + //vector arrFile; //will fill up with names of all sub folders + WIN32_FIND_DATA *arrFile=0;UINT arrFilesz=0; + + ZeroMemory(&FindFileData,sizeof(FindFileData)); + + { + static wchar_t tmp[STR_MAX]=L""; + ZeroMemory(tmp,sizeof(tmp)); + wcscat(tmp,strPath); + wcscat(tmp,L"\\*.*"); + + hFind=FindFirstFile(tmp, &FindFileData); + + err=GetLastError(); + } + + + if(hFind==INVALID_HANDLE_VALUE) + return; + + do + { + if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + { + if(wcscmp(FindFileData.cFileName,L".")==0 || + wcscmp(FindFileData.cFileName,L"..")==0 ) + continue; + + ++arrFoldersz; + arrFolder=(WIN32_FIND_DATA*)realloc(arrFolder,arrFoldersz*sizeof(FindFileData)); + + if(arrFolder==NULL) + exit(-1); + + arrFolder[arrFoldersz-1]=FindFileData; + + } + else + { + ++arrFilesz; + arrFile=(WIN32_FIND_DATA*)realloc(arrFile,arrFilesz*sizeof(FindFileData)); + + if(arrFile==NULL) + exit(-1); + + arrFile[arrFilesz-1]=FindFileData; + } + } + while(FindNextFile(hFind,&FindFileData)); + + FindClose(hFind); + + if(bShowFiles){ + DrawTree(strPath,arrFile,arrFilesz,width,prevLine,FALSE); //will free(arrFile) + } + + DrawTree(strPath,arrFolder,arrFoldersz,width,prevLine,TRUE); //will free(arrFile) + + free(arrFolder); + free(arrFile); +} + +/** +* @name: main +* standard main functionality as required by C/C++ for application startup +* +* @return +* error /success value +*/ +int _tmain(int argc, _TCHAR* argv[]) +{ + //printf("Tree command by Asif Bahrainwala (asif_bahrainwala@hotmail.com), for ReactOS\n"); + DWORD dwSerial=0; + wchar_t t=0; + wchar_t *strPath=NULL; + DWORD sz=0; + wchar_t *context=NULL ; + wchar_t *driveLetter=NULL; + + int i;for(i=1;i>16,dwSerial&0xffff); + + sz=GetCurrentDirectory(1,&t); //get the buffer size + strPath=(wchar_t*)malloc(sizeof(wchar_t)*sz); //must not return before calling delete[] + + GetCurrentDirectory(sz,strPath); //get the current directory + + + driveLetter=(wchar_t*)malloc(sizeof(wchar_t)*sz); //get the drive letter , must not return before calling delete[] + + wcscpy_s(driveLetter,sz,strPath); + wcstok_s(driveLetter,L":",&context); //parse for the drive letter + + + wprintf(L"%s:.\n",driveLetter); + + free( driveLetter); + + + //int testcase=0; + //for(testcase=0;testcase<1000;testcase++) //this is for testing only, to determine resource leak + GetDirectoryStructure(strPath,1,L" "); //get the sub directories within this current folder + + free(strPath); + wprintf(L"\n"); + + return 0; +} + Index: . =================================================================== --- . (revision 0) +++ . (working copy) Property changes on: . ___________________________________________________________________ Added: bugtraq:logregex ## -0,0 +1,2 ## +([Ii]ssue|[Bb]ug)s? #?(\d+)(,? ?#?(\d+))*(,? ?(and |or )?#?(\d+))? +(\d+) \ No newline at end of property Added: bugtraq:url ## -0,0 +1 ## +http://www.reactos.org/bugzilla/show_bug.cgi?id=%BUGID% \ No newline at end of property Added: bugtraq:message ## -0,0 +1 ## +See issue #%BUGID% for more details. \ No newline at end of property Index: CMakeLists.txt =================================================================== --- CMakeLists.txt (revision 0) +++ CMakeLists.txt (working copy) @@ -0,0 +1,5 @@ +add_executable(tree tree.c) +set_module_type(tree win32cui) +set_target_properties(tree PROPERTIES SUFFIX ".com") +add_importlibs(tree msvcrt kernel32) +add_cd_file(TARGET tree DESTINATION reactos/system32 FOR all) Index: stdafx.h =================================================================== --- stdafx.h (revision 0) +++ stdafx.h (working copy) @@ -0,0 +1,56 @@ +/* +* PROJECT: ReactOS Kernel +* LICENSE: GNU GPLv2 only as published by the Free Software Foundation +* PURPOSE: Implements tree.com functionality similar to Windows +* PROGRAMMERS: Asif Bahrainwala (asif_bahrainwala@hotmail.com) +*/ + +// stdafx.h : include file for standard system include files, +// or project specific include files that are used frequently, but +// are changed infrequently +// + +#pragma once + +//#include "targetver.h" + +#include +#include + + + +// TODO: reference additional headers your program requires here +#include +//#include +//#include +//using namespace std; + +#define STR_MAX 2048 + +/** +* @name: GetDirectoryStructure +* +* @param strPath +* Must specify folder name +* +* @param width +* specifies drawing distance for correct formating of tree structure being drawn on console screen +* +* @param prevLine +* specifies the previous line writtn on console, is used for correct formating +* @return +* void +*/ +static void GetDirectoryStructure(wchar_t* strPath,UINT width/*used internally for adding spaces*/,wchar_t *prevLine/*used internally for formating reasons*/); + +/** +* @name: HasSubFolder +* +* @param strPath +* Must specify folder name +* +* @return +* true if folder has sub folders, else will return false +*/ +static BOOL HasSubFolder(const wchar_t *strPath1); + Index: tree.c =================================================================== --- tree.c (revision 0) +++ tree.c (working copy) @@ -0,0 +1,300 @@ +/* +* PROJECT: ReactOS Kernel +* LICENSE: GNU GPLv2 only as published by the Free Software Foundation +* PURPOSE: Implements tree.com functionality similar to Windows +* PROGRAMMERS: Asif Bahrainwala (asif_bahrainwala@hotmail.com) +*/ + +// Tree.cpp : Defines the entry point for the console application. +// +#include "stdafx.h" + + +const char* HELP = "\nGraphically displays the folder structure of a drive or path. \n\nTREE [drive:][path] [/F] [/A]\n\n /F Display the names of the files in each folder.\n\n\n"; +const char *INVALID ="No subfolders exist"; + +BOOL bShowFiles=FALSE; //if this flag is set to true, files will also be listed + +/** +* @name: HasSubFolder +* +* @param strPath +* Must specify folder name +* +* @return +* true if folder has sub folders, else will return false +*/ +static BOOL HasSubFolder(const wchar_t *strPath1) +{ + BOOL ret=FALSE; + WIN32_FIND_DATA FindFileData; + HANDLE hFind = NULL; + static wchar_t strPath[STR_MAX]=L""; + ZeroMemory(strPath,sizeof(strPath)); + + wcscat(strPath,strPath1); + wcscat(strPath,L"\\*."); + + hFind=FindFirstFile(strPath, &FindFileData); + do + { + if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + { + if(wcscmp(FindFileData.cFileName,L".")==0 || + wcscmp(FindFileData.cFileName,L"..")==0 ){ + continue; + } + + ret=TRUE; //found subfolder + break; + } + } + while(FindNextFile(hFind,&FindFileData)); + + FindClose(hFind); + return ret; +} + +/** +* @name: DrawTree +* +* @param strPath +* Must specify folder name +* +* @param arrFolder +* must be a list of folder names to be drawn in tree format +* +* @param width +* specifies drawing distance for correct formating of tree structure being drawn on console screen +* +* @return +* void +*/ +static void DrawTree(const wchar_t* strPath,const WIN32_FIND_DATA *arrFolder,const size_t szArr,UINT width /*used internally for adding spaces*/,const wchar_t *prevLine/*used internally for formating reasons*/,BOOL drawfolder) +{ + BOOL bHasSubFolder=HasSubFolder(strPath); + UINT i=0; + + for(i=0;i arrFolder; //will fill up with names of all sub folders + WIN32_FIND_DATA *arrFolder=0;UINT arrFoldersz=0; + //vector arrFile; //will fill up with names of all sub folders + WIN32_FIND_DATA *arrFile=0;UINT arrFilesz=0; + + ZeroMemory(&FindFileData,sizeof(FindFileData)); + + { + static wchar_t tmp[STR_MAX]=L""; + ZeroMemory(tmp,sizeof(tmp)); + wcscat(tmp,strPath); + wcscat(tmp,L"\\*.*"); + + hFind=FindFirstFile(tmp, &FindFileData); + + err=GetLastError(); + } + + + if(hFind==INVALID_HANDLE_VALUE) + return; + + do + { + if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + { + if(wcscmp(FindFileData.cFileName,L".")==0 || + wcscmp(FindFileData.cFileName,L"..")==0 ) + continue; + + ++arrFoldersz; + arrFolder=(WIN32_FIND_DATA*)realloc(arrFolder,arrFoldersz*sizeof(FindFileData)); + + if(arrFolder==NULL) + exit(-1); + + arrFolder[arrFoldersz-1]=FindFileData; + + } + else + { + ++arrFilesz; + arrFile=(WIN32_FIND_DATA*)realloc(arrFile,arrFilesz*sizeof(FindFileData)); + + if(arrFile==NULL) + exit(-1); + + arrFile[arrFilesz-1]=FindFileData; + } + } + while(FindNextFile(hFind,&FindFileData)); + + FindClose(hFind); + + if(bShowFiles){ + DrawTree(strPath,arrFile,arrFilesz,width,prevLine,FALSE); //will free(arrFile) + } + + DrawTree(strPath,arrFolder,arrFoldersz,width,prevLine,TRUE); //will free(arrFile) + + free(arrFolder); + free(arrFile); +} + +/** +* @name: main +* standard main functionality as required by C/C++ for application startup +* +* @return +* error /success value +*/ +int _tmain(int argc, _TCHAR* argv[]) +{ + //printf("Tree command by Asif Bahrainwala (asif_bahrainwala@hotmail.com), for ReactOS\n"); + DWORD dwSerial=0; + wchar_t t=0; + wchar_t *strPath=NULL; + DWORD sz=0; + wchar_t *context=NULL ; + wchar_t *driveLetter=NULL; + + int i;for(i=1;i>16,dwSerial&0xffff); + + sz=GetCurrentDirectory(1,&t); //get the buffer size + strPath=(wchar_t*)malloc(sizeof(wchar_t)*sz); //must not return before calling delete[] + + GetCurrentDirectory(sz,strPath); //get the current directory + + + driveLetter=(wchar_t*)malloc(sizeof(wchar_t)*sz); //get the drive letter , must not return before calling delete[] + + wcscpy_s(driveLetter,sz,strPath); + wcstok_s(driveLetter,L":",&context); //parse for the drive letter + + + wprintf(L"%s:.\n",driveLetter); + + free( driveLetter); + + + //int testcase=0; + //for(testcase=0;testcase<1000;testcase++) //this is for testing only, to determine resource leak + GetDirectoryStructure(strPath,1,L" "); //get the sub directories within this current folder + + free(strPath); + wprintf(L"\n"); + + return 0; +} +