home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / UTILITY / DIR / TOADADEL.ZIP / AGEDEL.C next >
Encoding:
C/C++ Source or Header  |  1991-05-25  |  4.9 KB  |  230 lines

  1. /* AGEDEL aged file delete utility
  2.  * By request of SEMPER BBS (whose SYSOP wanted to delete all files
  3.  * older than 6 months or whatever in his download directory).
  4.  * Cribbed from an old MicroCornucopia file find utility.
  5.  * Hacked unmercifully, and given to the public domain.
  6.  * David Kirschbaum
  7.  * Toad Hall
  8.  * kirsch@usasoc.soc.mil
  9.  */
  10.  
  11. #include <dir.h>
  12. #include <dos.h>
  13. #include <stdlib.h>        /* atoi() */
  14. #include <string.h>
  15. #include <stdio.h>        /* printf() */
  16. #include "filesrch.h"
  17.  
  18.  
  19. /* global data */
  20.  
  21. /* global variables */
  22.     FILE_DATA ourfd;
  23.     char drive[3] = "";        /* sizes are from dir.h */
  24.     char dir[66] = "";
  25.     char name[9] = "";
  26.     char ext[5] = "";
  27.     unsigned int targetdate;
  28.     int counter = 0;
  29.  
  30.     int Test = 0;            /* -t flag */
  31.     int Visual = 0;            /* -v flag */
  32.  
  33. char Usage[] =
  34. "AGEDEL aged file delete utility\n"
  35. "Usage:  agedel [-v][-t] n [d:][\path\]filename.typ [...]\n"
  36. "-v - visual    - display progress to stdout\n"
  37. "-t - test      - list files that would have been deleted without deleting\n"
  38. "n              - target file age in months,\n"
  39. "filename.typ   - target filename(s).\n"
  40. "Wildcards and/or multiple filenames are permitted.\n"
  41. "\nWARNING!  This is a potentially disastrous utility!\n"
  42. "'agedel 1 *.*' will delete EVERYTHING in the current directory\n"
  43. "older than 1 month!\n"
  44. "Be ABSOLUTELY sure you know what you're doing when using this!\n";
  45.  
  46. static union  REGS  regs;
  47. static struct SREGS sregs;
  48.  
  49. static unsigned old_dta_seg;
  50. static unsigned old_dta_off;
  51.  
  52. /* local function prototypes */
  53.  
  54. static void set_dta(void * new_dta);
  55. static void reset_dta(void);
  56. unsigned int today(void);
  57.  
  58.  
  59. int find_first(char * spec, char attrib, FILE_DATA * fd)
  60. {
  61.     unsigned res;
  62.  
  63.     set_dta(fd);
  64.  
  65.     regs.h.ah = 0x4E;
  66.     regs.x.cx = (unsigned)attrib;
  67.  
  68.     regs.x.dx = (unsigned)spec;
  69.     intdos(®s,®s);
  70.  
  71.     res = regs.x.cflag;
  72.  
  73.     reset_dta();
  74.  
  75.     return res;
  76. }
  77.  
  78. int find_next(FILE_DATA * fd)
  79. {
  80.     unsigned res;
  81.  
  82.     set_dta(fd);
  83.  
  84.     regs.h.ah = 0x4F;
  85.     intdos(®s,®s);
  86.  
  87.     res = regs.x.cflag;
  88.  
  89.     reset_dta();
  90.  
  91.     return res;
  92. }
  93.  
  94. static void set_dta(void * new_dta)
  95. {
  96.     regs.h.ah = 0x2F;
  97.     intdosx(®s,®s,&sregs);
  98.  
  99.     old_dta_seg = sregs.es;
  100.     old_dta_off = regs.x.bx;
  101.  
  102.     regs.h.ah = 0x1A;
  103.  
  104.     regs.x.dx = (unsigned)(new_dta);
  105.     intdos(®s,®s);
  106. }
  107.  
  108. static void reset_dta(void)
  109. {
  110.     segread(&sregs);
  111.  
  112.     regs.h.ah = 0x1A;
  113.     sregs.ds  = old_dta_seg;
  114.     regs.x.dx = old_dta_off;
  115.  
  116.     intdosx(®s,®s,&sregs);
  117. }
  118.  
  119. unsigned int abs_dta_date(int fd_date)
  120. /* compute an absolute day-based date from a DTA-type date integer.
  121.  * ourfd.date looks like this:
  122.  * bits 0-4  day
  123.  * bits 5-8  month
  124.  * bits 9-15 year
  125.  */
  126. {
  127.     int year,month;    /* ,day; */
  128.  
  129.     year  = ((int)(fd_date >>9) & 0x7f);
  130.     month = ((int)(fd_date >>5) & 0xf);
  131. /*
  132.     day   = (int)(fd_date & 0x1f);
  133. */
  134.     return( (year*12) + month);
  135. }
  136.  
  137.  
  138. unsigned int today(void)
  139. /* compute an absolute day-based date from a today's date struct.
  140.  * the struct date, used by getdate, is like this:
  141.  * struct date {
  142.  *   int da_year;
  143.  *   char da_day;
  144.  *   char da_mon;
  145.  * };
  146. */
  147. {
  148.     struct date date;
  149.  
  150.     getdate(&date);
  151.     date.da_year -= 1980;        /* move down to a decent range */
  152.     return( (date.da_year * 12) + date.da_mon);
  153. }
  154.  
  155.  
  156. void delete(int flags)
  157. /* flags are from a fnsplit() */
  158. {
  159.     char s[128] = "";
  160.  
  161.     if( (!strcmp(ourfd.name,".")            /* no subdirectory stuff */
  162.       || !strcmp(ourfd.name,"..")) )
  163.         return;
  164.  
  165.     if(flags & DRIVE)
  166.         strcat(s,drive);        /* "C:" */
  167.     if(flags & DIRECTORY)
  168.         strcat(s,dir);            /* "/FOO/" */
  169.     strcat(s,ourfd.name);        /* finally the expanded name */
  170.  
  171. /* Test this file's date against our target date.
  172.  * Delete if older.
  173.  */
  174.     if(s[0])                /* be sure we have a name */
  175.     if (abs_dta_date(ourfd.date) < targetdate){
  176.         if(Visual)
  177.             printf("Deleting [%s]\n",s);
  178.         if(!Test)
  179.             unlink(s);
  180.         counter++;
  181.     }
  182. }
  183.  
  184.  
  185. void main(int ArgC,char **ArgV)
  186. {
  187.     int i,res;
  188.     int n=0;
  189.  
  190.     for(i=1;i<ArgC;i++)        /* check all args */
  191.     if(ArgV[i][0]=='-')        /* if an option ... */
  192.     switch(ArgV[i][1]){        /* check which */
  193.         case 0:                /* ignore null switches */
  194.             break;
  195.         case 't':
  196.             Test++;
  197.             break;
  198.         case 'v':
  199.             Visual++;
  200.             break;
  201.         default:
  202.             fprintf(stderr,"AGEDEL: unknown option %s\n",ArgV[i]);
  203.     }
  204.     else
  205.         ArgV[++n]=ArgV[i];        /* pack list */
  206.  
  207.     if( (n < 2)                    /* need at least 2 (age & filename) */
  208.      || (res = atoi(ArgV[1])) < 1)    /* and age had better be valid! */
  209.         printf(Usage);                /* help the dummy */
  210.     else {
  211.  
  212.     targetdate = today() - res;        /* age we'll look for */
  213.     if(Visual)
  214.         printf("AGEDEL: deleting all files older than %u months:\n",res);
  215.  
  216.     for(i=1;i<=n;i++) {                /* switches have been gobbled */
  217.         if (!find_first(ArgV[i],ATTR_ALL,&ourfd)){
  218.             res = fnsplit(ArgV[i],drive,dir,name,ext);
  219.             delete(res);
  220.             if (res & WILDCARDS) {    /* only find_next if wild cards */
  221.                 while(!find_next(&ourfd))
  222.                     delete(res);
  223.             }
  224.         }
  225.     }
  226.     if(Visual)
  227.         printf("%u files deleted\n",counter);
  228.     }
  229. }
  230.