#!/usr/bin/perl
# Simple perl script to count the number of actual lines of code in a file.
# It uses the preprocessor to strip comments and expand #define's.
# Does tallying for multiple files.
$cc = "gcc";
$total = 0;
$files = 0;
$totsize = 0;
printf( "Lines of Code File Size Filename\n" );
LOOP: foreach $filename ( @ARGV ) {
next LOOP if( !-r $filename );
open( FILE, "${cc} -E ${filename} |" );
$lines = 0;
while( <FILE> ) {
if( !/^[ ]*$/ ) {
$lines++;
}
}
close( FILE );
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat( $filename );
printf( "%9d %15d %-12s\n", $lines, $size, $filename );
$totsize += $size;
$total += $lines;
$files++;
}
print "There were ${files} files.\n";
$ave = $total / $files;
printf( "Total lines: %18d; Average: %10d.\n", $total, $ave );
$ave = $totsize / $files;
printf( "Total filesize: %15d; Average: %10d.\n", $totsize, $ave );