C - Read from keyboard (controlled by Ctrl+D)
<p>I'm trying to emulate a <code>tail</code> command in C but I have a problem.</p>
<p>I need to read some lines from keyboard (STDIN) (I don't care how many lines). This read will be interrupted when Ctrl+d is pressed. I have done this function but it doesn't work.</p>
<pre><code>int tail(int N)
{
    int i, j, c;
    char ** lines;
    /* Char matrix dynamic declaration */
    lines=NULL; // Char matrix declaration
    lines=malloc(N*sizeof(char *));
    for (i=0; i&lt;N; i++){ 
        lines[i]=malloc(80 * sizeof(char));
    }
    i=0;
    while (true){ // I tried this loop, but doesn't work
        /* This works. Lanes reading from the user's keyboard */
        if(i&gt;=N){
            for(j=0; j&lt;=N; j++){
                if (j&lt;(N-1))
                    lines[j]=lines[j+1];
            }
            scanf(&quot; %[^\n]&quot;, lines[N-1]);
        }else{
            scanf(&quot; %[^\n]&quot;, lines[i]);
        }
        i++;
        /* ------------------------------------ */
        c=getchar(); // This doesn't work
        if(c==EOF)
        goto end;
    }
end:
    for (i=0; i&lt;N; i++){
        printf(&quot;%s\n&quot;, lines[i]); // Showing those lanes
        free(lines[i]); // Free memory
    }    
    free(lines);
    lines = NULL;

    return 0;
}
</code></pre>
<p>My problem is about read user's keyboard lines and read ctrl+d at the same time. When I launch the function, it let me write some lines, but it doesn't detect when I press Ctrl+D. How can I do this?</p>
<h1><strong>EDIT:</strong></h1>
<p>I have tried a new method to capture Ctrl+d and now my program ends, but I have some problems. Here is the new code:</p>
<pre><code>int tail(int N)
{
    int i, j, k, c;
    char ** lines;

    lines=NULL;
    lines=malloc(N*sizeof(char *));
    for (i=0; i&lt;N; i++){ 
        lines[i]=malloc(MAXLON * sizeof(char));
    }
    i=0;
    while (true){
        /* Read lines from keyboard. */
        if(i&lt;N)
            scanf(&quot; %[^\n]&quot;, lines[i]);
        else{
            for(j=0; j&lt;N-1; j++){
                for(k=0; k&lt;MAXLON; k++){
                    lines[j][k]=lines[j+1][k];      
                }
            }
            scanf(&quot; %[^\n]&quot;, lines[N-1]);
        }
        /* ------------------------------------ */
        if((c=getchar())==EOF) break;
        else i++;
    }

    for (i=0; i&lt;N; i++){
        printf(&quot;%s\n&quot;, lines[i]);
        free(lines[i]);
    }
    free(lines);
    lines= NULL;

    return 0;

}
</code></pre>
<p>Right now, my program captures N lines from keyboard until I press 2 times Ctrl+d, but my problem now is: If I write 5 lines which are (with tail(3) for example, which shows the last 3 lines):</p>
<pre><code>Line 1 
Line 2 
Line 3 
Line 4 
Line 5  
AND HERE I PRESS 2 TIMES CTRL+D
</code></pre>
<p>The function shows:</p>
<pre><code>Line 4
Line 5
Line 5
</code></pre>
<p>Whats wrong? The function must show:</p>
<pre><code>Line 3
Line 4
Line 5
</code></pre>
<p>Thank you so much!</p>