Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site wu1.UUCP Path: utzoo!linus!philabs!cmcl2!rocky2!cubsvax!wu1!rf From: rf@wu1.UUCP Newsgroups: net.lang.c Subject: Re: Need Help: 1-char input in C Message-ID: <216@wu1.UUCP> Date: Tue, 27-Dec-83 18:43:14 EST Article-I.D.: wu1.216 Posted: Tue Dec 27 18:43:14 1983 Date-Received: Fri, 30-Dec-83 00:23:24 EST References: <464@ihuxn.UUCP>, <415@bbncca.ARPA> Organization: Western Union Telegraph, Mahwah, NJ Lines: 55 The following will work only if you controlling terminal is not initially in cbreak mode: system("stty cbreak"); c=getchar(); system("stty -cbreak"); Raw mode should be avoided, since it will sometimes return characters with whose values are either less than zero or greater than 127. A switch to raw mode may also cause the terminal to lose some output, since raw mode disables the XON/XOFF protocol. You probably want to enable cbreak mode when you begin your program and end it when you're done. The following less portable code is a hacked version of something which does work and is much faseter than system("stty . . .");. It probably works, but has not been tested. struct sgttyb otsgb; /* iniline -- initialize line This routine stores the terminal characteristics of stdin in otsgb and turns on cbreak mode. If stdin's file is not a terminal (perhaps for testing,) iniline does nothing. */ iniline() { struct sgttyb ntsgb; if (isatty(fileno(stdin))) { ioctl(fileno(stdin), TIOCGETP, &otsgb); ntsgb = otsgb; ntsgb.sg_flags |= CBREAK; ioctl(fileno(stdin), TIOCSETP, &ntsgb); } } /* resetline -- reset line characteristics This routine restores the terminal characteristics of stdin, which should be preserved in otsgb. If the line's file is not a terminal (perhaps for testing,) resetline does nothing. */ resetline() { if (isatty(fileno(stdin))) { ioctl(fileno(stdin), TIOCSETP, &otsgb); } } Randolph Fritz Western Union Telegraph