1

I am writing a program in c to identify functions' signature and copy in another file.

Idea was to determine parenthesis in any line and to copy that line to a file. Afterwards we can check for return type and parameters, so as to differentiate constructs if, while from user defined functions except main.

But my code stuck in infinite loop. Can't find the problem.

find_sig.c

#include <stdio.h>
int main()
{
int count=0;
char c;
FILE *f1,*f2;
f1=fopen("input.c","r");
f2=fopen("output.c","w");
while((c=getc(f1))!=EOF)
 {
    do
    {
        if(c=='(')
        {
            fseek(f1,-count,1);
            do{
                putc(c,f2);

            }while((c=getc(f1))!=')');
            count=0;
        }
        else
            count++;

    }while((c=getc(f1))!=10);
    count=0;
 }
fclose(f1);
fclose(f2);
return 0;
}

input.c

#include<stdio.h>
void fun();
int main()
{
  fun();
  return 0;
}

void fun()
{
  printf("hello");
}

Any other idea for determining functions' signature will be very helpful.

tshepang
  • 12,111
  • 21
  • 91
  • 136
sandy
  • 509
  • 1
  • 6
  • 23
  • 1
    You will have to parse C properly. That's not trivial. Maybe look into libclang? –  Sep 01 '13 at 18:55
  • @H2CO3 what's libclang – sandy Sep 01 '13 at 18:56
  • 4
    What's Google? See first match for "libclang". – LorenzoDonati4Ukraine-OnStrike Sep 01 '13 at 19:11
  • maybe this helps: http://stackoverflow.com/questions/1570917/extracting-c-c-function-prototypes – jev Sep 01 '13 at 19:16
  • @LorenzoDonati Sorry for asking silly question, but i have never heared of this term libclang. So i googled it, found some tutorials. Reading them. – sandy Sep 01 '13 at 19:26
  • @jev i am trying to install cproto. But i want to write a program(not too complicated) and doesn't want a tool for that. – sandy Sep 01 '13 at 19:33
  • you can check the available to get an idea source – jev Sep 01 '13 at 20:28
  • Have you tried using a debugger and step through your program one line at a time? I spot a couple of 'highly suspicious' constructions -- for instance, in every loop where you check `c != 10`, what would happen at the end of a line? – Jongware Sep 01 '13 at 21:06

1 Answers1

1

i figured it out.

#include<stdio.h>
#include<string.h>

char str1[50];
int count=0,i=0;
int main()
{
char c;
FILE *f1,*f2;
f1=fopen("input.c","r");
f2=fopen("output.c","w");

while((c=getc(f1))!=EOF)
{
   if(c!=10)                                    //checks for /n
   {
       if(c=='(')
       {
           ++count;
           fseek(f1,-count,1);              //moves f1 to 'count' bytes back i.e. beginning of line
           i=0;
           while((c=getc(f1))!=';'&&c!='{') //checks for declaration or definition
           {
              str1[i++]=c;
           }

           if(strstr(str1,"main(")!=NULL)   //checks whether str1 contains main 
               return 0;
           else
               {
               fprintf(f2,"%s",str1);   // copies str1 in f2
               count=0;
               }
       }
       else
       count++;
   }

   else
       count=0;
   if(c==10)
         putc(c,f2);

}

fclose(f1);
fclose(f2);
return 0;
}
sandy
  • 509
  • 1
  • 6
  • 23