home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / bbs / ff846.lha / FF846 / FileCache / ProtectTree.c < prev    next >
C/C++ Source or Header  |  1993-03-31  |  3KB  |  167 lines

  1. /* ProtectTree V1.01
  2.  * Protect recursivly files against writing and execution
  3.  * (C) 1992 Christophe PASSUELLO
  4.  * Mon Feb  8 21:07:05 1993
  5.  */
  6.  
  7. #include <exec/types.h>
  8. #include <exec/memory.h>
  9. #include <libraries/dos.h>
  10. #include <libraries/arpbase.h>
  11. #include <mytypes.h>
  12.  
  13.  
  14. #define PROTECTTREEVER    "ProtectTree V1.00"
  15.  
  16.  
  17. CPTR ArpBase = NULL;        /* pointer to arp.library */
  18.  
  19.  
  20. struct ExtAnchorPath
  21. {
  22.     struct AnchorPath Path;
  23.     char buffer[255];
  24. } *Ancre=NULL;
  25.  
  26.  
  27. /*
  28.  * free ressources
  29.  */
  30. VOID clean_exit()
  31. {
  32.     if (Ancre) FreeMem(Ancre, sizeof(struct ExtAnchorPath));
  33.     if (ArpBase) CloseLibrary(ArpBase);
  34.     exit(0);
  35. }
  36.  
  37.  
  38. /*
  39.  * Presentation
  40.  */
  41. VOID Presentation()
  42. {
  43.     puts("\x9b1;33;40m" PROTECTTREEVER "\x9b0;31;40m\x9b3;32;40m");
  44.     puts("\x9b0;31;40m(C) 1992 by Christophe PASSUELLO, All Rights Reserved.\n");
  45. }
  46.  
  47.  
  48. /*
  49.  * Print usage
  50.  */
  51. VOID Usage()
  52. {
  53.     puts("Usage: ProtectTree <FilePattern> [-all]");
  54.     puts("  <FilePattern> can be a file or a pattern as *.h");
  55.     puts("  -all option enables recursive protection.");
  56. }
  57.  
  58.  
  59. /*
  60.  * Main
  61.  */
  62. main(int ac, char **av)
  63. {
  64.     STRPTR FilePattern=NULL;
  65.     STRPTR FileName=NULL;
  66.     ULONG Mask=0;
  67.     BOOL all=FALSE;
  68.  
  69.     /* Open the arp.library V39 */
  70.     if (!(ArpBase = OpenLibrary(ArpName, ArpVersion)))
  71.     {
  72.         puts("You need arp.library V39+");
  73.         clean_exit();
  74.     }
  75.  
  76.     Presentation();
  77.  
  78.     if ((ac <= 1) || (ac > 3))
  79.     {
  80.         Usage();
  81.         clean_exit();
  82.     }
  83.  
  84.     /* Parsing of arguments */
  85.     FilePattern = av[1];
  86.  
  87.     if ((ac == 3) && (stricmp(av[2], "-all") == 0))
  88.         all = TRUE;
  89.  
  90.     /* main routine  */
  91.     if (Ancre = AllocMem(sizeof(struct ExtAnchorPath), MEMF_CLEAR|MEMF_PUBLIC))
  92.     {
  93.         LONG error;
  94.         UWORD nbfiles=0;
  95.         UWORD nbfilesprot=0;
  96.  
  97.         /* init Ancre */
  98.         Ancre->Path.ap_BreakBits = SIGBREAKF_CTRL_C;
  99.         Ancre->Path.ap_StrLen = 255;
  100.         Ancre->Path.ap_Flags = APF_DoWild;
  101.  
  102.         Mask = FIBF_WRITE|FIBF_EXECUTE;
  103.     
  104.         error = FindFirst(FilePattern, Ancre);
  105.         while(!error)
  106.         {
  107.             FileName = Ancre->Path.ap_Buf;
  108.  
  109.             /* check if directory */
  110.             if (Ancre->Path.ap_Info.fib_DirEntryType >= 0)
  111.             {
  112.                 if (all)
  113.                 {
  114.                     /* recursive protection */
  115.                     if (!(Ancre->Path.ap_Flags & APF_DidDir))
  116.                         Ancre->Path.ap_Flags |= APF_DoDir;
  117.  
  118.                     Ancre->Path.ap_Flags &= ~APF_DidDir;
  119.                 }
  120.             }
  121.             else
  122.             {
  123.                 nbfiles++;
  124.                 if (SetProtection(FileName, Mask))    /* Protection of file */
  125.                     nbfilesprot++;
  126.             }
  127.  
  128.             if (Ancre->Path.ap_Flags & APF_ItsWild == 0)        /* Stop if single file */
  129.                 break;
  130.  
  131.             error=FindNext(Ancre);
  132.         }
  133.  
  134.         FreeAnchorChain(&(Ancre->Path));
  135.         switch(error)
  136.         {
  137.             case ERROR_BREAK:
  138.                 puts("***BREAK");
  139.  
  140.             case 0:
  141.             case ERROR_NO_MORE_ENTRIES:
  142.                 printf(" - %d files protected succesfully on %d files.\n", nbfilesprot, nbfiles);
  143.                 break;
  144.  
  145.             case ERROR_OBJECT_NOT_FOUND:
  146.                 puts("File not found.");
  147.                 break;
  148.  
  149.             case ERROR_BUFFER_OVERFLOW:
  150.                 puts("Path too long!");
  151.                 break;
  152.  
  153.             default:
  154.                 printf("IO error %ld!\n",error);
  155.                 break;
  156.         }
  157.  
  158.         clean_exit();
  159.         /* THE END */
  160.     }
  161.     else
  162.     {
  163.         puts("Not enough memory");
  164.         clean_exit();
  165.     }
  166. }
  167.