line counter

php

<?php
 
// define a list of programming file extensions
$countExtensions = array(
"asp",
"aspx",
"c",
"cc",
"cpp",
"h",
"htm",
"html",
"java",
"js",
"jsp",
"m",
"mm",
"php",
"pl",
"shtml"
);
 
if( $argc != 2 )
	exit("\nUsage: linecount.php <dirname>\n\n");
 
$dir = $argv[1];
 
$countExtReg = implode($countExtensions, "|");
$lineCount = 0;
 
function processDir($dirname){
	global $countExtReg, $lineCount;
 
	if( !($dh = opendir($dirname)) )
		return;
 
	$oldDir = getcwd();
 
	chdir($dirname);
 
	while( false !== ($file = readdir($dh)) ){
		// skip current and previous references
		if( preg_match("/^\.|^\.\./", $file) != 0 )
			continue;
 
		// compare against programming extensions
		if( preg_match("/^.*\.($countExtReg)$/i", $file) != 0 ){
			echo realpath($file) . "\n";
			$lines = file($file);
			$lineCount += count($lines);
			unset($lines);
		}
 
		if( is_dir($file) )
			processDir($file);
	}
 
	closedir($dh);
	chdir($oldDir);
}
 
if( !is_dir($dir) )
	exit("\n$dir is not a directory\n");
 
processDir($dir);
 
exit("\nLine Count: $lineCount\n\n");
 
?>