Created
February 27, 2026 20:41
-
-
Save zsteva/4bf1f6088d7b91de6ee76ab1eb0fa396 to your computer and use it in GitHub Desktop.
stdio workaround for tty buffer line limit.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <termios.h> | |
| #include <unistd.h> | |
| #include <string.h> | |
| /* | |
| claude haiku 4.5 prompt: | |
| make me C program with do: | |
| - save termios settings of current terminal | |
| - reset icanon on terminal settings, and vmin to 1 | |
| - read stdin and write to stdout, until get 0x04 character | |
| - restore termios settings and finish executing | |
| */ | |
| int main(void) { | |
| struct termios original_settings, new_settings; | |
| int exit_code = 0; | |
| /* Save original terminal settings */ | |
| if (tcgetattr(STDIN_FILENO, &original_settings) == -1) { | |
| perror("tcgetattr"); | |
| return 1; | |
| } | |
| /* Copy settings for modification */ | |
| new_settings = original_settings; | |
| /* Disable canonical mode (ICANON) and set VMIN to 1 */ | |
| new_settings.c_lflag &= ~ICANON; /* Reset ICANON flag */ | |
| new_settings.c_cc[VMIN] = 1; /* Set minimum characters to read to 1 */ | |
| /* Apply new settings */ | |
| if (tcsetattr(STDIN_FILENO, TCSANOW, &new_settings) == -1) { | |
| perror("tcsetattr"); | |
| exit_code = 1; | |
| goto restore; | |
| } | |
| /* Read from stdin and write to stdout until EOF (0x04) */ | |
| unsigned char ch; | |
| ssize_t bytes_read; | |
| while ((bytes_read = read(STDIN_FILENO, &ch, 1)) > 0) { | |
| if (ch == 0x04) { /* EOF character (Ctrl+D) */ | |
| break; | |
| } | |
| if (write(STDOUT_FILENO, &ch, 1) == -1) { | |
| perror("write"); | |
| exit_code = 1; | |
| break; | |
| } | |
| } | |
| if (bytes_read == -1) { | |
| perror("read"); | |
| exit_code = 1; | |
| } | |
| restore: | |
| /* Restore original terminal settings */ | |
| if (tcsetattr(STDIN_FILENO, TCSANOW, &original_settings) == -1) { | |
| perror("tcsetattr restore"); | |
| return 1; | |
| } | |
| return exit_code; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment