Skip to content

Instantly share code, notes, and snippets.

@betawaffle
Created August 19, 2010 13:21
Show Gist options
  • Save betawaffle/537853 to your computer and use it in GitHub Desktop.
Save betawaffle/537853 to your computer and use it in GitHub Desktop.
Terminal Stuff
#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