home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / CCOUNT.ZIP / CCOUNT.C
Encoding:
Text File  |  1988-07-21  |  3.0 KB  |  119 lines

  1. /*
  2.     Title:        ccount
  3.  
  4.     Function:    Counts number of lines of code, lines with comments,
  5.             and total lines in C source programs.
  6.  
  7.     Usage:        ccount <file1> [<file2> ...]
  8.             ccount        (prints help info)
  9.  
  10.     History:    May 1983 - Created by Deb Brown, Mitre
  11.             (Based on Tom Anderson's cnest.)
  12. */
  13.  
  14. #include <stdio.h>
  15.  
  16. #define TRUE -1
  17. #define FALSE 0
  18.  
  19. main (argc, argv)
  20. unsigned int    argc;
  21. char   *argv[];
  22.  
  23. {
  24.     char    c;
  25.     char    prev_char;
  26.     unsigned register int   in_comment;
  27.     unsigned register int   line_number;
  28.     unsigned register int   file_index;
  29.     unsigned register int   count_code;
  30.     unsigned register int   count_com;
  31.     unsigned register int   some_comment;
  32.     unsigned register int   some_code;
  33.     unsigned register int   total_code = 0;
  34.     unsigned register int   total_com = 0;
  35.     unsigned register int   total_lines = 0;
  36.     char    status;
  37.     FILE   *chk_file;
  38.  
  39.     status = 0;
  40.  
  41. if (argc == 1) {
  42.     printf ("Usage: %s <file1> [<file2> ...]\n", argv [0]);
  43.     printf ("Output: lines_of_code lines_of_comments total_lines\n");
  44.     exit ();
  45. }
  46.  
  47.     for (file_index = 1; file_index < argc; ++file_index)
  48.     {
  49.     line_number = 1;
  50.     in_comment = FALSE;
  51.     count_code = 0;
  52.     count_com = 0;
  53.     prev_char = ' ';
  54.     if (argc == 1)
  55.         chk_file = stdin;
  56.     else
  57.     {
  58.         if ((chk_file = fopen (argv[file_index], "r")) == (FILE *) NULL)
  59.         {
  60.         fprintf (stderr, "%s: Can't access %s", argv[0], argv[file_index]);
  61.         status = 2;
  62.         continue;
  63.         }
  64.     }
  65.  
  66.     while ((c = fgetc (chk_file)) != EOF)
  67.     {
  68.         switch (c)
  69.         {
  70.         case '\n':
  71.             line_number++;
  72.             if (some_comment)
  73.             ++count_com;
  74.             if (some_code)
  75.             ++count_code;
  76.             some_comment = FALSE;
  77.             some_code = FALSE;
  78.             break;
  79.         case '/':
  80.             if (prev_char == '*')
  81.             {
  82.             in_comment = FALSE;
  83.             some_comment = TRUE;
  84.             }
  85.             break;
  86.         case '*':
  87.             if (prev_char == '/')
  88.             {
  89.             in_comment = TRUE;
  90.             some_comment = TRUE;
  91.             }
  92.             break;
  93. case ' ':
  94. case '\t':
  95.     break;
  96.         default:
  97.             if (in_comment)
  98.             some_comment = TRUE;
  99.             else
  100.             some_code = TRUE;
  101.             break;
  102.         }
  103.         prev_char = c;
  104.     }
  105.     fclose (chk_file);
  106.     total_code += count_code;
  107.     total_com += count_com;
  108.     total_lines += (--line_number);
  109. if (argc > 1)
  110.     printf ("%7d %7d %7d %s\n",
  111.          count_code, count_com, line_number, argv [file_index]);
  112. else
  113.     printf ("%7d %7d %7d\n", count_code, count_com, line_number);
  114.     }
  115. if (argc > 2)
  116.     printf ("\n%7d %7d %7d Total\n", total_code, total_com, total_lines);
  117.     exit (status);
  118. }
  119.