tty

윤강훈·2024년 10월 17일

System Programming

목록 보기
8/12

Device

  • Unix 시스템에서 모든 디바이스(sound card, terminal, mouse 등)는 파일로 처리
  • 각 디바이스들은 파일명을 가짐
  • /dev 디렉토리에 각종 디바이스 파일이 존재
  • 파일이나 디바이스에 동일한 system call 사용
  • 디바이스 파일은 디바이스와의 연결을 의미 (저장공간이 아님)
  • 디바이스의 inode는 device driver에 대한 pointer를 저장

tty(teletypewriter)

  • tty: 현재 접속한 터미널의 이름을 출력
  • 8: inode number
  • mode's c: Character Device(c)
  • /dev/pts/5: filename
$ who > /dev/pts/1

-> who의 출력 결과가 /dev/pts/1에 출력됨

tcgetattr

  • 터미널 속성 가져오기
    - fd에 연결된 객체의 속성값을 가져옴
  • struct
struct termios
{
  tcflag_t c_iflag; /* input mode flags */
  tcflag_t c_oflag; /* output mode flags */
  tcflag_t c_cflag; /* control mode flags */
  tcflag_t c_lflag; /* local mode flags */
  cc_t c_cc[NCCS]; /* control charters */
  speed_t c_ispeed; /* input speed */
  speed_t c_ospeed; /* output speed */
};

ioctl

  • 입출력 장치 제어 함수
  • 디바이스 정보를 읽어오거나, 디바이스의 값을 변경

TIOCGWINSZ

  • Terminal Input Output Control Get WindowSize
#include <stdio.h>
#include <sys/ioctl.h>

int main(){
	struct winsize wbuf;
    
    if (ioctl(0, TIOCGWINSZ, &wbuf) != -1){
    	printf("%d rows x %d cols\n", wbuf.ws_row, wbuf.ws_col);
    	printf("%d wide x %d tall\n", wbuf.ws_xpixel, wbuf.ws_ypixel);
    }
    return 0;
}
  • result (일부 환경에서는 wide와 tall은 출력 안됨)
profile
Just do it.

0개의 댓글