home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cenvi23.zip
/
DELTREE.CMM
< prev
next >
Wrap
Text File
|
1996-02-01
|
5KB
|
191 lines
/*
* deltree.cmm
*
* CEnvi shell command 'deltree'. Kills an entire directory.
*/
#include "netware.lib"
#include "filename.lib"
usage()
{
printf("Use the DELTREE command to delete a directory and all of its contents.\n");
printf("Syntax:\n");
printf(" DELTREE [drive:][path]filename [/Y]\n");
printf("where:\n");
printf(" drive:/path/filename Specifies the directory you want to delete.\n");
printf(" /Y Suppresses prompting to confirm you want to\n");
printf(" delete the subdirectory.\n");
printf("\nNote: Use DELTREE cautiously. Every file and subdirectory within the\n");
printf("specified directory will be deleted.\n");
exit(EXIT_FAILURE);
}
DeleteDirectory(pDirSpec)
{
lReg.ah = 0x3A;
if !defined(_DOS32_)
lReg.ds = segment(pDirSpec), lReg.dx = offset(pDirSpec);
else
lReg.dx = pointer(pDirSpec);
return interrupt(0x21,lReg);
}
myrmdir(dir)
{
if( defined(_NWNLM_) ) return rmdir(dir);
if( defined(_OS2_) )
{
#define ORD_DOS32DELETEDIR 226
return DynamicLink("DOSCALLS",ORD_DOS32DELETEDIR,BIT32,CDECL,dir);
}
if( defined(_NTCON_) || defined(_NTWIN_) || defined(_95CON_) ||
defined(_95WIN_) )
{
return !DynamicLink("KERNEL32","RemoveDirectoryA",STDCALL,dir);
}
if( defined(_DOS_) || defined(_WINDOWS_) || defined(_DOS32_) )
{
return !DeleteDirectory(dir);
}
printf("Don't know how to remove a dir in this version of CEnvi.\n");
return 1;
}
unprotect(file)
{
if( defined(_NWNLM_) )
{
temp[0] = DOSTimeFromCalendar(time());
code = NLMLink("SetFileInfo",
file.name,
0x06,
file.attrib & ~(_A_RDONLY|_A_SYSTEM|_A_HIDDEN),
tempcreate,
tempaccess,
tempdate,
tempbackup,
file.uid);
if( code )
printf("\nError setting attributes for file %s\n",file.name);
return;
}
if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) )
{
SetFileAttributes(file.name,file.attrib & ~_A_RDONLY);
return;
}
if( defined(_NTCON_) || defined(_NTWIN_) || defined(_95CON_) ||
defined(_95WIN_) )
{
DynamicLink("KERNEL32","SetFileAttributesA",STDCALL,file.name,
file.attrib & ~_A_RDONLY);
return;
}
if( defined(_OS2_) )
{
}
}
/* ---------------------------------------------------------------------- */
dir = "";
are_you_sure = TRUE;
parse_command_line(argc,argv)
{
for( i=1;i<argc;i++ )
{
if( argv[i][0]=='-' || argv[i][0]=='/' )
{
switch( toupper(argv[i][1]) )
{
default: usage();
case 'Y': are_you_sure = FALSE; break;
}
} else {
if( dir[0]=='\0' )
dir = argv[i];
else
{
printf("You may not specify multiple directories.\n");
exit(EXIT_FAILURE);
}
}
}
}
/* ---------------------------------------------------------------------- */
/*
* Remove the contents of a directory, then remove it
*/
kill_directory(dirname)
{
strcpy(killdir,dirname);
if( defined(_NWNLM_) )
strcat(killdir,"/*.*");
else
strcat(killdir,"\\*.*");
to_kill = Directory(killdir,FALSE,0xff);
for( i=0;to_kill && i<=GetArraySpan(to_kill);i++ )
{
if( to_kill[i].attrib & _A_SUBDIR )
{
kill_directory(to_kill[i].name);
} else {
if( remove(to_kill[i].name) )
{
unprotect(to_kill[i]);
if( remove(FullPath(to_kill[i].name)) )
printf("Unable to delete file \"%s\".\n",FullPath(to_kill[i].name));
}
}
}
if( myrmdir(dirname) )
{
printf("Unable to remove directory \"%s\".\n",dirname);
}
}
/* --------------------------------------------------------------------------- */
main(argc,argv)
{
// First thing to do is parse the command line.
parse_command_line(argc,argv);
s = strlen(dir);
if( !strcmp(dir+s-1,filename_separator) ) dir[s-1] = '\0';
to_kill = Directory(dir);
if( to_kill==NULL )
{
printf("No such file or directory \"%s\".\n",dir);
exit(EXIT_FAILURE);
}
if( GetArraySpan(to_kill)>0 )
{
printf("You must specify a single directory.\n");
exit(EXIT_FAILURE);
}
if( (to_kill[0].attrib & _A_SUBDIR)==0 )
{
printf("You may only DELTREE a directory.\n");
exit(EXIT_FAILURE);
}
if( are_you_sure )
{
printf("Delete \"%s\" and all its contents; Are you sure? (Y/N) ",dir);
ch = gets();
if( toupper(ch[0])!='Y' ) exit(EXIT_FAILURE);
}
kill_directory(dir);
}