//foo.c
#include <stdio.h>
void foo() {
char id[5];
printf("enter id\n");
scanf("%s",id);
printf("you entered %s \n",id);
}
int main() {
foo();
printf("program ends here\n");
return 0;
}
//inp-write.c
#include <stdio.h>
int main() {
int i;
char buf[256];
buf[0]='a';
buf[1]='b';
buf[2]='c';
buf[3]='d';
buf[4]='e';
buf[5]='f';
buf[6]='g';
buf[7]='h';
buf[8]='i';
buf[9]=0x6f;
buf[10]=0x84;
buf[11]=0x04;
buf[12]=0x08;
write(1,buf,13);
return 0;
}
> gcc -o foo foo.c
>gcc -o inp-write inp-write.c
>./inp-write > inp
> ./foo < inp
출력화면..
enter id
you entered abcdefghio
enter id
you entered abcdefghit
입력을 inp-write 로 buf에 배열로 받아서 < or > 로 redirct해서 inp에 넣고
foo를 실행시킬때 키보드 입력대신 inp에서 받아서 출력하는 예제입니다.
그런데 inp-write에서 buf에 위와 같이 넣으면
위에 보신바와 같이 출력이 두번 됩니다.
이건 왜그런걸까요?
뒤에
buf[9]=0x6f; buf[10]=0x84; buf[11]=0x04; buf[12]=0x08;
때문인거 같긴 한데요..(아스키코드 찾아보니 6f ='o' , 84는 없고,,,, 04=EOT, 08=BS라고 되어있네요..)
근데 저거 위치를 바꾸면 되질 않는데 그 이유는 무얼까요?;;;
(( buf[8]=0x6f; buf[9]=0x84; buf[10]=0x04; buf[11]=0x08; ) 또는 buf위치를 하나씩 크게 해줘도 마찬가지 입니다.)
그리고 덤으로... 어딜봐서 어셈블리랑 상관이 있는 질문임?