stat(2), fstat(2), lstat(2)
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
int stat(const char *restrict pathname, struct stat *restrict statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *restrict pathname, struct stat *restrict statbuf);
/* 심볼릭 링크 파일 자체에 대한 정보를 반환함 */
/* return: 성공 → 0 / 에러 → -1 */
- 여기서 restrict pathname, restrict statbuf는 두 포인터가 서로 같은 메모리 영역을 참조하지 않음을 보장한다.
- stat 함수의 첫 번째 인자로 심볼릭 링크가 들어온다면, 그 심볼릭 링크가 가르키고 있는 파일에 대한 정보를 반환한다.
access(2)
#include <unistd.h>
int access(const char *pathname, int mode);
/* return: 성공 → 0 / 에러 → -1 */
value | description |
F_OK | tests for the existence of the file |
R_OK | whether the file exists and grants read permission |
W_OK | whether the file exists and grants write permission |
X_OK | whether the file exists and grants execute permission |
umask(2)
프로세스의 파일 생성 마스크를 생성하고 이전 값을 반환하는 시스템 호출 함수(2)이다.
* 파일에 mode에 있는 권한을 주지 않기 위해 사용된다.
* creat()의 mode & ~(umask()의 cmask)
#include <sys/types.h>
#include <sys/stat.h>
mode_t umask(mode_t cmask);
/* return: the previous value of the mask(이전의 파일 모드 생성 마스크) */
/* This system call always succeeds and the previous value of the mask is returned. */
chmod(2), fchmod(2)
기존 파일의 접근 권한을 변경하는 시스템 호출 함수(2)이다.
#include <sys/stat.h>
int chmod(const char *pathname, mode_t mode);
int fchmod(int filedes, mode_t mode);
/* return: 성공 → 0 / 실패 → -1 */
chown(2), fchown(2), lchown(2)
파일의 소유자 ID(uid)와 그룹 사용자 ID(gid)를 변경하는 시스템 호출 함수(2)이다.
#include <unistd.h>
int chown(const char *pathname, uid_t owner, gid_t group);
int fchown(int filedes, uid_t owner, gid_t group);
int lchown(const char *pathname, uid_t owner, gid_t group);
/* return: 성공 → 0 / 에러 → -1 */
truncate(2), ftruncate(2)
파일을 지정된 길이로 변경하는 시스템 호출 함수(2)이다.
* 변경이라고 한 이유는 대부분의 시스템에서 length가 원래 파일 크기보다 작은 경우에 파일의 크기를 늘리기 때문이다.
* 기존 EOF 부터 늘어난 크기까지 0으로 채워진다.
#include <unistd.h>
#include <sys/types.h>
int truncate(const char *filename, off_t length);
int ftruncate(int filedes, off_t length);
/* return: 성공 → 0 / 실패 → -1 */
link(2), unlink(2)
기존 파일에 대한 링크(심볼릭 링크가 아님)를 생성하는 시스템 호출 함수(2)이다.
#include <unistd.h>
int link(const char *existingpath, const char *newpath);
int unlink(const char *pathname);
/* return: 성공 → 0 / 실패 → -1 */
remove(3), rename(2)
remove()는 파일이나 디렉토리를 unlink 하는 라이브러리 함수(3)이다.
rename()은 파일의 이름을 변경하는 시스템 호출 함수(2)이다.
#include <stdio.h>
int remove(const char *pathname);
int rename(const char *oldname, const char *newname);
/* return: 성공 → 0 / 실패 → -1 */
symlink(2), readlink(2)
synlink()는 심볼릭 링크를 생성하는 시스템 호출 함수(2)이다.
readlink()는 심볼릭 링크를 읽는 시스템 호출 함수(2)이다.
#include <unistd.h>
int symlink(const char *actualpath, const char *sympath);
/* return: 성공 → 0 / 실패 → -1 */
ssize_t readlink(const char *pathname, char *buf, size_t bufsize);
/* return: 성공 → 읽은 바이트 수 / 실패 → -1 */
utime(2)
파일의 최종 접근 시간과 최종 변경 시간을 변경하는 시스템 호출 함수(2)이다.
#include <sys/types.h>
#include <utime.h>
int utime(const char *pathtime, const struct utimbuf *times);
/* return: 성공 → 0 / 에러 → -1 */
struct utimbuf{
time_t actime; // 접근 시간
time_t modtime; // 수정 시간
}
mkdir(2), rmdir(2)
mkdir()은 디렉토리를 생성하는 시스템 호출 함수(2)이다.
rmdir()은 디렉토리를 삭제하는 시스템 호출 함수(2)이다.
#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);
/* return: 성공 → 0 / 에러 → -1 */
#include <unistd.h>
int rmdir(const char *pathname);
/* return: 성공 → 0 / 에러 → -1 */
opendir(3), readdir(3), rewinddir(3), closedir(3), telldir(3), seekdir(3)
디렉토리의 내용을 보기 위한 라이브러리 함수(3)이다.
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
/* return: 성공 → 포인터 / 에러 → NULL */
#include <dirent.h>
struct dirent *readdir(DIR *dp);
/* return: 성공 → 포인터, 디렉토리 끝 → NULL / 에러 → NULL, errno 설정 */
#include <sys/types.h>
#include <dirent.h>
void rewinddir(DIR *dp);
int closedir(DIR *dp);
/* return: 성공 → 0 / 에러 → -1, errno 설정 */
#include <dirent.h>
long telldir(DIR *dp);
/* return: 성공 → dp에 해당하는 디렉토리 안에서 현재 위치 / 에러 → -1, errno 설정 */
#include <dirent.h>
void seekdir(DIR *dp, long loc);
struct dirent
(/usr/include/x86_64-linux-gnu/bits/dirent.h)
struct dirent
{
#ifndef __USE_FILE_OFFSET64
__ino_t d_ino;
__off_t d_off;
#else
__ino64_t d_ino;
__off64_t d_off;
#endif
unsigned short int d_reclen;
unsigned char d_type;
char d_name[256]; /* We must not include limits.h! */
};
DIR: directory stream
FILE: file stream
chdir(2), fchdir(2)
현재 프로세스의 작업 디렉토리를 변경하는 시스템 호출 함수(2)이다.
#include <unistd.h>
int chdir(const char *pathname);
int fchdir(int filedes);
/* return: On success → 0 / On error → -1, errno is set */
getcwd(2), get_current_dir_name(2)
현재 작업 디렉토리에 대한 전체 경로 이름을 반환하는 시스템 호출 함수(2)이다.
#include <unistd.h>
chat *getcwd(char *buf, size_t size);
char *get_current_dir_name(void);
/* On success → 작업 디렉토리의 pathname / On error → NULL, errno is set */
get_current_dir_name();
getcwd(NULL, 0);
/* 두 함수는 같은 기능을 수행한다 */
'HOME' 카테고리의 다른 글
[Linux] 커널 컴파일(build linux kernel) (1) (1) | 2025.05.08 |
---|---|
LinuxSP - Standard I/O Library: 함수 (0) | 2025.04.25 |
LinuxSP - Files and Directories: 개념 (0) | 2025.04.25 |
LinuxSP - FILE I/O: 함수 (0) | 2025.04.24 |
LinuxSP - FILE I/O: 개념 (0) | 2025.04.24 |