    #include <stdio.h>
    #include <stdlib.h>
    
    char * readLine (FILE * file)
    {
        size_t len = 0;
        int c = 0, i = 0;
        long pos = ftell(file);
        char * out = 0;
    
        // read the whole line
        do { c = fgetc(file); len++; }
        while (c!='\0' && c!='\n' && c!=EOF);
        
        // if the cursor didn't move return NULL
        if (pos == ftell(file) && c == EOF) return 0;
        
        // allocate required memory
        out = (char*)malloc(len+1);

        // rewind cursor to beginning of line
        fseek (file, pos, SEEK_SET);
        
        // copy the line
        do { out[i++] = fgetc(file); }
        while (c!='\0' && c!='\n' && c!=EOF);
        
        // make sure there's \0 at the end
        out[i] = '\0';

        return out;
    }
    
    int main (void)
    {
    //  FILE * file = fopen("test.txt", "r");
        char * line = readLine(stdin);
    
        while(line)
        {
            printf(line); // print current line
            free(line); // free allocated memory
            line = readLine(stdin); // recur
        }
    
        return 0;
    }