home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************/
- /* */
- /* del.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$ */
-
- /*
- typedef struct _WIN32_FIND_DATA { // wfd
- DWORD dwFileAttributes;
- FILETIME ftCreationTime;
- FILETIME ftLastAccessTime;
- FILETIME ftLastWriteTime;
- DWORD nFileSizeHigh;
- DWORD nFileSizeLow;
- DWORD dwReserved0;
- DWORD dwReserved1;
- TCHAR cFileName[ MAX_PATH ];
- TCHAR cAlternateFileName[ 14 ];
- } WIN32_FIND_DATA;
- */
-
- #include <windows.h>
- #include <string.h>
- #include "globals.h"
- #include "proto.h"
-
- #ifdef VERSION_STRINGS
- static BYTE *RcsId = "$Header$";
- #endif
-
- /* Local convienence define */
- #define FileDelete(file) (DeleteFile((LPCTSTR)file))
-
-
- BOOL del(INT argc, char *argv[])
- {
- WIN32_FIND_DATA dmp;
- HANDLE hDir;
- BYTE szPattern[MAX_CMDLINE] = "";
- BYTE szPath[MAX_CMDLINE] = "";
- BYTE szDrive[3];
- BYTE *pszSep;
- BOOL pflag = FALSE;
- BOOL deleted = FALSE;
- INT nPatLen;
- DWORD nRead;
-
- /* Make sure the user gave us a delete specification */
- if((argc < 2) || (argc > 3))
- {
- error_message(INV_NUM_PARAMS);
- return FALSE;
- }
-
- scan_name(argv[1], szDrive, szPath, szPattern);
-
- if(strcmp(szPattern,"*.*") == 0)
- {
- BYTE line[MAX_CMDLINE] = "", *resp;
-
- FOREVER
- {
- printf("All files in directory will be deleted!\nAre you sure (Y/N)?");
- ReadFile(hStdin, (LPVOID)line, (DWORD)1, &nRead, 0);
-
- resp = skipwh(line);
- if(*resp == 'n' || *resp == 'N')
- {
- return TRUE;
- }
- else if(*resp == 'y' || *resp == 'Y')
- break;
- else
- continue;
- }
- }
-
- /* Look for the first (and maybe only) occurrance of the user */
- /* specified szPattern. */
- if((hDir = FindFirstFile((LPCTSTR)argv[1], (LPWIN32_FIND_DATA)&dmp))
- == INVALID_HANDLE_VALUE)
- {
- error_mess_str = szPattern;
- error_message(FILE_NOT_FOUND);
- return TRUE;
- }
-
- /* Now follow standard DOS convention and loop through the */
- /* directory, expanding wild cards */
- do
- {
- BYTE szFileName[MAX_CMDLINE];
-
- /* Assemble a file name for each file found */
- *szFileName = '\0';
- if(*szDrive)
- {
- strcpy(szFileName, szDrive);
- strcat(szFileName, ":");
- }
- if(*szPath)
- {
- strcat(szFileName, szPath);
- strcat(szFileName, "\\");
- }
- strcat(szFileName, dmp.cFileName);
-
- /* Delete each file, and prompt if /p option */
- /* was issued. */
- if(pflag)
- {
- BYTE line[MAX_CMDLINE] = "", *resp;
-
- FOREVER
- {
- printf("Delete %s (Y/N)?", szFileName);
- ReadFile(hStdin, (LPVOID)line, (DWORD)1, &nRead, 0);
-
- resp = skipwh(line);
- if(*resp == 'n' || *resp == 'N')
- break;
- else if(*resp == 'y' || *resp == 'Y')
- {
- if(!FileDelete(szFileName))
- error_message(ACCESS_DENIED);
- break;
- }
- else
- continue;
- }
- }
- else
- /* Just delete each file found. */
- {
- if(FileDelete(szFileName))
- ++deleted;
- }
- }
- while(FindNextFile (hDir, (LPWIN32_FIND_DATA)&dmp));
-
- /* Here's an oddball. If we found a file, but could not delete */
- /* it, we need to warn the user. So here it is. */
- if(!pflag && deleted < 1)
- error_message(ACCESS_DENIED);
-
- CloseHandle(hDir);
- }
-
-