home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1896 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  3.6 KB

  1. From: gregory@agcsun.UUCP (Gregory Bloom)
  2. Newsgroups: alt.sources
  3. Subject: superfluous #include spotter
  4. Message-ID: <855@agcsun.UUCP>
  5. Date: 3 Oct 90 16:33:44 GMT
  6.  
  7.  
  8. Here is incspot.c, a REE-LEE-CHEE-ZEE program that I use to ferret out
  9. unnecessary #include statements in a project so as to minimize impact
  10. of header file changes.
  11.  
  12. To use, just compile and invoke it in the directory of your choice.
  13. incspot will test each #include in *.c using cc.  Of course, this
  14. could take a while, so you should plan to go get a ham sandwich
  15. or something while it is running.
  16.  
  17. It does not recurse to identify superfluous dependent #includes, so
  18. you might want to run it a second time after cleaning up a first pass
  19. to be sure you got everything.
  20.  
  21.  
  22. -------------------------------CUT HERE----------------------------------------
  23.  
  24. /*-----------------------------------------------------------------------------
  25. /*
  26. /*   incspot.c
  27. /*
  28. /*   An incredibly crude program to point out unnecessary #include statements
  29. /*
  30. /*   G. Bloom  7/19/90
  31. /*
  32. /*   agcsun!gregory@boulder.colorado.edu
  33. /*
  34. /*-----------------------------------------------------------------------------
  35. */
  36.  
  37. #include <stdio.h>
  38.  
  39. #define  MAX_LINE_LEN  1024
  40.  
  41. main ()
  42. {
  43.     FILE *ls, *source_file, *temp_file, *temp_source;
  44.     char source_file_name[256];
  45.     char source_line[MAX_LINE_LEN], temp_line[MAX_LINE_LEN];
  46.     int line_count, temp_count;
  47.  
  48.         /* open a pipe to an ls of all .c source files */
  49.     ls = popen ("ls *.c", "r");
  50.  
  51.         /* while there are .c files left to process */
  52.     while (fgets (source_file_name, 256, ls) != (char *)NULL)
  53.     {
  54.             /* blow newline */
  55.         source_file_name[strlen (source_file_name) - 1] = 0;
  56.  
  57.             /* open a read stream into this .c source */
  58.         source_file = fopen (source_file_name, "r");
  59.  
  60.  
  61.             /* for each line in this file... */
  62.         for (line_count = 0; 
  63.              fgets (source_line, MAX_LINE_LEN, source_file) != (char *)NULL;
  64.              line_count++)
  65.         {
  66.             if (strncmp (source_line, "#include", 8) == 0)
  67.             {
  68.                     /* create a new temp file to compile without one #include */
  69.                 temp_file = fopen ("incspot_temp.c", "w");
  70.  
  71.                     /* open a second read stream to copy this source to temp */
  72.                 temp_source = fopen (source_file_name, "r");
  73.  
  74.                     /* copy this source to temp except this #include */
  75.                 for (temp_count = 0;
  76.                      fgets (temp_line, 1024, temp_source) != (char *)NULL;
  77.                      temp_count++)
  78.                 {
  79.                         /* skip #include */
  80.                     if (temp_count != line_count)
  81.                         fputs (temp_line, temp_file);
  82.                 }
  83.  
  84.                 fclose (temp_source);
  85.                 fclose (temp_file);
  86.  
  87.                     /* see if the temp compiles without this #include */
  88.                 if (system ("cc -c incspot_temp.c 2> /dev/null") == 0)
  89.                 {
  90.                         /* blow newline */
  91.                     source_line[strlen (source_line) - 1] = 0;
  92.  
  93.                         /* tell the user this #include in this file is bogus */
  94.                     printf ("%s line #%d: %s\n", 
  95.                             source_file_name, line_count + 1, source_line);
  96.                 }
  97.             }
  98.         }
  99.  
  100.             /* we're through checking this source */
  101.         fclose (source_file);
  102.  
  103.     }  /* while (there are sources to check) */
  104.  
  105.         /* tidy up */
  106.     pclose (ls);
  107.     unlink ("incspot_temp.c");
  108.     unlink ("incspot_temp.o");
  109. }
  110.  
  111.  
  112. -------------------------------CUT HERE----------------------------------------
  113.  
  114.  
  115. Have fun.
  116.  
  117.  
  118. Gregory Bloom
  119. agcsun!gregory@boulder.colorado.edu
  120.