I have a version of ccount but I'm not sure where it came from.
I do use it though, even though its results are sometimes slightly
inaccurate. The program is quite short so the code is given below:-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*
Program to count number of lines in C source file.
*/
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#define MAXLINE (100)
#define BLANK (0)
#define CODE (1)
#define COMMENT (2)
main(argc, argv)
int argc;
char **argv;
{
FILE *fp;
char c;
char line[MAXLINE];
int i, j, k, flen;
int lineType,
inComment;
long nCodeTotal,
nBlankTotal,
nCommentTotal;
long nCode,
nBlank,
nComment,
nTotal;
/* ---------------------------------------------------------------- */
if (argc < 2)
{
fprintf(stderr, "Usage: %s filename [filename...]\n", argv[0]);
exit(0);
}
flen = strlen(argv[1]);
for (i = 2; i < argc; i++)
if (strlen(argv[i]) > flen)
flen = strlen(argv[i]);
for (i = 8; i < flen; i++)
printf (" ");
printf("%s %6s %6s %6s %6s\n",
"File Name", "Source", "Comments", "Blank", "Total");
for (i = 0; i < flen; i++)
printf (" ");
for (i = 0; i < 4; i++)
printf (" ");
printf("%s %6s %% %6s %% %6s %% %6s\n\n",
"", "Lines", "Lines", "Lines", "Lines");
nCodeTotal = nBlankTotal = nCommentTotal = 0;
for (i = 1; i < argc; i++)
{
nCode = nBlank = nComment = 0;
fp = fopen(argv[i], "r");
if (fp != NULL)
{
while (fgets(line, MAXLINE, fp) != NULL)
{
/* Is there a start of comment in the line */
lineType = BLANK;
j = 0;
while (c = line[j++])
{
if (isgraph(c))
lineType = CODE;
if (c == '/' && line[j] == '*')
{
/* Start of comment */
lineType = COMMENT;
/* Consume the rest of the line */
inComment = 1;
nComment++;
while (c = line[j++])
{
if (c == '*' && line[j] == '/')
{
inComment = 0;
break;
}
}
if (inComment)
{
/*
* Keep consuming lines until the end of comment
* marker is found.
*/
while (fgets(line, MAXLINE, fp) != NULL)
{
j = 0;
while (c = line[j++])
{
if (c == '*' && line[j] == '/')
{
inComment = 0;
break;
}
}
if (inComment)
{
nComment++;
}
else
{
break;
}
}
}
break;
}
}
if (lineType == CODE)
{
nCode++;
}
else if (lineType == BLANK)
{
nBlank++;
}
}
fclose(fp);
}
else
{
if (errno > 0)
perror("Opening file");
}
nCodeTotal += nCode;
nBlankTotal += nBlank;
nCommentTotal += nComment;
nTotal = nCode + nBlank + nComment;
if (nTotal > 0)
{
printf (" %s", argv[i]);
for (k = strlen(argv[i]); k < flen; k++)
printf (" ");
printf(" %6d %3d %6d %3d %6d %3d %6d\n",
nCode, nCode * 100 / nTotal,
nComment, nComment * 100 / nTotal,
nBlank, nBlank * 100 / nTotal, nTotal);
}
}
printf("\n -----------------------------------------\n\n");
printf(" %12s %6s %6s %6s %6s\n", "", "Source", "Comments", "Blank", "Total");
printf(" %12s %6s %% %6s %% %6s %% %6s\n\n", "", "Lines", "Lines", "Lines", "Lines");
--argc;
nTotal = nCodeTotal + nBlankTotal + nCommentTotal;
printf(" %12s %6d %6d %6d %6d\n",
"Totals", nCodeTotal, nCommentTotal, nBlankTotal, nTotal);
printf(" %12s %6d %3d %6d %3d %6d %3d %6d\n",
"Averages", nCodeTotal / argc, nCodeTotal * 100 / nTotal,
nCommentTotal / argc, nCommentTotal * 100 / nTotal,
nBlankTotal / argc, nBlankTotal * 100 / nTotal, nTotal / argc);
printf("\n %12s %d\n", "Number of files", argc);
/* ------------------------------------------------------------------------ */
--
Andrew Peter Marlow
APM Software Ltd
email: and...@apmsl.demon.co.uk