1999-03-20 20:28:42 +00:00
|
|
|
/*
|
|
|
|
* 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 <ekohl@abo.rhein-zeiung.de>)
|
|
|
|
* Fixed command line parsing bugs.
|
|
|
|
*
|
|
|
|
* 21-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeiung.de>)
|
|
|
|
* Started major rewrite using a new structure.
|
|
|
|
*
|
1999-03-30 22:06:36 +00:00
|
|
|
* 03-Feb-1999 (Eric Kohl <ekohl@abo.rhein-zeiung.de>)
|
1999-03-20 20:28:42 +00:00
|
|
|
* First working version.
|
1999-03-30 22:06:36 +00:00
|
|
|
*
|
|
|
|
* 30-Mar-1999 (Eric Kohl <ekohl@abo.rhein-zeiung.de>)
|
|
|
|
* Added quiet ("/Q"), wipe ("/W") and zap ("/Z") option.
|
1999-11-06 00:53:33 +00:00
|
|
|
*
|
|
|
|
* 06-Nov-1999 (Eric Kohl <ekohl@abo.rhein-zeiung.de>)
|
|
|
|
* Little fix to keep DEL quiet inside batch files.
|
1999-03-20 20:28:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#ifdef INCLUDE_CMD_DEL
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <tchar.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
1999-04-14 23:48:27 +00:00
|
|
|
#include <ctype.h>
|
1999-03-20 20:28:42 +00:00
|
|
|
|
|
|
|
#include "cmd.h"
|
1999-11-06 00:53:33 +00:00
|
|
|
#include "batch.h"
|
1999-03-20 20:28:42 +00:00
|
|
|
|
|
|
|
|
1999-03-30 22:06:36 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
DEL_ATTRIBUTES = 0x001, /* /A : not implemented */
|
|
|
|
DEL_ERROR = 0x002, /* /E : not implemented */
|
|
|
|
DEL_NOTHING = 0x004, /* /N */
|
|
|
|
DEL_PROMPT = 0x008, /* /P : not implemented */
|
|
|
|
DEL_QUIET = 0x010, /* /Q */
|
|
|
|
DEL_SUBDIR = 0x020, /* /S : not implemented */
|
|
|
|
DEL_TOTAL = 0x040, /* /T */
|
|
|
|
DEL_WIPE = 0x080, /* /W */
|
|
|
|
DEL_EMPTYDIR = 0x100, /* /X : not implemented */
|
|
|
|
DEL_YES = 0x200, /* /Y : not implemented */
|
|
|
|
DEL_ZAP = 0x400 /* /Z */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static BOOL
|
|
|
|
RemoveFile (LPTSTR lpFileName, DWORD dwFlags)
|
|
|
|
{
|
|
|
|
if (dwFlags & DEL_WIPE)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* FIXME: Wipe the given file */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return DeleteFile (lpFileName);
|
|
|
|
}
|
|
|
|
|
1999-03-20 20:28:42 +00:00
|
|
|
|
1999-10-23 18:17:37 +00:00
|
|
|
INT CommandDelete (LPTSTR cmd, LPTSTR param)
|
1999-03-20 20:28:42 +00:00
|
|
|
{
|
1999-10-23 18:17:37 +00:00
|
|
|
TCHAR szFullPath[MAX_PATH];
|
|
|
|
LPTSTR pFilePart;
|
1999-03-20 20:28:42 +00:00
|
|
|
LPTSTR *arg = NULL;
|
|
|
|
INT args;
|
|
|
|
INT i;
|
1999-03-30 22:06:36 +00:00
|
|
|
INT nEvalArgs = 0; /* nunber of evaluated arguments */
|
|
|
|
DWORD dwFlags = 0;
|
|
|
|
DWORD dwFiles = 0;
|
1999-03-20 20:28:42 +00:00
|
|
|
|
|
|
|
HANDLE hFile;
|
|
|
|
WIN32_FIND_DATA f;
|
|
|
|
|
|
|
|
if (!_tcsncmp (param, _T("/?"), 2))
|
|
|
|
{
|
|
|
|
ConOutPuts (_T("Deletes one or more files.\n"
|
1999-10-23 18:17:37 +00:00
|
|
|
"\n"
|
|
|
|
"DEL [/N /P /T /Q /W /Z] file ...\n"
|
|
|
|
"DELETE [/N /P /T /Q /W /Z] file ...\n"
|
|
|
|
"ERASE [/N /P /T /Q /W /Z] file ...\n"
|
|
|
|
"\n"
|
|
|
|
" file Specifies the file(s) to delete.\n"
|
|
|
|
"\n"
|
|
|
|
" /N Nothing.\n"
|
|
|
|
" /P Prompts for confirmation before deleting each file.\n"
|
|
|
|
" (Not implemented yet!)\n"
|
|
|
|
" /T Display total number of deleted files and freed disk space.\n"
|
|
|
|
" /Q Quiet.\n"
|
|
|
|
" /W Wipe. Overwrite the file with zeros before deleting it.\n"
|
|
|
|
" /Z Zap (delete hidden, read-only and system files).\n"));
|
1999-03-20 20:28:42 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
arg = split (param, &args);
|
|
|
|
|
|
|
|
if (args > 0)
|
|
|
|
{
|
|
|
|
/* check for options anywhere in command line */
|
|
|
|
for (i = 0; i < args; i++)
|
|
|
|
{
|
|
|
|
if (*arg[i] == _T('/'))
|
|
|
|
{
|
|
|
|
if (_tcslen (arg[i]) >= 2)
|
|
|
|
{
|
|
|
|
switch (_totupper (arg[i][1]))
|
|
|
|
{
|
|
|
|
case _T('N'):
|
1999-03-30 22:06:36 +00:00
|
|
|
dwFlags |= DEL_NOTHING;
|
1999-03-20 20:28:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case _T('P'):
|
1999-03-30 22:06:36 +00:00
|
|
|
dwFlags |= DEL_PROMPT;
|
1999-03-20 20:28:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case _T('Q'):
|
1999-03-30 22:06:36 +00:00
|
|
|
dwFlags |= DEL_QUIET;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case _T('S'):
|
|
|
|
dwFlags |= DEL_SUBDIR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case _T('T'):
|
|
|
|
dwFlags |= DEL_TOTAL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case _T('W'):
|
|
|
|
dwFlags |= DEL_WIPE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case _T('Z'):
|
|
|
|
dwFlags |= DEL_ZAP;
|
1999-03-20 20:28:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
nEvalArgs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* there are only options on the command line --> error!!! */
|
|
|
|
if (args == nEvalArgs)
|
|
|
|
{
|
|
|
|
error_req_param_missing ();
|
|
|
|
freep (arg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1999-11-06 00:53:33 +00:00
|
|
|
/* keep quiet within batch files */
|
|
|
|
if (bc != NULL)
|
|
|
|
dwFlags |= DEL_QUIET;
|
|
|
|
|
1999-03-20 20:28:42 +00:00
|
|
|
/* check for filenames anywhere in command line */
|
|
|
|
for (i = 0; i < args; i++)
|
|
|
|
{
|
|
|
|
if (!_tcscmp (arg[i], _T("*")) ||
|
1999-12-15 00:50:41 +00:00
|
|
|
!_tcscmp (arg[i], _T("*.*")))
|
1999-03-20 20:28:42 +00:00
|
|
|
{
|
1999-12-15 00:50:41 +00:00
|
|
|
INT res;
|
1999-03-20 20:28:42 +00:00
|
|
|
|
1999-12-15 00:50:41 +00:00
|
|
|
res = FilePromptYN (_T("All files in directory will be deleted!\n"
|
|
|
|
"Are you sure (Y/N)?"));
|
|
|
|
if ((res == PROMPT_NO) ||
|
|
|
|
(res == PROMPT_BREAK))
|
|
|
|
break;
|
1999-03-20 20:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*arg[i] != _T('/'))
|
|
|
|
{
|
|
|
|
#ifdef _DEBUG
|
1999-03-30 22:06:36 +00:00
|
|
|
ConErrPrintf (_T("File: %s\n"), arg[i]);
|
1999-03-20 20:28:42 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (_tcschr (arg[i], _T('*')) || _tcschr (arg[i], _T('?')))
|
|
|
|
{
|
|
|
|
/* wildcards in filespec */
|
|
|
|
#ifdef _DEBUG
|
1999-03-30 22:06:36 +00:00
|
|
|
ConErrPrintf (_T("Wildcards!\n\n"));
|
1999-03-20 20:28:42 +00:00
|
|
|
#endif
|
|
|
|
|
1999-10-23 18:17:37 +00:00
|
|
|
GetFullPathName (arg[i],
|
|
|
|
MAX_PATH,
|
|
|
|
szFullPath,
|
|
|
|
&pFilePart);
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
ConErrPrintf (_T("Full path: %s\n"), szFullPath);
|
|
|
|
ConErrPrintf (_T("File part: %s\n"), pFilePart);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
hFile = FindFirstFile (szFullPath, &f);
|
1999-03-20 20:28:42 +00:00
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
error_file_not_found ();
|
|
|
|
freep (arg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
1999-03-30 22:06:36 +00:00
|
|
|
/* ignore "." and ".." */
|
1999-03-20 20:28:42 +00:00
|
|
|
if (!_tcscmp (f.cFileName, _T(".")) ||
|
|
|
|
!_tcscmp (f.cFileName, _T("..")))
|
|
|
|
continue;
|
|
|
|
|
1999-10-23 18:17:37 +00:00
|
|
|
_tcscpy (pFilePart, f.cFileName);
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
ConErrPrintf (_T("Full filename: %s\n"), szFullPath);
|
|
|
|
#endif
|
|
|
|
|
1999-03-30 22:06:36 +00:00
|
|
|
if (!(dwFlags & DEL_QUIET) && !(dwFlags & DEL_TOTAL))
|
1999-10-23 18:17:37 +00:00
|
|
|
ConErrPrintf (_T("Deleting: %s\n"), szFullPath);
|
1999-03-20 20:28:42 +00:00
|
|
|
|
1999-03-30 22:06:36 +00:00
|
|
|
/* delete the file */
|
|
|
|
if (!(dwFlags & DEL_NOTHING))
|
1999-03-20 20:28:42 +00:00
|
|
|
{
|
1999-10-23 18:17:37 +00:00
|
|
|
if (RemoveFile (szFullPath, dwFlags))
|
1999-03-30 22:06:36 +00:00
|
|
|
{
|
|
|
|
dwFiles++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (dwFlags & DEL_ZAP)
|
|
|
|
{
|
1999-10-23 18:17:37 +00:00
|
|
|
if (SetFileAttributes (szFullPath, 0))
|
1999-03-30 22:06:36 +00:00
|
|
|
{
|
1999-10-23 18:17:37 +00:00
|
|
|
if (RemoveFile (szFullPath, dwFlags))
|
1999-03-30 22:06:36 +00:00
|
|
|
{
|
|
|
|
dwFiles++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ErrorMessage (GetLastError(), _T(""));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ErrorMessage (GetLastError(), _T(""));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ErrorMessage (GetLastError(), _T(""));
|
|
|
|
}
|
|
|
|
}
|
1999-03-20 20:28:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (FindNextFile (hFile, &f));
|
|
|
|
FindClose (hFile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* no wildcards in filespec */
|
|
|
|
#ifdef _DEBUG
|
1999-03-30 22:06:36 +00:00
|
|
|
ConErrPrintf (_T("No Wildcards!\n"));
|
1999-03-20 20:28:42 +00:00
|
|
|
#endif
|
1999-10-23 18:17:37 +00:00
|
|
|
GetFullPathName (arg[i],
|
|
|
|
MAX_PATH,
|
|
|
|
szFullPath,
|
|
|
|
&pFilePart);
|
|
|
|
#ifdef _DEBUG
|
|
|
|
ConErrPrintf (_T("Full path: %s\n"), szFullPath);
|
|
|
|
#endif
|
1999-03-30 22:06:36 +00:00
|
|
|
if (!(dwFlags & DEL_QUIET) && !(dwFlags & DEL_TOTAL))
|
1999-10-23 18:17:37 +00:00
|
|
|
ConOutPrintf (_T("Deleting %s\n"), szFullPath);
|
1999-03-30 22:06:36 +00:00
|
|
|
|
|
|
|
if (!(dwFlags & DEL_NOTHING))
|
1999-03-20 20:28:42 +00:00
|
|
|
{
|
1999-10-23 18:17:37 +00:00
|
|
|
if (RemoveFile (szFullPath, dwFlags))
|
1999-03-30 22:06:36 +00:00
|
|
|
{
|
|
|
|
dwFiles++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (dwFlags & DEL_ZAP)
|
|
|
|
{
|
1999-10-23 18:17:37 +00:00
|
|
|
if (SetFileAttributes (szFullPath, 0))
|
1999-03-30 22:06:36 +00:00
|
|
|
{
|
1999-10-23 18:17:37 +00:00
|
|
|
if (RemoveFile (szFullPath, dwFlags))
|
1999-03-30 22:06:36 +00:00
|
|
|
{
|
|
|
|
dwFiles++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ErrorMessage (GetLastError(), _T(""));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ErrorMessage (GetLastError(), _T(""));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ErrorMessage (GetLastError(), _T(""));
|
|
|
|
}
|
|
|
|
}
|
1999-03-20 20:28:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* only command given */
|
|
|
|
error_req_param_missing ();
|
|
|
|
freep (arg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
freep (arg);
|
|
|
|
|
1999-03-30 22:06:36 +00:00
|
|
|
if (!(dwFlags & DEL_QUIET))
|
|
|
|
{
|
|
|
|
if (dwFiles == 0)
|
|
|
|
ConOutPrintf (_T(" 0 files deleted\n"));
|
|
|
|
else
|
|
|
|
ConOutPrintf (_T(" %lu file%s deleted\n"),
|
1999-10-23 18:17:37 +00:00
|
|
|
dwFiles,
|
1999-10-28 23:09:59 +00:00
|
|
|
(dwFiles == 1) ? "" : "s");
|
1999-03-30 22:06:36 +00:00
|
|
|
}
|
|
|
|
|
1999-03-20 20:28:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|