tags:

views:

95

answers:

2

I am coding a terminal like Linux terminal with C under Linux OS and I need to quit program when the user presses ctrl+D keywords. But I don't know how to understand that the user pressed these keywords. Thanks for your helping.

I'm getting inputs with fgets()

+3  A: 

Ctrl+D is end-of-file. In that case, fgets() will return a null pointer.

So, your program's main loop can probably look something like this:

char buffer[2000];
const char* input = fgets(buffer, sizeof(buffer), stdin);
while (input) {
    do_something_with(input);
    input = fgets(buffer, sizeof(buffer), stdin);
}

Note that this only works for simple buffered input. For information on lower-level keyboard handling, check out http://tldp.org/HOWTO/Keyboard-and-Console-HOWTO.html

Kristopher Johnson
Yes CTRL + D is finishing my program. my problem is solved temporarily. but I think I will need keyword controls in future. It maybe CTRL+{A,B,...}. Can I understand the user's actions on keyboard.
FTI
Ctrl-D is not end-of-file. Ctrl-D is only the default setting of the VEOF c_cc array member of the `termios` structure, and VEOF is not end-of-file but pending-tty-buffer instead.
sambowry
+2  A: 
sambowry