home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
UDEL.ZIP
/
UDEL.C
< prev
next >
Wrap
Text File
|
1988-10-21
|
10KB
|
375 lines
/****************************************************************************
* udel.c - delete file command. *
* *
* T. Sandberg 10/10/88 *
****************************************************************************/
#define INCL_DOS
#define INCL_SUB
#include <os2.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <sfdos.h>
#include <sfscr.h>
#include <sfkb.h>
#include <sf.h>
#define TRUE 1
void check_parms(char *parm1,char *parm2,char *parm3,
char *path, int *sub, int *confirm);
int check_parm_value(char *parm1,char *parm2,char *parm3,char *value);
void check_for_path(char *path,char *parm);
void delete(char *path,int sub_dir,int confirm);
int confirm_delete(char *path,int confirm);
long total_files = 0L, total_fsize = 0L, total_fsize_a = 0L;
int block_size;
char BACK_SLASH = 92;
void main(argc,argv)
int argc;
char *argv[];
{
int return_code, x, sub_dir, confirm, percent;
unsigned long total_free, total_drive;
char vol[12], path[70], volid[14], drive, cur_drive, cur_path[70];
char parm1[70], parm2[70], parm3[70];
SFDIR dir_ent;
HDIR hdir = 0xffff;
FSALLOCATE disk_info;
if (argc == 1)
{
printf("Error - File Spec required\n");
exit(0);
}
if (argc >= 2)
strcpy(parm1,argv[1]);
else
strcpy(parm1,"\0");
if (argc >= 3)
strcpy(parm2,argv[2]);
else
strcpy(parm2,"\0");
if (argc >= 4)
strcpy(parm3,argv[3]);
else
strcpy(parm3,"\0");
check_parms(parm1,parm2,parm3,path,&sub_dir,&confirm);
if (path[1] == ':')
{
drive = path[0];
x = sf_find_last_char(':',path);
sf_copy_from(x+1,path,path);
}
else drive = sfdos_get_disk();
sfdos_get_volid(drive,volid);
strcpy(vol,volid);
drive = tolower(drive);
cur_drive = sfdos_get_disk();
if (drive != cur_drive)
sfdos_set_disk(drive);
if ((path[0] != BACK_SLASH) && (path[2] != BACK_SLASH))
{
sfdos_getcwd(drive,cur_path);
if (cur_path[3] != '\\')
strcat(cur_path,"\\");
strcat(cur_path,path);
strcpy(path,cur_path);
}
x = strlen(path);
if (path[x-1] == '\\')
strcat(path,"*.*");
else
{
if ((x = sf_find_last_char('?',path) == -1) &&
(x = sf_find_last_char('*',path) == -1))
{
x = sfdos_dir_first_entry(&hdir,path,&dir_ent,FA_DIREC);
if (x == 0)
strcat(path,"\\*.*");
}
}
sfdos_getdfree(drive,&disk_info,&total_drive,&total_free,&percent);
block_size = disk_info.cSectorUnit * disk_info.cbSector;
printf("\n");
delete(path,sub_dir,confirm);
if (total_files != 0)
{
printf("Total File Count................. %8ld \n",total_files);
printf("Total File Size.................. %8ld \n",total_fsize);
printf("Total File Size (Allocated)...... %8ld \n",total_fsize_a);
sfdos_getdfree(drive,&disk_info,&total_drive,&total_free,&percent);
printf("Free %lu of %lu %% %d",total_free,total_drive,percent);
printf("\n...\n");
}
sfdos_set_disk(cur_drive);
}
void check_parms(char *parm1,char *parm2,char *parm3,
char *path,int *sub,int *confirm)
/******************************************************************************
* check for valid parms passed in set sub directory and no confirm flags *
******************************************************************************/
{
int return_value;
*sub = 0;
*confirm = TRUE;
strcpy(path,"\0");
return_value = check_parm_value(parm1,parm2,parm3,"/S");
if (return_value == TRUE)
*sub = TRUE;
return_value = check_parm_value(parm1,parm2,parm3,"/NC");
if (return_value == TRUE)
*confirm = FALSE;
check_for_path(path,parm1);
check_for_path(path,parm2);
check_for_path(path,parm3);
if (path[0] == 0)
{
printf("Error - File Spec required\n");
exit(0);
}
}
int check_parm_value(char *parm1,char *parm2,char *parm3,char *value)
/*****************************************************************************
* check to see if any of the three parms equal value. *
*****************************************************************************/
{
if ((!strcmp(strupr(parm1),value)) || (!strcmp(strupr(parm2),value))
|| (!strcmp(strupr(parm3),value)))
return TRUE;
else
return FALSE;
}
void check_for_path(char *path,char *parm)
/*****************************************************************************
* check parm for path parm *
*****************************************************************************/
{
if ((parm[0] != 0) && (parm[0] != '/'))
strcpy(path,parm);
}
void delete(char *path,int sub_dir,int confirm)
/******************************************************************************
* search direcories and confirm delete of file *
******************************************************************************/
{
int return_code, del_file;
int x, y, first_time = 0;
long dir_files = 0L, dir_fsize = 0L, dir_fsize_a = 0L;
SFDIR dir_ent;
HDIR hdir = 0xffff;
char cur_dir[60], cur_pathdir[70], search[12];
char cur_path[70], save[70];
char fullpath[85];
x = sf_find_last_char('\\',path);
sf_copy_to(x,path,cur_pathdir); /* find out the current path */
sf_copy_from(x+1,path,search);
strcpy(cur_path,cur_pathdir);
if (cur_path[0] != '\0') strcat(cur_path,"\\");
strcat(cur_path,"*.*");
if (cur_pathdir[0] != '\0') strcat(cur_pathdir,"\\");
return_code = sfdos_dir_first_entry(&hdir,path,&dir_ent,FA_ALL);
x = sf_find_last_char(BACK_SLASH,path);
sf_copy_to(x,path,cur_pathdir); /* find out the current path */
sf_copy_from(x+1,path,search);
strcpy(cur_path,cur_pathdir);
strcat(cur_path,"*.*");
while (return_code == 0)
{
if (return_code == 0)
{
if (first_time == 0)
printf("%s\n",path);
first_time = 1;
strcpy(fullpath,cur_pathdir);
strcat(fullpath,dir_ent.fnameext);
printf("%-12s %7ld %s %s ",dir_ent.fnameext,dir_ent.fsize,
dir_ent.fdate,dir_ent.ftime);
del_file = confirm_delete(fullpath,confirm);
if (del_file == TRUE)
{
dir_fsize = dir_fsize + dir_ent.fsize;
dir_fsize_a = dir_fsize_a + dir_ent.fsize;
x = dir_ent.fsize % block_size;
dir_files++;
if (x != 0)
dir_fsize_a = dir_fsize_a + block_size - x;
}
}
return_code = sfdos_dir_next_entry(hdir,&dir_ent,FA_ALL);
del_file = FALSE;
}
total_files = total_files + dir_files;
total_fsize = total_fsize + dir_fsize;
total_fsize_a = total_fsize_a + dir_fsize_a;
strcpy(save,cur_pathdir);
if (first_time == 1)
printf("\n");
if (sub_dir)
{
hdir = 0xffff;
return_code = sfdos_dir_first_entry(&hdir,cur_path,&dir_ent,FA_DIREC);
if (dir_ent.fnameext[0] == '\0') return_code = -1;
do {
if (dir_ent.fnameext[0] == '.')
{
return_code = sfdos_dir_next_entry(hdir,&dir_ent,FA_DIREC);
return_code = sfdos_dir_next_entry(hdir,&dir_ent,FA_DIREC);
}
if (return_code == 0)
{
strcpy(cur_pathdir,save);
strcat(cur_pathdir,dir_ent.fnameext);
strcat(cur_pathdir,"\\");
strcat(cur_pathdir,search);
delete(cur_pathdir,sub_dir,confirm);
return_code = sfdos_dir_next_entry(hdir,&dir_ent,FA_DIREC);
}
}
while (return_code == 0);
}
}
int confirm_delete(char *fullpath,int confirm)
/******************************************************************************
* confirm delete with Y/y or none with N/n or q or escape to quit. *
******************************************************************************/
{
char vchar[9];
int doit, x, y, rc;
strcpy(vchar,"yYnN qQ"); /* accepte 'y' 'Y' 'N' 'n' ' ' 'Q' or 'q' */
vchar[7] = 27; /* escape key */
vchar[8] = '\0';
sfscr_get_cursor_loc(&x,&y);
if (confirm == TRUE)
{
printf("DELETE file ?");
sfkb_flush();
sfkb_getch_vas(&doit,K_WAIT,vchar);
}
sfscr_set_cursor_loc(x,y);
if ((doit == 'y') || (doit == 'Y') || (confirm == FALSE))
{
rc = sfdos_delete_file(fullpath);
if (rc == 0)
printf("FILE DELETED \n");
else
{
if (rc == -2)
printf("FILE PROTECTED - NOT DELETED\n");
else
printf("UNKONWN REASON - NOT DELETED\n");
return FALSE;
}
return TRUE;
}
printf(" \n");
if ((doit == 27 ) || (doit == 'q') || (doit == 'Q'))
{
printf("\nUDEL - Canceled");
exit(0);
}
return FALSE;
}