home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / utility / misc / toolmng.lzh / ToolManager / Source / DeleteTool.c < prev    next >
C/C++ Source or Header  |  1991-10-10  |  6KB  |  253 lines

  1. /*
  2.  * DeleteTool.c   V1.0
  3.  *
  4.  * Trashcan tool for ToolManager
  5.  *
  6.  * (c) 1991 by Stefan Becker
  7.  *
  8.  */
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <workbench/icon.h>
  13. #include <workbench/startup.h>
  14. #include <dos/exall.h>
  15. #include <clib/dos_protos.h>
  16. #include <clib/exec_protos.h>
  17. #include <clib/icon_protos.h>
  18. #include <clib/intuition_protos.h>
  19.  
  20. /* Global data */
  21. char Version[]="$VER: DeleteTool 1.0 (10.10.1991)";
  22. char About[]="DeleteTool V1.0 (10.10.1991)\n⌐ 1991 Stefan Becker\n"\
  23.              "This program is freely distributable\n"\
  24.              "It eats all files you drop on it!";
  25. struct Library *IconBase;
  26. char Template[]="QUIET/S,NAMES/M";
  27. struct {
  28.         ULONG quiet;
  29.         char  **names;
  30.        } def={FALSE,NULL};
  31. struct EasyStruct SafetyES={sizeof(struct EasyStruct),0,"DeleteTool",
  32.                             "Warning: You cannot get\nback what you delete!",
  33.                             "Ok|Cancel"};
  34. struct EasyStruct AboutES={sizeof(struct EasyStruct),0,"About",About,"OK"};
  35.  
  36. /* Recursive directory scan */
  37. static void ScanDirectory(BPTR dl)
  38. {
  39.  struct ExAllControl *eac;
  40.  struct ExAllData *eadata;
  41.  BPTR ofl;
  42.  BOOL more;
  43.  
  44.  /* Get memory for ExAllControl */
  45.  if (!(eac=AllocDosObject(DOS_EXALLCONTROL,NULL))) goto sde1;
  46.  
  47.  /* Get memory for ExAllControl */
  48.  if (!(eadata=malloc(10*sizeof(struct ExAllData)))) goto sde2;
  49.  
  50.  /* Initialize ExAllControl */
  51.  eac->eac_LastKey=0;
  52.  eac->eac_MatchString=NULL; /* Delete all files except "*.info" */
  53.  eac->eac_MatchFunc=NULL;
  54.  
  55.  /* Scan directory */
  56.  ofl=CurrentDir(dl);
  57.  do
  58.   {
  59.    register struct ExAllData *ead;
  60.  
  61.    /* Get directory entries */
  62.    more=ExAll(dl,eadata,10*sizeof(struct ExAllData),ED_TYPE,eac);
  63.  
  64.    /* Check result */
  65.    if (!more && (IoErr()!=ERROR_NO_MORE_ENTRIES)) /* Error occured */
  66.     break;
  67.  
  68.    /* Finished? */
  69.    if (eac->eac_Entries==0)
  70.     continue;
  71.  
  72.    /* Examine results */
  73.    ead=eadata;
  74.    do
  75.     {
  76.      LONG len;
  77.  
  78.      /* Directory? */
  79.      if (ead->ed_Type>=0)
  80.       {
  81.        BPTR fl=Lock(ead->ed_Name,SHARED_LOCK);
  82.  
  83.        /* Recursion */
  84.        if (fl)
  85.         {
  86.          ScanDirectory(fl);
  87.          UnLock(fl);
  88.         }
  89.       }
  90.  
  91.      /* File or icon? */
  92.      len=strlen(ead->ed_Name);
  93.      if ((len>4) && !stricmp(ead->ed_Name+len-5,".info"))
  94.       {
  95.        /* Icon */
  96.        SetProtection(ead->ed_Name,0);
  97.        ead->ed_Name[len-5]='\0';      /* remove the ".info" */
  98.        DeleteDiskObject(ead->ed_Name);
  99.       }
  100.      else
  101.       {
  102.        /* File */
  103.        char *cp;
  104.  
  105.        SetProtection(ead->ed_Name,0);
  106.        if (DeleteFile(ead->ed_Name) && (cp=malloc(len+6)))
  107.         {
  108.          BPTR fl;
  109.  
  110.          /* Create icon name */
  111.          strcpy(cp,ead->ed_Name);
  112.          strcat(cp,".info");
  113.  
  114.          /* No icon? Create it! */
  115.          if (fl=Lock(cp,SHARED_LOCK))
  116.           UnLock(fl);
  117.          else
  118.           {
  119.            BPTR fh=Open(cp,MODE_NEWFILE);
  120.            if (fh) Close(fh);
  121.           }
  122.  
  123.          /* Delete icon */
  124.          SetProtection(cp,0);
  125.          DeleteDiskObject(ead->ed_Name);
  126.          free(cp);
  127.         }
  128.       }
  129.  
  130.      /* Next entry */
  131.      ead=ead->ed_Next;
  132.     }
  133.    while (ead);
  134.   }
  135.  while (more);
  136.  
  137.  CurrentDir(ofl);
  138.  
  139.  /* Free all resources */
  140.  free(eadata);
  141. sde2: FreeDosObject(DOS_EXALLCONTROL,eac);
  142. sde1: return;
  143. }
  144.  
  145. /* Requester function */
  146. BOOL PopUpRequester(struct EasyStruct *es)
  147. {
  148.  struct Screen *pubsc;
  149.  struct Window *w=NULL;
  150.  BOOL rc;
  151.  
  152.  /* Get Window pointer */
  153.  if (pubsc=LockPubScreen("Workbench"))
  154.   w=pubsc->FirstWindow;
  155.  
  156.  /* Pop up requester */
  157.  rc=EasyRequest(w,es,NULL,"");
  158.  
  159.  /* Unlock screen */
  160.  if (pubsc) UnlockPubScreen(NULL,pubsc);
  161.  
  162.  return(rc);
  163. }
  164.  
  165. /* CLI entry point */
  166. void main(int argc, char **argv)
  167. {
  168.  struct RDArgs *rda;
  169.  struct FileInfoBlock *fib;
  170.  char **name;
  171.  
  172.  /* Open icon.library V37: DeleteDiskObject() is new for V37!!! */
  173.  if (!(IconBase=OpenLibrary(ICONNAME,37))) goto me1;
  174.  
  175.  /* Read command line arguments */
  176.  rda=ReadArgs(Template,(LONG *) &def,NULL);
  177.  
  178.  /* Get memory for FIB */
  179.  if (!(fib=calloc(sizeof(struct FileInfoBlock),1))) goto me2;
  180.  
  181.  /* Any objects to delete? */
  182.  if (name=def.names)
  183.   {
  184.    /* Yes. Should we open a safety requester? */
  185.    if (!def.quiet && !PopUpRequester(&SafetyES)) goto me3;
  186.  
  187.    /* Delete objects */
  188.    while (*name)
  189.     {
  190.      BPTR fl;
  191.      char *cp;
  192.      ULONG len;
  193.      BOOL deleted;
  194.  
  195.      /* Init variables */
  196.      cp=*name;
  197.      len=strlen(cp)-1;
  198.      deleted=TRUE;
  199.  
  200.      /* File or Directory? */
  201.      if (fl=Lock(cp,SHARED_LOCK))
  202.       if (Examine(fl,fib))
  203.        {
  204.         /* Directory? */
  205.         if (fib->fib_DirEntryType>=0)
  206.          ScanDirectory(fl); /* Delete all files in directory */
  207.  
  208.         /* Remove trailing '/' */
  209.         if (cp[len]=='/') cp[len]='\0';
  210.  
  211.         /* Delete file */
  212.         UnLock(fl);
  213.         SetProtection(cp,0);
  214.         if (!DeleteFile(cp)) deleted=FALSE;
  215.        }
  216.       else UnLock(fl);
  217.  
  218.      /* Delete icon */
  219.      if (deleted && (cp=malloc(len+7)))
  220.       {
  221.        /* Create icon file name */
  222.        strcpy(cp,*name);
  223.        strcat(cp,".info");
  224.  
  225.        /* No icon? Create it! */
  226.        if (fl=Lock(cp,SHARED_LOCK))
  227.         UnLock(fl);
  228.        else
  229.         {
  230.          BPTR fh=Open(cp,MODE_NEWFILE);
  231.          if (fh) Close(fh);
  232.         }
  233.  
  234.        /* Delete icon */
  235.        SetProtection(cp,0);
  236.        DeleteDiskObject(*name);
  237.        free(cp);
  238.       }
  239.  
  240.      /* Next argument */
  241.      name++;
  242.     }
  243.   }
  244.  else /* Pop up about requester */
  245.   PopUpRequester(&AboutES);
  246.  
  247.  /* Exit program */
  248. me3: free(fib);
  249. me2: if (rda) FreeArgs(rda);
  250.      CloseLibrary(IconBase);
  251. me1: exit(0);
  252. }
  253.