Q:

How to count number of lines in a function

I need to count the number of lines in a function in a file.
I have the following inputs.
inputs:

name of the file,
name of the function

required output:

number of lines in the function

Example:
int main()
{
    line1;
    line2;
    line3;
}
int func()
{
    line1
    line2
    line3
    line4
    line5
    if(---)  //line 6
    {        //line 7
        line 8
        line 9
    }        //line10
}

so the above example should return 10 if we pass the file name and the function name as "func"
Kindly suggest some way to do it...

A:

code has a strict format, INDENT your source files  

indent  -kr -bap -nce -i8 -ts8 -sob -l80 -ss -bs -npsl -bl  -bli0  file.c

awk + substr($0,1,1) match '{' and '}'

    awk -F"," '{
    if( index(v_func,$0)<10 )
    {
            findfunc=1;
    }

    if( findfunc == 1)
    {
       if( substr($0,1,1) == "{" )
       {
               lineCnt=0;
       }
       else if(substr($0,1,1) == "}" )
       {
               print lineCnt;
       }
       else
       {
               lineCnt = lineCnt+1;
       }
    } }' v_func=$2 $1

