#include #include #include #include struct termios term; int echoing = 1, gotterm = 0, atexitted = 0; void set_echo(int which) { if (echoing == which) return; if (! gotterm) { int r = tcgetattr(0, & term); if (r < 0) perror("tcegtattr"); gotterm = 1; } if (which) term.c_lflag |= ECHO; else term.c_lflag &= ~ ECHO; int r = tcsetattr(0, 0, & term); if (r < 0) perror("tcsetattr"); echoing = which; } void echo_on(void) { set_echo(1); } void echo_off(void) { set_echo(0); if (! atexitted) { atexit(echo_on); atexitted = 1; } } int main(int argc, char *argv[]) { char line[1024]; while (1) { printf("> "); char * s = fgets(line, sizeof(line), stdin); if (s == NULL) { printf("\n"); break; } int end = strlen(line) - 1; while (end >= 0 && line[end] <= ' ') { line[end] = '\0'; end -= 1; } if (strcmp(line, "+") == 0) { echo_on(); printf("echo on\n"); } else if (strcmp(line, "-") == 0) { echo_off(); printf("echo off\n"); } else printf("You typed \"%s\"\n", line); } }