#!/bin/sh
# -*- perl -*-
# This code allows us to start perl from our path or an environment variable
# rather than hardcoding a path into the #! line.  It works from sh or csh.
(exit $?0) && eval 'exec ${QPERLQ-perl} -x $0 ${1+"$@"}'
if (! $?QPERLQ) setenv QPERLQ perl
exec $QPERLQ -x $0 $argv:q

#!/usr/local/bin/perl -w
#
# $Id: count_lines,v 1.4 1998/07/28 18:13:12 ejb Exp $
# $Source: /home/ejb/scripts/RCS/count_lines,v $
# $Author: ejb $
#


my $whoami = ($0 =~ m,([^/]*)$,) ? $1 : $0;

require 5.001;
use strict;

&usage if (@ARGV < 1);
my @files = (@ARGV);

undef $/;
my $total = 0;
my $f;
foreach $f (@files)
{
    if (open(FILE, "<$f"))
    {
	my $lines = 0;
	my $input = scalar(<FILE>);
	$input =~ s,/\*.*?\*/,,sg;
	$input =~ s,//.*$,,mg;
	$input =~ s/^(\s+)$//mg;
	$input =~ s/\n{2,}/\n/g;
	$input =~ s/^\n//;
	my @t = split(/\n/, $input);
	$lines = scalar(@t);
	close(FILE);
	print "$f: $lines\n";
	$total += $lines;
    }
    else
    {
	print STDERR "$whoami: can't read $f\n";
    }
}
print "TOTAL: $total\n" if (@files > 1);



sub usage
{
    print STDERR "Usage: $whoami file [ file ... ]\n";
    print STDERR "  Counts lines of C/C++ code excluding comments " .
	"and blank lines.\n";
    exit 1;
}
