1

I have been trying to figure out how it is possible to change the terminal's mode from "cooked" to "raw" using C and in a Git Bash terminal.

I have tried to execute the program with winpty and that makes that certain part of the program function as expected. However, it messes up the other parts, like clearing the screen with the control sequence \x1b[2J, and vice versa. Clearing the screen works perfectly when I am NOT executing the program with winpty.

I have also tried using system("clear") and system("cls") but with no luck. I have also tried to set the mode using the function _setmode(_fileno(stdin), _O_BINARY) but no such function seems to be working with Git Bash and more specifically MinTTY.

When I am using getch() nothing simply happens and I have to use CTRL+C to exit the program. However, it should exit the program when I press the 'i' character.

The code looks like this:

#include <windows.h>
#include <conio.h>
#include <stdio.h>

void clear_screen() {
    printf("\\033\[2J");
    printf("\\033\[1;1H");
    fflush(stdout);
}

int main() {
#ifdef _WIN32
    clear_screen();
    while (1) {
        char c = _getch();
        if (c == 'i') {
            exit(0);
        }
    }
#endif

#ifdef linux
#endif
}
ilittlebig
  • 21
  • 3
  • "When I am using getch() nothing simply happens " --> Exactly what input did you use? – chux - Reinstate Monica Dec 31 '22 at 06:09
  • 1
    The escape is `\033[2J"` (etc...) Including the additional backslashes likely results in unwanted output. If you have a git-bash terminal, what are you using to compile the code? (that may also bear on the additional slash issue) Is your mintty terminal from git bash, or do you have Msys64 installed? Providing a bit more about your setup will help us help you. – David C. Rankin Dec 31 '22 at 08:42
  • I don’t have anything except for the git-bash terminal that follows when you install git. I am using `gcc main.c -o main` to compile the code. I do not have Msys64 installed either. – ilittlebig Dec 31 '22 at 14:16
  • ”When I use getch() nothing simply happens” - I am using the character 'i' as the only input as it shall exit the program when I press it. – ilittlebig Dec 31 '22 at 14:18
  • Well, you would have had to install gcc somehow -- it's not part of git-bash. On windows, the only "windows aware" gcc is mingw (MinGW64 is quite good, the install with Msys2 is exceptionally good providing Archlinux's pacman package manager allowing it all to be kept up to date). So show us the output of `gcc --version`. – David C. Rankin Jan 03 '23 at 04:27

0 Answers0