⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 del.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
*  DEL.C - del internal command.
*
*
*  History:
*
*    06/29/98 (Rob Lake rlake@cs.mun.ca)
*        rewrote del to support wildcards
*        added my name to the contributors
*
*    07/13/98 (Rob Lake)
*        fixed bug that caused del not to delete file with out
*        attribute. moved set, del, ren, and ver to there own files
*
*    27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
*        added config.h include
*
*    09-Dec-1998 (Eric Kohl)
*        Fixed command line parsing bugs.
*
*    21-Jan-1999 (Eric Kohl)
*        Started major rewrite using a new structure.
*
*    03-Feb-1999 (Eric Kohl)
*        First working version.
*
*    30-Mar-1999 (Eric Kohl)
*        Added quiet ("/Q"), wipe ("/W") and zap ("/Z") option.
*
*    06-Nov-1999 (Eric Kohl)
*        Little fix to keep DEL quiet inside batch files.
*
*    28-Jan-2004 (Michael Fritscher <michael@fritscher.net>)
*        Added prompt ("/P"), yes ("/Y") and wipe("/W") option.
*
*    22-Jun-2005 (Brandon Turner <turnerb7@msu.edu>)
*        Added exclusive deletion "del * -abc.txt -text*.txt"
*
*    22-Jun-2005 (Brandon Turner <turnerb7@msu.edu>)
*        Implemented /A   example "del /A:H /A:-R *.exe -ping.exe"
*    
*    07-Aug-2005
*        Removed the exclusive deletion (see two comments above) because '-' is a valid file name character.
*        Optimized the recursive deletion in directories.
*        Preload some nice strings.
*/

#include <precomp.h>

#ifdef INCLUDE_CMD_DEL


enum
{
	DEL_ATTRIBUTES = 0x001,   /* /A */
	DEL_NOTHING    = 0x004,   /* /N */
	DEL_PROMPT     = 0x008,   /* /P */
	DEL_QUIET      = 0x010,   /* /Q */
	DEL_SUBDIR     = 0x020,   /* /S */
	DEL_TOTAL      = 0x040,   /* /T */
	DEL_WIPE       = 0x080,   /* /W */
	DEL_EMPTYDIR   = 0x100,   /* /X : not implemented */
	DEL_YES        = 0x200,   /* /Y */
	DEL_FORCE      = 0x800    /* /F */
};

enum
{
	ATTR_ARCHIVE     = 0x001,   /* /A:A */
	ATTR_HIDDEN      = 0x002,   /* /A:H */
	ATTR_SYSTEM      = 0x004,   /* /A:S */
	ATTR_READ_ONLY   = 0x008,   /* /A:R */
	ATTR_N_ARCHIVE   = 0x010,   /* /A:-A */
	ATTR_N_HIDDEN    = 0x020,   /* /A:-H */
	ATTR_N_SYSTEM    = 0x040,   /* /A:-S */
	ATTR_N_READ_ONLY = 0x080    /* /A:-R */
};

static TCHAR szDeleteWipe[RC_STRING_MAX_SIZE];
static TCHAR szDelHelp2[RC_STRING_MAX_SIZE];
static TCHAR szDelHelp3[RC_STRING_MAX_SIZE];
static TCHAR szDelHelp4[RC_STRING_MAX_SIZE];
static TCHAR szDelError5[RC_STRING_MAX_SIZE];
static TCHAR szDelError6[RC_STRING_MAX_SIZE];
static TCHAR szDelError7[RC_STRING_MAX_SIZE];
static TCHAR CMDPath[MAX_PATH];

static BOOLEAN StringsLoaded = FALSE;

static VOID LoadStrings(VOID)
{
        LoadString( CMD_ModuleHandle, STRING_DELETE_WIPE, szDeleteWipe, RC_STRING_MAX_SIZE);
        LoadString( CMD_ModuleHandle, STRING_DEL_HELP2, szDelHelp2, RC_STRING_MAX_SIZE);
        LoadString( CMD_ModuleHandle, STRING_DEL_HELP3, szDelHelp3, RC_STRING_MAX_SIZE);
        LoadString( CMD_ModuleHandle, STRING_DEL_HELP4, szDelHelp4, RC_STRING_MAX_SIZE);
        LoadString( CMD_ModuleHandle, STRING_DEL_ERROR5, szDelError5, RC_STRING_MAX_SIZE);
        LoadString( CMD_ModuleHandle, STRING_DEL_ERROR6, szDelError6, RC_STRING_MAX_SIZE);
        LoadString( CMD_ModuleHandle, STRING_DEL_ERROR7, szDelError7, RC_STRING_MAX_SIZE);
        GetModuleFileName(NULL, CMDPath, MAX_PATH);
        StringsLoaded = TRUE;
}

