home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / fish / disks / d1099.lha / Programs / Retire / retire.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-04  |  4.2 KB  |  213 lines

  1. /* RETIRE   Remove entries from User-Startup                         ***RGR***
  2.                                                                       3-apr-95
  3.    Author:  Ralf Gruner, An der Sense 5a, D-02779 Großschönau, Germany.
  4.  
  5.    MANX Aztec C 5.2:
  6.    cc retire
  7.    ln retire -lc
  8. */
  9.  
  10. //#define DEBUG
  11.  
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15.  
  16. #define EOL 10
  17.  
  18. char *filename1="S:User-Startup", *filename2="RAM:User-Startup.temp";
  19. FILE *infile=0, *outfile=0;
  20. char *help="\nRETIRE removes entries from User-Startup.\nUsage: retire <Application 1> [<Application 2> ... [<Application n>]...]\n\n";
  21. char *version="$VER: RETIRE 1.0 (03.04.95)";
  22. char *application;
  23. int i,j,k,l;
  24. char line[256], *lineptr;
  25. int c=0;
  26.  
  27. long ftell();
  28.  
  29. /*************/
  30. void CloseAndExit(char *Msg)
  31. {
  32.     if (Msg!=0L)    puts(Msg);
  33.     if (infile)    fclose(infile);
  34.     if (outfile)    fclose(outfile);
  35.     remove(filename2);
  36.     if (Msg!=0L)    exit(10);
  37.     exit(0);
  38. }
  39.  
  40. /*************/
  41. void ReadTextLine(void)
  42. {
  43.     for(k=0; k<256; k++)
  44.         line[k]='\0';    // clear line buffer
  45.  
  46.     k=0;
  47.     while((c=fgetc(infile))!=EOF)    // read line
  48.     {
  49.         line[k++]=c;
  50.  
  51.         #ifdef DEBUG
  52.             printf("%c",c);
  53.         #endif
  54.  
  55.         if(c==EOL || c==EOF || k>=256) break;
  56.     }
  57. }
  58.  
  59. /*************/
  60. main(int argc, char *argv[])
  61. {
  62.     long beginpos;
  63.     int ok=0;
  64.  
  65.     if (*argv[1] == '?')
  66.     {
  67.         printf("\nRETIRE v%s   Author: Ralf Gruner, Großschönau, Germany.\n",version+13);
  68.         printf(help);
  69.         exit(0);
  70.     }
  71.  
  72.     if (argc < 2)
  73.     {
  74.         printf("\nMissing application name.\n");
  75.         printf(help);
  76.         CloseAndExit("");
  77.     }
  78.     else
  79.     {
  80.         for(i=1; i<=argc-1; i++)
  81.         {
  82.             #ifdef DEBUG
  83.                 printf("\nPass %d: %s\n",i,argv[i]);
  84.             #endif
  85.  
  86.             if(!(infile=fopen(filename1,"r")))
  87.             {
  88.                 printf("%s does not exist.\n",filename1);
  89.                 CloseAndExit(0L);
  90.             }
  91.  
  92.             if(!(outfile=fopen(filename2,"w")))
  93.             {
  94.                 printf("\nCould not open %s.\n",filename2);
  95.                 CloseAndExit("");
  96.             }
  97.  
  98.             while((c=fgetc(infile))!=EOF)    // copy s:user-startup into ram-disk
  99.             {
  100.                 if(fputc(c, outfile)==EOF)
  101.                     CloseAndExit("\nError while writing.\n");
  102.             }
  103.  
  104.             fclose(infile);
  105.             fclose(outfile);
  106.  
  107.             if(!(infile=fopen(filename2,"r")))
  108.             {
  109.                 printf("\nCould not open %s.\n",filename2);
  110.                 CloseAndExit("");
  111.             }
  112.  
  113.             if(!(outfile=fopen(filename1,"w")))
  114.             {
  115.                 printf("\nCould not open %s for writing.\n",filename1);
  116.                 CloseAndExit("");
  117.             }
  118.  
  119.             application=argv[i];    // application name
  120.             c=0;
  121.             ok=0;
  122.  
  123.             while(c!=EOF)
  124.             {
  125.                 beginpos=ftell(infile);
  126.                 ReadTextLine();
  127.                 while(ok==1 && line[0]==EOL && c!=EOF)
  128.                 {
  129.                     beginpos=ftell(infile);
  130.                     ReadTextLine();    // remove empty lines after removed entry
  131.                 }
  132.                 ok=0;
  133.                 if(strnicmp(&line[0],";BEGIN ",7)==0)
  134.                 {
  135.                     lineptr=&line[7];
  136.                     while(*lineptr==' ')
  137.                         lineptr++;    // skip spaces
  138.                     if(strnicmp(lineptr,application,strlen(application))==0)
  139.                     {
  140.                         lineptr+=strlen(application);
  141.                         while(*lineptr==' ')
  142.                             lineptr++;    // skip spaces
  143.                         if(*lineptr==EOL)
  144.                         {
  145.                             ok=1;
  146.                             while(strnicmp(&line[0],";END ",5)!=0 && c!=EOF)
  147.                             {
  148.                                 ReadTextLine();
  149.                                 if(c==EOF)
  150.                                 {
  151.                                     ok=0;
  152.                                     break;
  153.                                 }
  154.                             }
  155.                             if(ok==1)
  156.                             {
  157.                                 // found ;END
  158.                                 lineptr=&line[5];
  159.                                 while(*lineptr==' ')
  160.                                     lineptr++;    // skip spaces
  161.                                 if(strnicmp(lineptr,application,strlen(application))==0)
  162.                                 {
  163.                                     // found name
  164.                                     lineptr+=strlen(application);
  165.                                     while(*lineptr==' ')
  166.                                         lineptr++;    // skip spaces
  167.                                     if(*lineptr==EOL)
  168.                                     {
  169.                                         ok=1;
  170.                                     }
  171.                                     else
  172.                                     {
  173.                                         ok=0;
  174.                                     }
  175.                                 }
  176.                                 else
  177.                                     ok=0;
  178.                             }
  179.                             if(ok==0)
  180.                             {
  181.                                 printf("\nError in structure of S:User-Startup:\n;BEGIN %s  does not match  %s\n",application,&line[0]);
  182.                                 fseek(infile,beginpos,0);
  183.                                 ReadTextLine();    // if END not valid, read BEGIN again
  184.                             }
  185.                             if(ok==1)
  186.                             {
  187.                                 #ifdef DEBUG
  188.                                     printf("(*** removed.***)\n");
  189.                                 #endif
  190.                                 printf("%s retired.\n",application);
  191.                                 continue;
  192.                             }
  193.                         }
  194.                     }
  195.                 }
  196.  
  197.                 l=0;
  198.                 while(l<k)    // write line
  199.                 {
  200.                     c=line[l++];
  201.                     if(fputc(c, outfile)==EOF)
  202.                         CloseAndExit("\nError while writing.\n");
  203.                 }
  204.             }
  205.  
  206.             fclose(infile);
  207.             fclose(outfile);
  208.         }
  209.     }
  210. CloseAndExit(0L);
  211. }
  212.  
  213.