The script command makes a typescript of everything displayed on your terminal. This implementation demonstrates:
Docs available here https://42-course.github.io/ft_script/html/
Usage: ft_script [options] [file]
Options:
-a Append to output file instead of overwriting
-c <command> Execute command instead of interactive shell
-q Quiet mode (suppress start/done messages)
-e Return exit status of child process
-f Flush output after each write (BONUS)
-h Display help and exit
- Fork: Creates child process
- Setsid: Child creates new session (becomes session leader)
- Open slave: PTY slave becomes controlling terminal
- Redirect I/O: stdin/stdout/stderr → PTY slave
- Exec shell: Replace process image with shell
Uses select() to monitor multiple file descriptors:
- stdin → Read user input → Write to PTY master
- PTY master → Read child output → Write to stdout and file
WHAT IS A PTY?
A pseudo-terminal is a pair of character devices: - Master: Controlled by the application (our ft_script) - Slave: Used by the child process (the shell) Notes: man 2 open, man 4 tty cat /proc/sys/kernel/pty/nr (to see number of opened PTYs)WHY RAW MODE??
For script to work correctly, we need to pass every character immediately to the child without processing. If we stayed in cooked mode, line editing would happen twice (once in our terminal, once in the child's terminal), which would be confusing. c_iflag (input modes): - ICRNL: translate carriage return to newline on input - IXON: enable XON/XOFF flow control (Ctrl-S/Ctrl-Q) c_oflag (output modes): - OPOST: enable implementation-defined output processing (includes ONLCR: NL-to-CRNL translation) c_lflag (local modes): - ECHO: echo input characters back to terminal - ICANON: canonical (line-buffered) mode — read waits for newline - IEXTEN: implementation-defined input processing (e.g. Ctrl-V) - ISIG: generate signals on INTR/QUIT/SUSP (Ctrl-C, Ctrl-Z...) c_cc (special characters, noncanonical mode): - VMIN = 1: read() returns as soon as >= 1 byte is available - VTIME = 0: no timeout — read() blocks until data arrives (the four MIN/TIME combinations are explained in man 3 termios under "Noncanonical mode") (all flags documented in man 3 termios)Data written to the slave is presented on the master file descriptor as input. Data written to the master is presented to the slave as input. - from: man 7 pts
man 4 pts- Pseudoterminal master and slave (ptmx, pts)man 7 pty- Pseudoterminal Interfacesman 2 setsid- Creates a session and sets the process group IDman 1 script- Script command manualman 2 syscalls- Linux system callsman 3 posix_openpt- PTY creationman 2 select- I/O multiplexingman 3 termios- Terminal attributesman 7 credentials- Process Identifiers