home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / UDEL.ZIP / UDEL.C < prev    next >
Text File  |  1988-10-21  |  10KB  |  375 lines

  1. /****************************************************************************
  2. *   udel.c  -  delete file command.                                         *
  3. *                                                                           *
  4. *                                               T. Sandberg 10/10/88        *
  5. ****************************************************************************/
  6. #define INCL_DOS
  7.  
  8. #define INCL_SUB
  9.  
  10. #include <os2.h>
  11.  
  12. #include <string.h>
  13.  
  14. #include <stdio.h>
  15.  
  16. #include <conio.h>
  17.  
  18. #include <dos.h>
  19.  
  20. #include <sfdos.h>
  21.  
  22. #include <sfscr.h>
  23.  
  24. #include <sfkb.h>
  25.  
  26. #include <sf.h>
  27.  
  28. #define TRUE 1
  29.  
  30.  
  31.  
  32. void check_parms(char *parm1,char *parm2,char *parm3,
  33.                  char *path, int *sub, int *confirm);
  34.  
  35. int check_parm_value(char *parm1,char *parm2,char *parm3,char *value);
  36.  
  37. void check_for_path(char *path,char *parm);
  38.  
  39. void delete(char *path,int sub_dir,int confirm);
  40.  
  41. int confirm_delete(char *path,int confirm);
  42.  
  43. long total_files = 0L,  total_fsize = 0L, total_fsize_a = 0L;
  44.  
  45.  
  46. int block_size;
  47.  
  48.  
  49. char BACK_SLASH = 92;
  50.  
  51.  
  52. void main(argc,argv)
  53. int argc;
  54. char *argv[];
  55. {
  56.   int return_code, x, sub_dir, confirm, percent;
  57.   unsigned long total_free, total_drive;
  58.   char vol[12], path[70], volid[14], drive, cur_drive, cur_path[70];
  59.  
  60.   char parm1[70], parm2[70], parm3[70];
  61.  
  62.   SFDIR dir_ent;
  63.   HDIR hdir = 0xffff;
  64.   FSALLOCATE disk_info;
  65.  
  66.   if (argc == 1)
  67.     {
  68.       printf("Error - File Spec required\n");
  69.       exit(0);
  70.     }
  71.  
  72.   if (argc >= 2)
  73.     strcpy(parm1,argv[1]);
  74.   else
  75.     strcpy(parm1,"\0");
  76.  
  77.   if (argc >= 3)
  78.     strcpy(parm2,argv[2]);
  79.   else
  80.     strcpy(parm2,"\0");
  81.  
  82.   if (argc >= 4)
  83.     strcpy(parm3,argv[3]);
  84.   else
  85.     strcpy(parm3,"\0");
  86.  
  87.  
  88.   check_parms(parm1,parm2,parm3,path,&sub_dir,&confirm);
  89.  
  90.   if (path[1] ==  ':')
  91.     {
  92.       drive = path[0];
  93.       x = sf_find_last_char(':',path);
  94.       sf_copy_from(x+1,path,path);
  95.     }
  96.   else drive = sfdos_get_disk();
  97.      sfdos_get_volid(drive,volid);
  98.  
  99.   strcpy(vol,volid);
  100.  
  101.   drive = tolower(drive);
  102.  
  103.  
  104.   cur_drive = sfdos_get_disk();
  105.  
  106.   if (drive != cur_drive)
  107.      sfdos_set_disk(drive);
  108.  
  109.  
  110.   if ((path[0] != BACK_SLASH) && (path[2] != BACK_SLASH))
  111.    {
  112.      sfdos_getcwd(drive,cur_path);
  113.  
  114.      if (cur_path[3] != '\\')
  115.        strcat(cur_path,"\\");
  116.      strcat(cur_path,path);
  117.      strcpy(path,cur_path);
  118.    }
  119.  
  120.    x = strlen(path);
  121.    if (path[x-1] == '\\')
  122.      strcat(path,"*.*");
  123.    else
  124.      {
  125.        if ((x = sf_find_last_char('?',path) == -1) &&
  126.           (x = sf_find_last_char('*',path) == -1))
  127.           {
  128.             x = sfdos_dir_first_entry(&hdir,path,&dir_ent,FA_DIREC);
  129.             if (x == 0)
  130.             strcat(path,"\\*.*");
  131.           }
  132.      }
  133.  
  134.   sfdos_getdfree(drive,&disk_info,&total_drive,&total_free,&percent);
  135.   block_size = disk_info.cSectorUnit * disk_info.cbSector;
  136.  
  137.  
  138.   printf("\n");
  139.  
  140.  
  141.   delete(path,sub_dir,confirm);
  142.  
  143.   if (total_files != 0)
  144.     {
  145.       printf("Total File Count................. %8ld \n",total_files);
  146.       printf("Total File Size.................. %8ld \n",total_fsize);
  147.       printf("Total File Size (Allocated)...... %8ld \n",total_fsize_a);
  148.       sfdos_getdfree(drive,&disk_info,&total_drive,&total_free,&percent);
  149.       printf("Free %lu of %lu %% %d",total_free,total_drive,percent);
  150.       printf("\n...\n");
  151.     }
  152.  
  153.   sfdos_set_disk(cur_drive);
  154.  
  155. }
  156.  
  157.  
  158.  
  159. void check_parms(char *parm1,char *parm2,char *parm3,
  160.                  char *path,int *sub,int *confirm)
  161. /******************************************************************************
  162. * check for valid parms passed in set sub directory and no confirm flags      *
  163. ******************************************************************************/
  164. {
  165.    int return_value;
  166.  
  167.    *sub = 0;
  168.    *confirm = TRUE;
  169.    strcpy(path,"\0");
  170.  
  171.  
  172.    return_value = check_parm_value(parm1,parm2,parm3,"/S");
  173.    if (return_value == TRUE)
  174.      *sub = TRUE;
  175.    return_value = check_parm_value(parm1,parm2,parm3,"/NC");
  176.    if (return_value == TRUE)
  177.      *confirm = FALSE;
  178.  
  179.   check_for_path(path,parm1);
  180.   check_for_path(path,parm2);
  181.   check_for_path(path,parm3);
  182.  
  183.   if (path[0] == 0)
  184.      {
  185.       printf("Error - File Spec required\n");
  186.       exit(0);
  187.     }
  188.  
  189. }
  190.  
  191.  
  192. int check_parm_value(char *parm1,char *parm2,char *parm3,char *value)
  193. /*****************************************************************************
  194. * check to see if any of the three parms equal value.                        *
  195. *****************************************************************************/
  196. {
  197.   if ((!strcmp(strupr(parm1),value))  || (!strcmp(strupr(parm2),value))
  198.                                       || (!strcmp(strupr(parm3),value)))
  199.     return TRUE;
  200.   else
  201.     return FALSE;
  202. }
  203.  
  204.  
  205.  
  206.  
  207. void check_for_path(char *path,char *parm)
  208. /*****************************************************************************
  209. * check parm for path parm                                                   *
  210. *****************************************************************************/
  211. {
  212.   if ((parm[0] != 0) && (parm[0] != '/'))
  213.     strcpy(path,parm);
  214. }
  215.  
  216.  
  217. void delete(char *path,int sub_dir,int confirm)
  218. /******************************************************************************
  219. * search direcories and confirm delete of file                                *
  220. ******************************************************************************/
  221. {
  222.   int return_code, del_file;
  223.   int x, y, first_time = 0;
  224.   long dir_files = 0L, dir_fsize = 0L, dir_fsize_a = 0L;
  225.  
  226.   SFDIR dir_ent;
  227.   HDIR hdir = 0xffff;
  228.  
  229.   char cur_dir[60], cur_pathdir[70], search[12];
  230.  
  231.   char cur_path[70], save[70];
  232.  
  233.   char fullpath[85];
  234.  
  235.   x = sf_find_last_char('\\',path);
  236.  
  237.   sf_copy_to(x,path,cur_pathdir);       /* find out the current path */
  238.   sf_copy_from(x+1,path,search);
  239.   strcpy(cur_path,cur_pathdir);
  240.   if (cur_path[0] != '\0') strcat(cur_path,"\\");
  241.   strcat(cur_path,"*.*");
  242.   if (cur_pathdir[0] != '\0') strcat(cur_pathdir,"\\");
  243.  
  244.   return_code = sfdos_dir_first_entry(&hdir,path,&dir_ent,FA_ALL);
  245.  
  246.   x = sf_find_last_char(BACK_SLASH,path);
  247.  
  248.   sf_copy_to(x,path,cur_pathdir);       /* find out the current path */
  249.   sf_copy_from(x+1,path,search);
  250.   strcpy(cur_path,cur_pathdir);
  251.   strcat(cur_path,"*.*");
  252.  
  253.  
  254.   while (return_code == 0)
  255.     {
  256.        if (return_code == 0)
  257.          {
  258.             if (first_time == 0)
  259.               printf("%s\n",path);
  260.  
  261.             first_time = 1;
  262.             strcpy(fullpath,cur_pathdir);
  263.             strcat(fullpath,dir_ent.fnameext);
  264.             printf("%-12s %7ld %s %s  ",dir_ent.fnameext,dir_ent.fsize,
  265.                                       dir_ent.fdate,dir_ent.ftime);
  266.             del_file = confirm_delete(fullpath,confirm);
  267.             if (del_file == TRUE)
  268.               {
  269.                 dir_fsize = dir_fsize + dir_ent.fsize;
  270.                 dir_fsize_a = dir_fsize_a + dir_ent.fsize;
  271.                 x = dir_ent.fsize % block_size;
  272.                 dir_files++;
  273.                 if (x != 0)
  274.                 dir_fsize_a = dir_fsize_a + block_size - x;
  275.              }
  276.          }
  277.  
  278.        return_code = sfdos_dir_next_entry(hdir,&dir_ent,FA_ALL);
  279.  
  280.       del_file = FALSE;
  281.     }
  282.  
  283.   total_files = total_files + dir_files;
  284.   total_fsize = total_fsize + dir_fsize;
  285.   total_fsize_a = total_fsize_a + dir_fsize_a;
  286.  
  287.   strcpy(save,cur_pathdir);
  288.  
  289.   if (first_time == 1)
  290.     printf("\n");
  291.  
  292.   if (sub_dir)
  293.     {
  294.       hdir = 0xffff;
  295.       return_code = sfdos_dir_first_entry(&hdir,cur_path,&dir_ent,FA_DIREC);
  296.       if (dir_ent.fnameext[0] == '\0') return_code = -1;
  297.  
  298.       do {
  299.            if (dir_ent.fnameext[0] == '.')
  300.              {
  301.                return_code = sfdos_dir_next_entry(hdir,&dir_ent,FA_DIREC);
  302.                return_code = sfdos_dir_next_entry(hdir,&dir_ent,FA_DIREC);
  303.              }
  304.            if (return_code  == 0)
  305.              {
  306.                strcpy(cur_pathdir,save);
  307.                strcat(cur_pathdir,dir_ent.fnameext);
  308.                strcat(cur_pathdir,"\\");
  309.                strcat(cur_pathdir,search);
  310.                delete(cur_pathdir,sub_dir,confirm);
  311.                return_code = sfdos_dir_next_entry(hdir,&dir_ent,FA_DIREC);
  312.              }
  313.          }
  314.        while (return_code == 0);
  315.      }
  316. }
  317.  
  318.  
  319.  
  320. int confirm_delete(char *fullpath,int confirm)
  321. /******************************************************************************
  322. * confirm delete with Y/y or none with N/n or q or escape to quit.            *
  323. ******************************************************************************/
  324. {
  325.   char vchar[9];
  326.   int doit, x, y, rc;
  327.  
  328.   strcpy(vchar,"yYnN qQ");       /*  accepte 'y' 'Y' 'N' 'n' ' ' 'Q' or 'q' */
  329.   vchar[7] = 27;                 /* escape key  */
  330.   vchar[8] = '\0';
  331.  
  332.  
  333.   sfscr_get_cursor_loc(&x,&y);
  334.  
  335.   if (confirm == TRUE)
  336.     {
  337.       printf("DELETE file ?");
  338.       sfkb_flush();
  339.       sfkb_getch_vas(&doit,K_WAIT,vchar);
  340.     }
  341.  
  342.   sfscr_set_cursor_loc(x,y);
  343.  
  344.   if ((doit == 'y') || (doit == 'Y') || (confirm == FALSE))
  345.     {
  346.       rc = sfdos_delete_file(fullpath);
  347.       if (rc == 0)
  348.         printf("FILE DELETED    \n");
  349.       else
  350.         {
  351.           if (rc == -2)
  352.             printf("FILE PROTECTED - NOT DELETED\n");
  353.           else
  354.             printf("UNKONWN REASON - NOT DELETED\n");
  355.  
  356.           return FALSE;
  357.         }
  358.       return TRUE;
  359.  
  360.     }
  361.  
  362.   printf("                      \n");
  363.  
  364.   if ((doit == 27 ) || (doit == 'q') || (doit == 'Q'))
  365.     {
  366.       printf("\nUDEL - Canceled");
  367.       exit(0);
  368.     }
  369.  
  370.   return FALSE;
  371.  
  372. }
  373.  
  374.  
  375.