home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume5 / vick < prev    next >
Encoding:
Text File  |  1989-02-03  |  3.5 KB  |  144 lines

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