blog
October 03, 2021
unix_lecture_notes/chapter_01: http://www.compsci.hunter.cuny.edu/~sweiss/course_materials/unix_lecture_notes/chapter_01.pdf
c 로 프로그램을 작성할 때 컴퓨터의 다양한 요소가 어떻게 작동하는지를 전반적으로 폭넓게 차근차근 설명해주는 좋은 자료를 발견했다!
그런데 여기서 예제를 발견했다.
ioctl(tty_fd, TIOCGWINSZ, &window_structure)
struct winsize
type 으로 선언하면 된다고 한다.#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
void a005()
{
int tty;
struct winsize window_structure;
tty = open("/dev/tty", O_RDONLY);
ioctl(tty, TIOCGWINSZ, &window_structure);
printf("row: %d, col: %d \n", window_structure.ws_row, window_structure.ws_col);
}
struct winsize
type 에 멤버 변수로 ws_row
와 ws_col
이 있다고 한다.%d
로 출력해보았더니 잘 출력이 되었다.struct winsize
type 을 include/
에서 찾아보았다.sys/ttycom.h
에 구현되어 있었다.struct winsize {
unsigned short ws_row;
unsigned short ws_col;
unsigned short ws_xpixel;
unsigned short ws_ypixel;
};
#include <stdio.h>
int main()
{
printf("Hello, World! \n");
return (0);
}
xxd -a a.out
로 실행파일을 보고 “Hello, World \n” 를 육안으로 찾아보았다.xxd -s 0x3f9a -l 15 a.out
로 해당 위치에 15 byte 를 출력해보니 확실하게 “Hello, World! \n” 가 들어있는 것을 볼 수 있었다.echo "3f9a:<바꿀 byte data>" | xxd -r - a.out
명령을 통해 값을 바꿀 것이기 때문에, 우선 바꿀 byte data 를 구해야 한다.echo "world. hello?" | xxd
로 byte data 를 구했다.echo "3f9a:776f 726c 642e 2068 656c 6c6f 3f" | xxd -r - a.out
를 하여 문자열을 바꿨다.xxd -s 0x3f9a -l 15 a.out
으로 해당 위치의 데이터를 다시 출력해보니 제대로 바뀐 것을 볼 수 있었다.% ./a.out
world. hello?
%