static BOOL
RemoveFile (LPTSTR lpFileName, DWORD dwFlags, WIN32_FIND_DATA* f)
{
	/*This function is called by CommandDelete and
	does the actual process of deleting the single 
	file*/
		if(CheckCtrlBreak(BREAK_INPUT))
			return 1;

        /*check to see if it is read only and if this is done based on /A
          if it is done by file name, access is denied. However, if it is done 
          using the /A switch you must un-read only the file and allow it to be
          deleted*/
        if((dwFlags & DEL_ATTRIBUTES) || (dwFlags & DEL_FORCE))
        {
                if(f->dwFileAttributes & FILE_ATTRIBUTE_READONLY)
		{
			/*setting file to normal, not saving old attrs first
			  because the file is going to be deleted anyways
			  so the only thing that matters is that it isnt
			  read only.*/
                        SetFileAttributes(lpFileName,FILE_ATTRIBUTE_NORMAL);
                }
        }

        if (dwFlags & DEL_WIPE)
        {

	        HANDLE file;
	        DWORD temp;
#define BufferSize 65536
	        BYTE buffer[BufferSize];
	        LONGLONG i;
	        LARGE_INTEGER FileSize;

	        FileSize.u.HighPart = f->nFileSizeHigh;
                FileSize.u.LowPart = f->nFileSizeLow;

	        for(i = 0; i < BufferSize; i++)
	        {
		        buffer[i]=rand() % 256;
	        }
	        file = CreateFile (lpFileName, GENERIC_WRITE, 0, NULL, OPEN_EXISTING,  FILE_FLAG_WRITE_THROUGH, NULL);
                if (file != INVALID_HANDLE_VALUE)
                {
                        for(i = 0; i < (FileSize.QuadPart - BufferSize); i += BufferSize)
		        {
			        WriteFile (file, buffer, BufferSize, &temp, NULL);
			        ConOutPrintf (_T("%I64d%% %s\r"),(i * (LONGLONG)100)/FileSize.QuadPart,szDeleteWipe);
		        }
		        WriteFile (file, buffer, (DWORD)(FileSize.QuadPart - i), &temp, NULL);
		        ConOutPrintf (_T("100%% %s\n"),szDeleteWipe);
		        CloseHandle (file);
                }
        }

	return DeleteFile (lpFileName);
}


static DWORD
DeleteFiles(LPTSTR FileName, DWORD* dwFlags, DWORD dwAttrFlags)
{
        TCHAR szFullPath[MAX_PATH];
        TCHAR szFileName[MAX_PATH];
        LPTSTR pFilePart;
        HANDLE hFile;
        WIN32_FIND_DATA f;
        BOOL bExclusion;
        INT res;
        DWORD dwFiles = 0;

        _tcscpy(szFileName, FileName);

        if(_tcschr (szFileName, _T('*')) == NULL && 
	   IsExistingDirectory (szFileName))
        {
	        /* If it doesnt have a \ at the end already then on needs to be added */
		if(szFileName[_tcslen(szFileName) -  1] != _T('\\'))
                        _tcscat (szFileName, _T("\\"));
                /* Add a wildcard after the \ */
                _tcscat (szFileName, _T("*"));
        }

	if(!_tcscmp (szFileName, _T("*")) ||
           !_tcscmp (szFileName, _T("*.*")) ||
           (szFileName[_tcslen(szFileName) -  2] == _T('\\') && szFileName[_tcslen(szFileName) -  1] == _T('*')))
        {
                /* well, the user wants to delete everything but if they didnt yes DEL_YES, DEL_QUIET, or DEL_PROMPT
	           then we are going to want to make sure that in fact they want to do that.  */

		if (!((*dwFlags & DEL_YES) || (*dwFlags & DEL_QUIET) || (*dwFlags & DEL_PROMPT)))
	        {
        	        res = FilePromptYNA (szDelHelp2);
		        if ((res == PROMPT_NO) || (res == PROMPT_BREAK))
			        return 0x80000000;
		        if(res == PROMPT_ALL)
			        *dwFlags |= DEL_YES;
	        }
	}

        GetFullPathName (szFileName,
                         MAX_PATH,
                         szFullPath,
                         &pFilePart);

        hFile = FindFirstFile(szFullPath, &f);
        if (hFile != INVALID_HANDLE_VALUE)
        {
                do
                {
                        bExclusion = FALSE;

		        /*if it is going to be excluded by - no need to check attrs*/
		        if(*dwFlags & DEL_ATTRIBUTES && !bExclusion)
		        {

			        /*save if file attr check if user doesnt care about that attr anyways*/
			        if(dwAttrFlags & ATTR_ARCHIVE && !(f.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE))
				        bExclusion = TRUE;
			        if(dwAttrFlags & ATTR_HIDDEN && !(f.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
				        bExclusion = TRUE;
			        if(dwAttrFlags & ATTR_SYSTEM && !(f.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM))
				        bExclusion = TRUE;
			        if(dwAttrFlags & ATTR_READ_ONLY && !(f.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
			                bExclusion = TRUE;
		                if(dwAttrFlags & ATTR_N_ARCHIVE && (f.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE))
			                bExclusion = TRUE;
		                if(dwAttrFlags & ATTR_N_HIDDEN && (f.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
			                bExclusion = TRUE;
		                if(dwAttrFlags & ATTR_N_SYSTEM && (f.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM))
			                bExclusion = TRUE;
		                if(dwAttrFlags & ATTR_N_READ_ONLY && (f.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
			                bExclusion = TRUE;
	                }

	                if(bExclusion)
		                continue;

		        /* ignore directories */
		        if (f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		                continue;


		        _tcscpy (pFilePart, f.cFileName);

		        /* We cant delete ourselves */
		        if(!_tcscmp (CMDPath,szFullPath))
			        continue;


#ifdef _DEBUG
		        ConErrPrintf(_T("Full filename: %s\n"), szFullPath);
#endif

		        /* ask for deleting */
		        if (*dwFlags & DEL_PROMPT)
		        {
		                ConErrPrintf(szDelError5, szFullPath);

		                res = FilePromptYN (szDelError6);

		                if ((res == PROMPT_NO) || (res == PROMPT_BREAK))
		                {
								 nErrorLevel = 0;
			                continue;
		                }
	                }

	                /*user cant ask it to be quiet and tell you what it did*/
	                if (!(*dwFlags & DEL_QUIET) && !(*dwFlags & DEL_TOTAL))
	                {
		                ConErrPrintf(szDelError7, szFullPath);
	                }

	                /* delete the file */
	                if(*dwFlags & DEL_NOTHING)
		                continue;

	                if(RemoveFile (szFullPath, *dwFlags, &f))
		                dwFiles++;
		        else
                        {		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -