<nitpick> This isn't a valid Prototype in ISO C, because the compiler can't distinguish between an empty parameter list and K&R definitions. So it should be void GetOne(void) </nitpick> In any case, I decided to make a slight change to the function. It would be more useful for the function to take the file descriptor as the
On Sat, 31 May 2003 05:49:51 +0200 Philipp Thomas <philipp.thomas@t-link.de> wrote: parameter where the original function assumed that stdin was the input device. This function can be applied to any arbitrary input terminal device. Note I added the tcflush(3) function. Also, note that there is no error checking. ------------------------------------------ #include <stdio.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <termios.h> void GetOne(int fd) { int rv; char ch; struct termios oldflags, newflags; /*********** Reset the terminal to accept unbuffered input ***/ /* get the current oldflags */ tcgetattr(fd, &oldflags); /* make a copy of the flags so we can easily restore them */ newflags = oldflags; /* set raw input */ newflags.c_cflag |= (CLOCAL | CREAD); newflags.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* set the newflags */ tcsetattr(fd, TCSANOW, &newflags); rv = read(fd, &ch, 1); /* Flush the input queue. This takes into account the case * where an F button is pressed */ tcflush(fd, TCIFLUSH); /* restore the oldflags -- This is important otherwise * your terminal will no longer echo characters */ tcsetattr(fd, TCSANOW, &oldflags); } int main(void) { printf("Wait for a char: "); fflush(stdout); GetOne(fileno(stdin)); /* do this after resetting the terminal because * stdin and stdout should be the same pseudo terminal */ printf("\n"); /* simply return back to beginning of line */ return 0; } --------------------------------------------------- -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9