Catch ctrl+d in C

Hello,
I am programming some kind of shell in special distribution of Linux. My trouble comes with programming the sort function. It should work the same like in the standard shell. When you terminate input, there's need to put End Of Transmission character, which is CTRL+D. But I am not able to do it right. I tried this:

        while (!feof(stdin)) {
            fgets(buf, MAXBUF, stdin);

            AddToArray(buf, strlen(buf) + 1);
            i++;
        }

but if the ctrl+d didn't come right after new line (enter), i need to press it twice. So it doesn't work right. Please, would you have any advice?
Thank you very much!
Samuel

I recently discovered the same thing myself. apparently it's supposed to work that way and changing that probably means messing with terminal settings. But even a normal shell does it: Run cat and type abcd^D. So I think it's just normal behavior behaving as normally does.

Page 45 of Unix Programming Environment has an explanation of this.