Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************************
- * Terminal I/O *
- * Normally, on Unix, ctrl-C refers to "cancel" and ctrl-D refers to EOF. *
- * What if we want to change that? This program will change the default *
- * keys to something else using <termios.h> defined in POSIX.1 standard. *
- * ** Note: this program can't run on ideone.com as we have to enter input *
- * interactively. I presume that ideone.com only take input in HTML form *
- * prior to running the program. So I use pastebin.com, instead. *
- * You can still copy the code and paste it on Unix terminal to test it. *
- ****************************************************************************/
- #include <unistd.h> /* isatty(3), fpathconf(2) */
- #include <termios.h> /* tcgetattr(3), tcsetattr(3) */
- #include <stdio.h>
- #include <stdlib.h> /* exit(3) */
- int main(void)
- {
- struct termios newterm, oldterm; /* Declare new and old terminal. */
- long vdisable;
- /* Check if stdin is a terminal (tty) using isatty(3). *
- * isatty stands for "Is A TTY?". *
- * Note: stdin is a (FILE *) and STDIN_FILENO is a file descriptor. *
- * But both refer to the same thing. */
- if (isatty(STDIN_FILENO) == 0) {
- fprintf(stderr, "standard input is not a terminal device");
- exit(EXIT_FAILURE);
- }
- /* pathconf or fpathconf family is used to get configurable pathname *
- * variables. It returns the current variable value. *
- * - pathconf(2) takes (FILE *) as the first argument. *
- * - fpathconf(2) takes a file descriptor as the first argument. *
- * The following lines of code, we are trying to get terminal character *
- * disabling value (_PC_VDISABLE). We can also get the others value, *
- * see fpathconf(2). The following lines, we call fpathconf(2) and set *
- * char disabling value to vdisable. */
- if ((vdisable = fpathconf(STDIN_FILENO, _PC_VDISABLE)) == -1) {
- fprintf(stderr, "fpathconf error or _POSIX_VDISABLE not in effect");
- exit(EXIT_FAILURE);
- }
- /* tcgetattr stands for Terminal Control Get Attribute. We get the *
- * current attribute of STDIN_FILENO (stdin) and store it in 'oldterm'. */
- if (tcgetattr(STDIN_FILENO, &oldterm) == -1) {
- fprintf(stderr, "tcgetattr");
- exit(EXIT_FAILURE);
- }
- newterm = oldterm; /* Copy "oldterm" to "newterm". */
- newterm.c_cc[VINTR] = vdisable; /* Disable INTR character in "newterm". */
- newterm.c_cc[VEOF] = 2; /* EOF is now ctrl-B in "newterm". */
- /* We can find c_cc index in /usr/include/termios.h . */
- /* We now set the attribute of our modified "newterm" to STDIN_FILENO. *
- * For details for the 2nd arg, consult man pages tcsetattr(3). *
- * This example, we want it to take into effect immediately. So we use *
- * TCSANOW. */
- if (tcsetattr(STDIN_FILENO, TCSANOW, &newterm) == -1) {
- fprintf(stderr, "tcsetattr");
- exit(EXIT_FAILURE);
- }
- printf("try repeating ^C, ^D then close with ^B or <enter>:");
- getchar();
- /* We now restore "oldterm", which we previously saved, back to normal *
- * attribute, prior to end of program. */
- if (tcsetattr(STDIN_FILENO, TCSANOW, &oldterm) == -1) {
- fprintf(stderr, "tcsetattr");
- exit(EXIT_FAILURE);
- }
- fputs("\n", stdout);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement