home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 500.lha / nodelete_v1.5a / nodelete.c < prev    next >
C/C++ Source or Header  |  1991-04-08  |  7KB  |  373 lines

  1. /* NoDelete V1.5A (C) HooverSoft 1991
  2.  
  3.     cc NoDelete +L
  4.     ln NoDelete -la -lc32
  5.  
  6.  
  7. */
  8.  
  9.  
  10. #ifdef AZTEC_C                    /* Works under Manx, convert it to Lattice */
  11.                                     /* if you don't mind the hassle...             */
  12.  
  13. #include<libraries/arpbase.h>
  14. #include<intuition/intuition.h>
  15. #include<intuition/intuitionbase.h>
  16. #include<libraries/dos.h>
  17. #include<libraries/dosextens.h>
  18. #include<stdio.h>
  19. /* #define DEBUG 1 */
  20.  
  21. char bodystring[80],patterns[10][10], titlebak[80], title[80];
  22. ULONG answer, PAT_SET = 0L,  howmany = 0L;
  23. ULONG OVERRIDE = 0L, SILENT = 0L;
  24.  
  25.  
  26. struct NewWindow mywindow = {
  27.     100,0,    /* window XY origin relative to TopLeft of screen */
  28.     350,10,    /* window width and height */
  29.     0,1,    /* detail and block pens */
  30.     CLOSEWINDOW+VANILLAKEY,    /* IDCMP flags */
  31.     WINDOWDRAG+WINDOWDEPTH+WINDOWCLOSE+SIMPLE_REFRESH,    /* other window flags */
  32.     NULL,    /* first gadget in gadget list */
  33.     NULL,    /* custom CHECKMARK imagery */
  34.     (UBYTE *)title,    /* window title */
  35.     NULL,    /* custom screen pointer */
  36.     NULL,    /* custom bitmap */
  37.     5,5,    /* minimum width and height */
  38.     640,110, /* maximum width and height */
  39.     WBENCHSCREEN    /* destination screen type */
  40.     };
  41.  
  42.  
  43. struct IntuiText body = {
  44.     1,0,
  45.     JAM1|COMPLEMENT,
  46.     30,16,
  47.     NULL,
  48.     NULL,
  49.     NULL
  50.     };
  51.  
  52. struct IntuiText fbody = {
  53.     1,0,
  54.     JAM1|COMPLEMENT,
  55.     30,5,
  56.     NULL,
  57.     (UBYTE *) "System tries to fool around...",
  58.     &body
  59.     };
  60.  
  61. struct IntuiText ok = {
  62.     1,0,
  63.     JAM1|COMPLEMENT,
  64.     6,3,
  65.     NULL,
  66.     (UBYTE *)    "Yes, Delete!",
  67.     NULL
  68.     };
  69.  
  70. struct IntuiText no = {
  71.     1,0,
  72.     JAM1|COMPLEMENT,
  73.     6,3,
  74.     NULL,
  75.     (UBYTE *)  "Oops, No Way!",
  76.     NULL
  77.     };
  78.  
  79.  
  80. struct IntuiMessage *msg, *GetMsg();
  81.  
  82. struct IntuitionBase *IntuitionBase;
  83. struct Window            *mw, *aw, *OpenWindow();
  84. struct DosBase            *DosBase;
  85. struct ArpBase            *ArpBase;
  86. UBYTE                        *filename;
  87. void                         *OpenLibrary();
  88. struct Task                *FindTask();
  89.  
  90. void NoDelete()
  91. {
  92.     char parsed[10];
  93. #asm
  94.     public    _LVOSetProtection
  95.  
  96.     movem.l    d0-d7/a0-a6,-(sp)
  97. #endasm
  98.     geta4();
  99. #asm
  100.     move.l    d1,_filename
  101. #endasm
  102.  
  103.     if(PAT_SET)
  104.         {
  105.  
  106.         for(answer=0L; answer < howmany; answer++)            
  107.             {
  108.             PreParse(patterns[answer], parsed);
  109.             if( !PatternMatch(parsed, filename) ) /* this is new */
  110.                 continue;
  111.             else
  112.                 goto request;
  113.             }
  114.         }
  115.     else 
  116.         goto request;
  117.     goto delete_anyway;        
  118.         
  119. request:
  120.     strcpy(bodystring, "Delete File `");
  121.     strcat(bodystring, (char *)filename);
  122.     strcat(bodystring, "'?");
  123.     body.IText = (UBYTE *) bodystring;
  124.     aw = (struct Window *) IntuitionBase->ActiveWindow;
  125.     answer = (long)AutoRequest(aw, &fbody, &ok, &no, NULL, NULL, 360L, 61L);
  126.     if(!answer)
  127.     {
  128.         ;
  129. #asm
  130.     movem.l    (sp)+,d0-d7/a0-a6
  131.     move.l    #1,d0
  132. #endasm
  133.     return;
  134.     }
  135.  
  136.     if(answer)
  137.     {
  138.  
  139. delete_anyway:
  140.         ;
  141. #asm
  142.  
  143.         move.l    _DosBase,a6        
  144.         move.l    #$70f7,d0            ;restore old vector
  145.         move.w    d0,-72(a6)
  146.         move.l    #$60000092,d0
  147.         move.l    d0,-70(a6)
  148.         
  149.         tst.l        _OVERRIDE
  150.         beq.s        _dont_unprotect
  151.  
  152.         move.l    _filename,d1
  153.  
  154.  
  155.         move.l    #0,d2
  156.         jsr        _LVOSetProtection(a6)
  157.  
  158. _dont_unprotect:
  159.  
  160.         move.l    _filename,d1
  161.         jsr        -72(a6)                ;DeleteFile() call
  162. #endasm
  163.  
  164.         ;
  165.  
  166.         SetFunction(DosBase, -72L, &NoDelete);
  167.  
  168. /* install our function */
  169.  
  170.  
  171. #asm
  172.         movem.l    (sp)+,d0-d7/a0-a6
  173.         move.l    #1,d0
  174. #endasm
  175.         return;
  176.         }
  177.     }
  178.         
  179.  
  180. void quit(why)
  181. UBYTE *why;
  182. {
  183.     if(!SILENT)
  184.         Printf(why);
  185.     SetTaskPri((struct Task *)FindTask(0L), 0L);
  186.     if(DosBase)
  187.     {
  188.         ;
  189. #asm
  190.         move.l    _DosBase,a6        
  191.         move.l    #$70f7,d0            ;restore old vector
  192.         move.w    d0,-72(a6)
  193.         move.l    #$60000092,d0
  194.         move.l    d0,-70(a6)
  195. #endasm
  196.         ;
  197.     }
  198.  
  199.     if    (mw)
  200.         CloseWindowSafely(mw);
  201.  
  202.     if(ArpBase)
  203.         CloseLibrary(ArpBase);
  204.     exit(0L);
  205. }
  206.  
  207. /* test_nd() tests the DeleteFile vector to see if NoDelete is already 
  208.     installed. Returns zero if NoDelete is installed, one if it is not.
  209. */
  210. #asm
  211.         public    _test_nd
  212. _test_nd:
  213.  
  214.         move.l    _DosBase,a6
  215.         cmpi.l    #$60000092,-70(a6)
  216.         bne.s        _is_there                
  217.         moveq        #1,d0
  218.         bra.s        _fuckoff
  219. _is_there:
  220.         moveq        #0,d0
  221. _fuckoff:
  222.         rts    
  223. #endasm
  224.  
  225.  
  226. void main(argc,argv)
  227. long argc;
  228. char *argv[];
  229.     {
  230.  
  231.     static UBYTE *msg1 = (UBYTE *)"Installing NoDelete...";
  232.     static UBYTE *msg2 = (UBYTE *)"done. NoDelete V1.5A (c) HooverSoft 1991. \
  233. This Program is FREEWARE.\n";
  234.     long i;
  235.  
  236.     if(! (ArpBase = (struct ArpBase *) OpenLibrary("arp.library",0L) ) )
  237.         quit("NoDelete needs arp.library\n");
  238.     
  239.     DosBase = (struct DosBase *) ArpBase->DosBase;
  240.     IntuitionBase = (struct IntuitionBase *) ArpBase->IntuiBase;  
  241.  
  242.     strcpy(title, (UBYTE *) "NoDelete V1.5A (c) HooverSoft");
  243.  
  244.  
  245.     
  246.  
  247.  
  248.             
  249.  
  250. #ifdef    DEBUG
  251.         Printf("I found %ld patterns.\n", howmany);
  252. #endif            
  253.  
  254.  
  255.  
  256.     for(i=1; i<argc; i++)
  257.         {
  258. #ifdef DEBUG
  259.             Printf("Scanning argv[%ld]: %s\n", i, (UBYTE *) argv[i] );
  260. #endif
  261.             if( (char) argv[i][0] == '-')
  262.                 {
  263.                 switch( (char) argv[i][1] )
  264.                     {
  265.                     case 'y':    if( atoi(argv[i+1]) <= 245L)
  266.                                         mywindow.TopEdge = atoi(argv[i+1]);
  267.                                     i++;    
  268.                                     goto fuckoff;                    
  269.                                      break;
  270.  
  271.                     case 'p':    if(atoi(argv[i+1]) < 10L && atoi(argv[i+1]) > -20L)
  272.                                         SetTaskPri((struct Task *)FindTask(0L),\
  273.                                                         (long)atoi(argv[i+1]));
  274.                                     else
  275.                                         SetTaskPri((struct Task *)FindTask(0L), -5L);
  276.  
  277.                                     i++;
  278.                                     goto fuckoff;
  279.                                     break;
  280.  
  281.  
  282.                     case  'O':    OVERRIDE = 1L;
  283.                                     goto fuckoff;
  284.                                     break;
  285.                 
  286.                     case  'S':    SILENT = 1L;
  287.                                     goto fuckoff;
  288.                                     break;
  289.                 
  290.                     case default:
  291.                                     Printf("Unknown Option -%c\n", argv[i][1]); 
  292.                                     goto fuckoff; 
  293.                                     break;
  294.                     }
  295.                 /* end switch */
  296.                 }/*end if */
  297.             else
  298.                 {
  299.                 PAT_SET = 1L;
  300.                 if( howmany<10 )
  301.                     strcpy(patterns[howmany], argv[i]);
  302. #ifdef    DEBUG
  303.                 Printf("Copied %s, Result: %s\n", argv[i], patterns[i-1L]);
  304. #endif    
  305.                 howmany++;
  306.                 }
  307. fuckoff:
  308.             ;
  309.         /*end for */
  310.             }    
  311.                     
  312.     if(howmany)
  313.         strcat(title, (UBYTE *) " PAT SET");            
  314.     else
  315.         {
  316.         PAT_SET = 0L;
  317.         strcat(title, (UBYTE *) " NO PAT");
  318.         }
  319.  
  320. /* calculate dimension of TitleBar */
  321.  
  322.     mywindow.Width = ( strlen(title) * 8L) + 85L;
  323.     mywindow.LeftEdge = (640L-mywindow.Width) / 2L;
  324.  
  325.     if(!SILENT)
  326.         Printf(msg1);
  327.  
  328.     if( !test_nd() )
  329.         {
  330.         Printf("NoDelete already installed.\n");
  331.         CloseLibrary(DosBase);
  332.         CloseLibrary(IntuitionBase);
  333.         CloseLibrary(ArpBase);
  334.         exit(0L);
  335.         }
  336.  
  337.     mw = OpenWindow(&mywindow);
  338.     if(!mw)
  339.         quit("Could not open Window.\n\n");
  340.     if(!SILENT)
  341.         Printf(msg2);
  342.  
  343.     SetFunction(DosBase, -72L, &NoDelete); 
  344.  
  345.     for(;;)
  346.         {
  347.         Wait(1L << mw->UserPort->mp_SigBit);
  348. getmsg:
  349.         msg = GetMsg(mw->UserPort);
  350.         ReplyMsg(msg);
  351.         if (msg->Class == CLOSEWINDOW)
  352.             quit("NoDelete V1.5A exiting.\n\n");
  353.         if(msg->Class == VANILLAKEY)
  354.             {
  355.             strcpy(titlebak, (UBYTE *) "Patterns: ");
  356.             for(i=0L; i < howmany ; i++)
  357.                 {
  358.                 strcat(titlebak, patterns[i]);
  359.                 strcat(titlebak, " ");
  360.                 }
  361.             SetWindowTitles(mw, titlebak);
  362.             Wait(1L << mw->UserPort->mp_SigBit);
  363.             SetWindowTitles(mw, title);
  364.             ReplyMsg( GetMsg(mw->UserPort) );
  365.             }
  366.  
  367.         else
  368.             continue;
  369.         }
  370.     }
  371. #endif    
  372.     
  373.