FUNCTIONS="malloc realloc free" ;
byline()
{
	for FUNCTION in ${FUNCTIONS} ; do
	printf "\033[32;01m * \033[0m\033[1m${FUNCTION}\033[33;01m()\033[0m's by file: \n" ;
		for SOURCEFILE in *.c ; do
			# debug
			# echo $FUNCTION $SOURCEFILE
			printf "\033[32;1m * \033[0m${SOURCEFILE}: \n";
			grep --color=always -n "${FUNCTION}(" ${SOURCEFILE} > /tmp/grep;
			ENTRIES=`echo \`cat /tmp/grep | wc -l\` | bc`
			if [ ${ENTRIES} \!= 0 ]; then
				COUNT=1
				while [ ${COUNT} != ${ENTRIES} ]; do
					printf "  \033[32;1m * \033[0m"
					cat /tmp/grep | head -n ${COUNT} | tail -n 1
					COUNT=`echo ${COUNT}+1 | bc`
				done
			fi
		done		
	done
}

context()
{
	for FUNCTION in ${FUNCTIONS}; do 
		printf "\033[32;01m * \033[0m\033[1m${FUNCTION}\033[33;01m()\033[0m's by file: \n" ;
		for SOURCEFILE in *.c ; do
			# debug
			# echo $FUNCTION $SOURCEFILE
			printf "\033[32;1m * \033[0m${SOURCEFILE}: ";
			grep --color=always -n -C 1 "${FUNCTION}(" ${SOURCEFILE} > /tmp/grep;
			ENTRIES=`echo \`cat /tmp/grep | wc -l\` | bc`;
			if [ ${ENTRIES} \!= 0 ]; then
				COUNT=1;
				printf "\n";
				while [ ${COUNT} != ${ENTRIES} ]; do
					printf "  \033[32;1m * \033[0m"
					cat /tmp/grep | head -n ${COUNT} | tail -n 1
					COUNT=`echo ${COUNT}+1 | bc`
				done
			else 
				printf " No occurences of ${FUNCTION}\n";
			fi
		done		
	done
}

default()
{
	for FUNCTION in ${FUNCTIONS}; do 
		printf "\033[32;01m * \033[0m\033[1m${FUNCTION}\033[33;01m()\033[0m's by file: \n" ;
		for SOURCEFILE in *.c ; do
		printf "\033[32;1m * \033[0m${SOURCEFILE}	: ";
		grep "${FUNCTION}(" ${SOURCEFILE} -c;
		done
	done
}

if [ $# == 0 ]; then 
	default; 
	exit;
fi

if [ $# == 1 ]; then
	if [ $1 == '-l' ]; then
		byline;
		rm /tmp/grep;
		exit;
	fi
	if [ $1 == '-C' ]; then
		context;
		rm /tmp/grep;
		exit;
	fi
fi

default;
