Most file I/O on a UNIX system can be preformed using only five functions:
open
,read
,write
,lseek
, andclose
.
STDIN_FILENO
, STDOUT_FILENO
, STDERR_FILENO
)로 바꾸어 쓸 수 있다.OPEN_MAX - 1
까지의 값이 가능하다.open
, openat
함수파일을 열기 위해 이 함수들을 사용한다.
#include <fcntl.h>
int open(const char *path, int oflag, ... /* mode_t mode */ );
int openat(int fd, const char *path, int oflag, ... /* mode_t mode */ );
/* Both return: file descriptor if OK, -1 on error */
path
— 열거나 생성할 파일의 이름. open
의 경우 파일의 전체 패스, openat
의 경우 상대 경로.
oflag
— 파일 열기 옵션. 다음과 같은 옵션들이 <fcntl.h>
에 정의되어 있다. 아래 다섯 옵션은 반드시 배타적으로 하나만 지정되어야 하는 옵션이다.
O_RDONLY |
읽기 전용으로 연다(대체로 0). |
---|---|
O_WRONLY |
쓰기 전용으로 연다(대체로 1). |
O_RDWR |
읽기/쓰기 겸용으로 연다(대체로 2). |
O_EXEC |
실행 전용으로 연다. |
O_SEARCH |
찾기 전용으로 연다(디렉토리에 적용). |
O_SEARCH
에 관하여그 외의 다른 옵션들은 선택적이다. 모든 목록을 확인하려면 open(2)
를 참고하라.
다중 인자(...
) — open
과 openat
에 쓰이는 다중 인자는 mode
인수만을 위해 사용된다.
mode
— 파일이 새로 생성될 때만 적용된다. 이 mode
는 새로 만들어지는 파일의 권한 비트를 생성하기 위해 사용되며, mode & ~umask
계산을 통해 적용된다.
fd
— openat
의 경우 상대경로를 받는데, fd
는 상대경로 검색을 시작하는 파일을 가리킨다. 만약 fd
가 AT_FDCWD
상수로 지정되어있다면 현재 경로를 기준으로 상대경로를 검색한다.
openat
함수의 의의openat
을 비롯한 *at
함수들은 POSIX.1 표준에 비교적 최근 반영되었다. 이 함수들은 기존의 POSIX 함수들의 다음과 같은 문제를 해결할 수 있었다.