Created
August 19, 2010 13:21
-
-
Save betawaffle/537853 to your computer and use it in GitHub Desktop.
Terminal Stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <termios.h> | |
int | |
main(void) | |
{ | |
struct termios iattr; | |
int ret; | |
if (ret = tcgetattr(STDIN_FILENO, &iattr), ret < 0) { | |
switch (errno) { | |
case EBADF: | |
printf("Standard input is closed.\n"); | |
case ENOTTY: | |
printf("Standard input is not a terminal.\n"); | |
} | |
goto fail; | |
} | |
iattr.c_lflag &= ~(ICANON | ECHO); | |
attr: ret = tcsetattr(STDIN_FILENO, TCSANOW, &iattr); | |
if (ret < 0) { | |
switch (errno) { | |
case EINTR: | |
goto attr; | |
case EINVAL: | |
printf("Invalid argument passed to tcsetattr().\n"); | |
case EBADF: | |
printf("Standard input is closed.\n"); | |
case ENOTTY: | |
printf("Standard input is not a terminal.\n"); | |
case EIO: | |
printf("IO Error!\n"); | |
} | |
goto fail; | |
} | |
loop: ret = getchar(); | |
if (ret == EOF) { | |
goto exit; | |
} | |
printf("(%c)", (char) ret % 128); | |
goto loop; | |
exit: | |
return EXIT_SUCCESS; | |
fail: | |
return EXIT_FAILURE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment