home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume05 / vick < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  3.6 KB

  1. From decwrl!labrea!rutgers!mailrus!cwjcc!hal!ncoast!allbery Sat Nov  5 22:38:41 PST 1988
  2. Article 706 of comp.sources.misc:
  3. Path: granite!decwrl!labrea!rutgers!mailrus!cwjcc!hal!ncoast!allbery
  4. From: jbm@uncle.UUCP (John B. Milton)
  5. Newsgroups: comp.sources.misc
  6. Subject: v05i029: Program to hunt for vi modelines in text files
  7. Message-ID: <353@uncle.UUCP>
  8. Date: 2 Nov 88 02:04:55 GMT
  9. Sender: allbery@ncoast.UUCP
  10. Reply-To: jbm@uncle.UUCP (John B. Milton)
  11. Organization: Just me and my computer, Columbus Ohio
  12. Lines: 131
  13. Approved: allbery@ncoast.UUCP
  14.  
  15. Posting-number: Volume 5, Issue 29
  16. Submitted-by: "John B. Milton" <jbm@uncle.UUCP>
  17. Archive-name: vick
  18.  
  19. [Uh, it's the first and last FIVE lines, no?  ++bsa]
  20.  
  21. This has already been posted to:
  22. Newsgroups: unix-pc.bugs,unix-pc.sources,comp.sys.att,comp.unix.wizards
  23.  
  24. This does make one think, now that everone knows that this can be done!
  25. HP took care of it in their vi, you just can't do it; no switch to enable or
  26. anything. It is indeed a big security hole. NEVER vi a user file from root,
  27. you never know what might be in it! Below is a program "vick", which will
  28. scan stdin or a list of files for these commands. It only looks at the first
  29. four and the last four lines, and for ex, ei, vi and vx. The command passed to
  30. vi seems to be from the : after "vi" to the LAST colon on the line. If you are
  31. not using this on a UNIXpc, check your local vi and tune it accordingly.
  32. Always look for strange vi behavior, it can come from: the environment variable
  33. EXINIT, from ./.exrc, /.exrc, these imbedded vi:: commands, functions and 
  34. aliases picked up here and there, weirdness from shelling out of other programs.
  35.  
  36. ---cut---cut---cut---cut---cut---cut---cut---
  37. /* vi:set ai sm ts=2 sw=2: */
  38.  
  39. #include <stdio.h>
  40. #include <string.h>
  41.  
  42. extern int errno;
  43. extern int optind;
  44. extern char *optarg;
  45.  
  46. #define VIMAXLINESIZE 1000
  47.  
  48. static char *me,Verbose=0;
  49.  
  50. void perrorf(format,a1,a2,a3,a4,a5,a6,a7,a8,a9)
  51. char *format,*a1,*a2,*a3,*a4,*a5,*a6,*a7,*a8,*a9;
  52. {
  53.     char line[200];
  54.  
  55.     sprintf(line,format,a1,a2,a3,a4,a5,a6,a7,a8,a9);
  56.     perror(line);
  57. }
  58.  
  59. static int HasExCmd(s)
  60. char *s;
  61. { /* sort of-> [ev][ix]:.*: */
  62.     int i;
  63.     char *colon;
  64.  
  65.     if ((colon=strchr(s,':'))==NULL)
  66.         return(0); /* no colon */
  67.     if (colon-s<2)
  68.         return(0); /* colon too close to beginning to have vi */
  69.     colon--;
  70.     if (*colon!='i' && *colon!='x')
  71.         return(0); /* character before : is not i|x */
  72.     colon--;
  73.     if (*colon!='v' && *colon!='e')
  74.         return(0); /* character before : is not v|e */
  75.     if (strchr(colon+3,':')==NULL)
  76.         return(0); /* no second colon, command ignored */
  77.     else {
  78.         if (Verbose) {
  79.             fputs(s,stdout);
  80.             for (i=0; i<colon-s; i++)
  81.                 putchar(' ');
  82.             puts("^");
  83.         }
  84.         return(1); /* GOT ONE! */
  85.     }
  86. }
  87.  
  88. static int filt(f)
  89. FILE *f;
  90. {
  91.     int i;
  92.     long int l=0;
  93.     char line[4][VIMAXLINESIZE];
  94.  
  95.     while (fgets(line[l%4],VIMAXLINESIZE,f)!=NULL) {
  96.         if (l<4 && HasExCmd(line[l%4])) /* will ALWAYS eval left to right! */
  97.             return(1);
  98.         l++;
  99.     }
  100.     if (l>4)
  101.         for (i=l-4; i<l; i++)
  102.             if (i>=4)
  103.                 if (HasExCmd(line[i%4]))
  104.                     return(1);
  105.     return(0);
  106. }
  107.  
  108. static char *usage="Usage: %s\n";
  109.  
  110. int main(argc,argv)
  111. int argc;
  112. char *argv[];
  113. {
  114.     int bad=0,i,opt;
  115.     FILE *f;
  116.  
  117.     me=argv[0];
  118.     while ((opt=getopt(argc,argv,"v?"))!=EOF)
  119.         switch (opt) {
  120.             case 'v':
  121.                 Verbose=1;
  122.                 break;
  123.             case '?':
  124.             default:
  125.                 fprintf(stderr,usage,me);
  126.                 exit(1);
  127.                 break;
  128.         }
  129.     if (argc==optind)
  130.         bad+=filt(stdin);
  131.     else
  132.         for (i=optind; i<argc; i++)
  133.             if ((f=fopen(argv[i],"r"))==NULL)
  134.                 perrorf("%s: open %s for reading",me,argv[i]);
  135.             else {
  136.                 bad+=filt(f);
  137.                 fclose(f);
  138.             }
  139.     return(bad);
  140. }
  141.  
  142. John
  143. -- 
  144. John Bly Milton IV, jbm@uncle.UUCP, n8emr!uncle!jbm@osu-cis.cis.ohio-state.edu
  145. home (614) 294-4823, work (614) 764-4272; ei:wq!: (isn't vi fun?)
  146.  
  147.  
  148.