Why is getch not portable?

What makes getch inherently unsportsmanlike to be included as a standard C function?

It is so intuitive and elegant for console interfaces. Without this, a single character request is always misleading because users are allowed to enter more than one key.

Worse, you often need to clear the standard input after reading the input to the console, which is not even provided as a standard function! I have to use my own!

A simple program that can be:

#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <conio.h> int main(int argc, char **argv) { while (1) { char yes_or_no; printf("is this statement correct? 1 + 1 = 2(Y/N) $ "); yes_or_no = tolower(getch()); switch (yes_or_no) { case 'y': puts("Right!"); goto done; case 'n': puts("\nhint> Please just say yes for the sake of this demo..."); break; case 'q': puts("\nExitting."); goto done; case EOF: puts("\nEOF."); goto done; default: printf("\nunknown response '%c'.\n", yes_or_no); break; } } done: return 0; } 

becomes:

 #include <stdio.h> #include <stdlib.h> #include <ctype.h> static inline void flush_stdin() { char ch; do { ch = getchar(); } while ((ch != '\n') && (ch != EOF)); } int main(int argc, char **argv) { while (1) { char yes_or_no; printf("is this statement correct? 1 + 1 = 2(Y/N) $ "); yes_or_no = tolower(getchar()); switch (yes_or_no) { case 'y': puts("Right!"); goto done; case 'n': puts("hint> Please just say yes for the sake of this demo..."); break; case EOF: puts("EOF."); goto done; default: printf("unknown response '%c'.\n", yes_or_no); break; } flush_stdin(); /* remove this to see the difference */ } done: return 0; } 

Every time I want to create a simple console console program, I feel compelled to create a bunch of such functions, and still get everything I need, for example, getch.

Of course, you could use curses, but curses capture your entire console and make your program behave differently than the user usually expects (so that the program simply scrolls and still shows the command in which you started the program in the console buffer).

I know “why” is a bad question (which should always be preferable), but it needs to be asked. Is there something inherently integral to getch that the desktop system cannot support? If this does not happen, I can just write my own and transfer it to all the platforms that I want to support, and I'm good.

If there is something getch that cannot be supported by the desktop, what is it? Therefore, I have more reason to understand why conio is avoided than "just using ncurses / conio is not standard."

+5
source share
1 answer

There is no concept of terminals or Windows in the C standard. Everything related to these things is implementation-specific and cannot be part of the C programming language.

However, there are industry standards for terminal programming. One of them is part of POSIX (IEEE 1003.1) as the termios terminal driver interface, one of which is X / Open curses, which defines a library of C functions for controlling the terminal at a higher level, and the third is ISO 6429, a set of ANSI terminal sequences.

By the way, X / Open curses provides a getch() function. Microsoft Windows also supports these standards, but is not a useful way.

The way you use getch() on Windows (i.e., via conio.h ) is not portable, as conio.h is a special DOS header that cannot be easily implemented on other platforms due to the different model of how the console works in DOS and terminals on other platforms.

+5
source

Source: https://habr.com/ru/post/1246972/


All Articles