리눅스 fcntl()와 redirection관련 해석부탁드립니다ㅠ.ㅠ';
#include < stdio.h >
#include < stdlib.h >
#include < fcntl.h >
#include < sys/stat.h >
int main(int argc, char *argv[])
{
int val;
if(argc != 2)
{
printf("2argu!\n");
exit(1);
}
printf("fd : %d\n\n",atoi(argv[1]));
if( (val = fcntl(atoi(argv[1]), F_GETFL, 0)) < 0 )
{
printf("fcntl error for fd %d",atoi(argv[1]));
exit(1);
}
switch(val & O_ACCMODE)
{
case O_RDONLY:
printf("read only");
break;
case O_WRONLY:
printf("write only");
break;
case O_RDWR:
printf("read write");
break;
default:
printf("unknown access mode");
exit(1);
}
if(val & O_APPEND)
printf(", append");
if(val & O_NONBLOCK)
printf(", nonblocking");
#if defined(O_SYNC)
if(val & O_SYNC)
printf(", sysnchronous writes");
#endif
#if !defined(_POSIX_C_SOURCE)&&defined(O_FSYNC)
if(val&O_FSYNC)
printf(", sysnchronous writes");
#endif
putchar('\n');
exit(0);
}
-------------------------------실행결과----------------------------------
root@com:/# ./a.out 0 < /dev/tty
read only
root@com:/# ./a.out 1 > temp.foo
root@com:/# cat temp.foo
write only
root@com:/# ./a.out 2 2>>temp.foo
write only, append
root@com:/# ./a.out 5 5<>temp.foo
read write
------------------------------------------------------------------------
안녕하세요 리눅스 초보입니다..ㅎㅎ;
fcntl함수 사용에 대한 기본예제입니다.
코드는 해석은 문제없는데, 실행 결과가 이해가 되질 않습니다ㅠ.ㅠ
리다이렉션 개념에 대해 찾아봤는데, 이 예제를 통해 적용해보려니 이해가 되지 않는군요..
실행 명령과 왜 저런 결과가 출력되는지 해석해주실분 계신가요 :'(
댓글 달기