cfmakeraw - Linux
Overview
cfmakeraw is a utility command for POSIX operating systems used to set the terminal into raw mode. Raw mode disables line buffering, echo, and interpretation of special characters, allowing for direct and unprocessed input from the keyboard. This is particularly useful for interactive terminal programs like text editors, game terminals, and scripting languages that require direct character input and manipulation.
Syntax
cfmakeraw(int fd)
Options/Flags
None
Examples
-
Disable Echoing:
#include <termios.h> int main() { int fd = STDIN_FILENO; struct termios old_tio, new_tio; // Get current terminal settings tcgetattr(fd, &old_tio); // Set up new settings for raw mode new_tio = old_tio; new_tio.c_lflag &= ~ECHO; // Apply new settings tcsetattr(fd, TCSAFLUSH, &new_tio); // Use terminal in raw mode // ... // Restore previous settings before exit tcsetattr(fd, TCSAFLUSH, &old_tio); }
-
Game Terminal:
Create a simple game terminal using cfmakeraw to disable line buffering and echo, allowing instant keystrokes from the user.
Common Issues
Input Blocking:
- When using cfmakeraw, input reads may block until the user presses the enter key, as line buffering is disabled. This can be resolved by using non-blocking IO or setting a timeout on input reads.
Integration
Shell Scripts:
- cfmakeraw can be used in shell scripts to set terminals to raw mode before executing commands that require direct character input. This is especially useful for automated testing or data entry tasks.
Related Commands
- stty: Set terminal settings
- tcgetattr: Get terminal attributes
- tcsetattr: Set terminal attributes