home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************/
- /* */
- /* dir.c */
- /* */
- /* Copyright (c) 2000 */
- /* Pasquale J. Villani */
- /* All Rights Reserved */
- /* */
- /* This file is part of CMD32. */
- /* */
- /* CMD32 is free software; you can redistribute it and/or */
- /* modify it under the terms of the GNU General Public License */
- /* as published by the Free Software Foundation; either version */
- /* 2, or (at your option) any later version. */
- /* */
- /* CMD32 is distributed in the hope that it will be useful, but */
- /* WITHOUT ANY WARRANTY; without even the implied warranty of */
- /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */
- /* the GNU General Public License for more details. */
- /* */
- /* You should have received a copy of the GNU General Public */
- /* License along with CMD32; see the file COPYING. If not, */
- /* write to the Free Software Foundation, 675 Mass Ave, */
- /* Cambridge, MA 02139, USA. */
- /****************************************************************/
-
-
- /* $Logfile$ */
-
- /* $Log$
- * $EndLog$ */
-
- #include <windows.h>
- #include <string.h>
- #include <ctype.h>
- #include "globals.h"
- #include "proto.h"
-
- #ifdef VERSION_STRINGS
- static BYTE *RcsId = "$Header$";
- #endif
-
-
- #define TM_HOUR(x) (((x)&0xf800)>>11)
- #define TM_MIN(x) (((x)&0x07e0)>>5)
- #define DT_DAY(x) ((x)&0x001f)
- #define DT_MONTH(x) (((x)&0x01e0)>>5)
- #define DT_YEAR(x) (((x)&0xfe00)>>9)
-
-
- static INT chk_line(INT);
-
-
-
- BOOL dir(INT argc, BYTE *argv[])
- {
- WIN32_FIND_DATA dmp;
- HANDLE hDir;
- WORD nFileCount = 0, nLineCount, nColumns = 0;
- DWORD nTotal = 0l;
- BYTE szVolumeID[MAX_CMDLINE], *ext;
- BYTE cudir[MAX_CMDLINE];
- DWORD nFreeClusters, nBytesPerSector, nSectorsPerCluster, nTotalClusters;
- BOOL wflag, pflag, lflag, bflag;
- BYTE szFilePattern[MAX_CMDLINE] = "", szFormatted[MAX_CMDLINE];
- BYTE szPath[MAX_CMDLINE] = "";
- BYTE szRoot[MAX_CMDLINE] = "";
- BYTE szDrive[3] = "";
- BYTE szPattern[MAX_PATH];
- DWORD nMaximumComponentLength;
- DWORD nFileSystemFlags;
-
- /* parse for options */
- wflag = pflag = lflag = bflag = FALSE;
- nFileCount = 0;
-
- switch(argc)
- {
- BYTE *argp;
-
- case 3:
- argp = argv[2];
- goto process;
- /* fall into pattern processing */
-
- case 2:
- argp = argv[1];
-
- process:
- while(*argp && (argp[0] == '/'))
- {
- switch(argp[1])
- {
- case 'p':
- pflag = TRUE;
- break;
-
- case 'l':
- lflag = TRUE;
- break;
-
- case 'b':
- bflag = TRUE;
- break;
-
- case 'w':
- wflag = TRUE;
- break;
- }
- argp++;
- }
- if(argv[1][0] != '/')
- {
- scan_name(argv[1], szDrive, szPath, szFilePattern);
- }
- break;
-
- case 1:
- strcpy(szFilePattern, "*.*");
- break;
-
- default:
- error_message(INV_NUM_PARAMS);
- return FALSE;
- }
-
- /* Set defaults for file name and path if not supplied. */
- if(*szDrive == '\0')
- {
- szDrive[0] = 'A' + default_drive;
- szDrive[1] = '\0';
- }
- if(islower(*szDrive))
- {
- *szDrive = toupper(*szDrive);
- }
-
- if(*szPath == '\0')
- {
- if(szDrive[0] != ('A' + default_drive))
- {
- strcpy(szPath, "\\");
- }
- else
- {
- GetCurrentDirectory(MAX_CMDLINE, szPath);
- if(*szDrive == '\0')
- {
- szDrive[0] = szPath[0];
- szDrive[1] = '\0';
- }
- memmove(szPath, &szPath[2], sizeof(szPath) - 2);
- }
- }
- if(*szFilePattern == '\0')
- strcpy(szFilePattern,"*.*");
- strcpy(szRoot, szDrive);
- strcat(szRoot, ":\\");
-
- /* Get the volume label */
- if(!GetVolumeInformation((LPCTSTR)szRoot, (LPTSTR)szVolumeID,
- (DWORD)MAX_CMDLINE, NULL,
- &nMaximumComponentLength, &nFileSystemFlags,
- 0, 0))
- {
- if(ERROR_NO_VOLUME_LABEL == GetLastError())
- strcpy(szVolumeID, "has no label");
- else
- {
- error_message(PATH_NOT_FOUND);
- return FALSE;
- }
- }
- if(*szVolumeID == '\0')
- strcpy(szVolumeID, "has no label");
-
-
- sprintf(szPattern, (szPath[0] == '\\' && !szPath[1]) ? "%s:%s%s" : "%s:%s\\%s",
- szDrive, szPath, szFilePattern);
- if((hDir = FindFirstFile((LPCTSTR)szPattern, (LPWIN32_FIND_DATA)&dmp))
- == INVALID_HANDLE_VALUE)
- {
- error_message(FILE_NOT_FOUND);
- return FALSE;
- }
- /* If the user specified a directory, he/she wants the contents of the */
- /* directory. Adjust everything to make this happen. */
- if((dmp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (strcmp(szFilePattern, "*.*")))
- {
- if((szPath[0] == '\\' && szPath[1]) || (szPath[0] == '.'))
- {
- strcat(szPath, "\\");
- }
- strcat(szPath, szFilePattern);
- strcpy(szFilePattern, "*.*");
- CloseHandle(hDir);
- sprintf(szPattern, (szPath[0] == '\\' && !szPath[1]) ? "%s:%s%s" : "%s:%s\\%s",
- szDrive, szPath, szFilePattern);
- if((hDir = FindFirstFile((LPCTSTR)szPattern, (LPWIN32_FIND_DATA)&dmp))
- == INVALID_HANDLE_VALUE)
- {
- error_message(FILE_NOT_FOUND);
- return FALSE;
- }
- }
-
- /* Display the header, if not turned off */
- if(bflag)
- nLineCount = 0;
- else
- nLineCount = 4;
- if(!bflag)
- {
- printf("\n Volume in drive %s %s\n", szDrive, szVolumeID);
- printf(" Directory of %s:%s\n\n", szDrive, szPath);
- }
-
- if(wflag)
- {
- BYTE szFileName[NAMEMAX+1];
-
- do
- {
- char *p;
-
- strcpy(szFileName,
- (*dmp.cAlternateFileName) ? dmp.cAlternateFileName : dmp.cFileName);
-
- if(dmp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- {
- if(bflag && *szFileName == '.')
- continue;
- sprintf(szFormatted, "[%s]", szFileName);
- if(lflag)
- strlwr(szFormatted);
- printf("%-14s ", szFormatted);
- ++nColumns;
- if(nColumns == 5)
- {
- if(pflag)
- nLineCount = chk_line(nLineCount);
- printf("\n");
- nColumns = 0;
- }
- }
- else
- {
- ext = " ";
- sprintf(szFormatted, "%s", szFileName);
- if(lflag)
- strlwr(szFormatted);
- printf("%-15s",szFormatted);
- ++nColumns;
- if(nColumns == 5)
- {
- if(pflag)
- nLineCount = chk_line(nLineCount);
- printf("\n");
- nColumns = 0;
- }
- }
- ++nFileCount;
- nTotal += dmp.nFileSizeLow;
- }
- while(FindNextFile((HANDLE)hDir, (LPWIN32_FIND_DATA)&dmp));
- }
- else
- {
- FILETIME LocalFileTime;
- WORD nFatDate, nFatTime;
- BYTE szFileName[NAMEMAX+1];
-
- do
- {
- WORD hour;
-
- strcpy(szFileName,
- (*dmp.cAlternateFileName) ? dmp.cAlternateFileName : dmp.cFileName);
- FileTimeToLocalFileTime(&dmp.ftCreationTime,
- &LocalFileTime);
- FileTimeToDosDateTime(&LocalFileTime,
- &nFatDate, &nFatTime);
- hour = TM_HOUR(nFatTime);
-
- if(szFileName[0] == '.')
- ext = "";
- else
- for(ext = szFileName; *ext != '\0'; ext++)
- {
- if(*ext == '.')
- {
- *ext++ = '\0';
- break;
- }
- }
- if(lflag)
- {
- strlwr(szFileName);
- strlwr(ext);
- }
- if(dmp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- {
- if(bflag && *szFileName == '.')
- continue;
- if(pflag)
- nLineCount = chk_line(nLineCount);
- printf(bflag ? "\n%s.%s" : "\n %8s %3s <DIR> %2d-%02d-%02d %2d:%02d%s %s",
- szFileName, ext,
- DT_MONTH(nFatDate),
- DT_DAY(nFatDate),
- (DT_YEAR(nFatDate) + 1980) % 100,
- hour > 12 ? hour - 12 : (hour == 0) ? 12 : hour,
- TM_MIN(nFatTime),
- hour >= 12 ? "p" : "a",
- dmp.cFileName);
- }
- else
- {
- if(pflag)
- nLineCount = chk_line(nLineCount);
- printf(bflag ? "\n%s.%s" : "\n %8s %3s %10ld %2d-%02d-%02d %2d:%02d%s %s",
- szFileName, ext, dmp.nFileSizeLow,
- DT_MONTH(nFatDate),
- DT_DAY(nFatDate),
- (DT_YEAR(nFatDate) + 1980) % 100,
- hour > 12 ? hour - 12 : (hour == 0) ? 12 : hour,
- TM_MIN(nFatTime),
- hour >= 12 ? "p" : "a",
- dmp.cFileName);
- }
- ++nFileCount;
- nTotal += dmp.nFileSizeLow;
- }
- while(FindNextFile((HANDLE)hDir, (LPWIN32_FIND_DATA)&dmp));
- }
-
- /* /b does not print any statistics */
- if(bflag)
- {
- printf("\n\n");
- }
- else
- /* Now print the available free bytes (It's really clusters */
- /* translated to bytes. */
- {
- GetDiskFreeSpace((LPCTSTR)szRoot,
- (LPDWORD)&nSectorsPerCluster,
- (LPDWORD)&nBytesPerSector,
- (LPDWORD) &nFreeClusters,
- (LPDWORD)&nTotalClusters);
- printf("\n\n %10d file(s) %10ld bytes\n", nFileCount, nTotal);
- printf( " %10lu bytes free\n\n",
- (DWORD)nFreeClusters
- * (DWORD)nBytesPerSector
- * (DWORD)nSectorsPerCluster);
- }
-
- FindClose(hDir);
- return TRUE;
- }
-
- static INT chk_line(INT nLineCount)
- {
- BYTE szLine[MAX_CMDLINE];
-
- if(nLineCount == 23)
- {
- DWORD nRead;
-
- printf("\nStrike a key when ready . . .");
- ReadFile(hStdin, (LPVOID)szLine, (DWORD)1, &nRead, 0);
- return 0;
- }
- return ++nLineCount;
- }
-
-
-
-
-