struct stat{
dev_t st_dev; //device
ino_t st_ino; //inode number
mode_t st_mode; //protection
nlink_t st_nlink; //number of hard link
uid_t st_uid; //user id
gid_t st_gid; //group id
dev_t st_rdev; //devive type
size_t st_size; //total size, bytes
size_t st_blksize; //blocksize
blkcnt_t st_blocks; //number of blocks allocated
time_t st_atime; //time of last access
time_t st_mtime; //time of last modification
time_t st_ctime; //time of last change
}
stat() 함수
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int stat(const char * pathname, struct stat * buf);
설명
유효한 파일인 경우에 파일의 정보를 읽고, 이를 메모리 공간에 저장함
파일이 심볼릭 링크인 경우에, 링크가 가리키는 원본 파일의 정보를 읽음
인자
pathname : 파일의 이름 또는 경로
buf : 파일의 정보를 저장할 메모리 공간에 대한 포인터
반환
성공 시 0
오류시 -1
lstat() 함수
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int lstat(const char * pathname, struct stat * buf);
설명
유효한 파일인 경우에 파일의 정보를 읽고, 이를 메모리 공간에 저장함
파일이 심볼릭 링크인 경우에, 링크가 가리키는 원본 파일의 정보를 읽음
인자
pathname : 파일의 이름 또는 경로
buf : 파일의 정보를 저장할 메모리 공간에 대한 포인터
반환
성공 시 0
오류시 -1
fstat() 함수
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int fstat(int fd, struct stat * buf);
설명
유효한 파일인 경우에 파일의 정보를 읽고, 이를 메모리 공간에 저장함
파일이 심볼릭 링크인 경우에, 링크가 가리키는 원본 파일의 정보를 읽음
인자
fd : 파일 지시자
open()을 이용하여 파일에 먼저 접근하고, 반환된 파일지시자 이용
buf : 파일의 정보를 저장할 메모리 공간에 대한 포인터
반환
성공시 0
오류시 -1
링크
파일 또는 디렉토리를 직접 복사하지 않고, 파일 또는 디렉토리를 가리키는 것을 의미
하드 링크
원본 파일과 동일한 inode를 가지는 링크
원본파일이 사라지더라도, 하드링크를 통해 접근이 가능
심볼릭 링크
원본파일을 가리키는 포인터
원본파일과 다른 inode를 가짐
원본 파일이 사라지면, 심볼릭 링크를 통해 접근이 불가능함
stat()와 lstat() 함수 예시
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main(void) {
int r = 0;
char *pathname = "/usr/bin/vi";
struct stat buf1;
struct stat buf2;
r = stat(pathname, &buf1);
if (r == -1) {
perror("stat() error");
exit(-1);
}
r = lstat(pathname, &buf2);
if (r == -1) {
perror("lstat() error");
exit(-1);
}
printf("Original file size : %ld\\n", buf1.st_size);
printf("Symbolic link file size : %ld\\n", buf2.st_size);
}
#include <pwd.h>
#include <sys/types.h>
struct passwd{
char *pw_name; //user login name
char *pw_passwd; //encrypted password
uid_t pw_uid; // user id
gid_t pw_gid; // group id
time_t pw_change; //passwd change time
char *pw_class; // user acess class
char *pw_gecos; // user full name
char *pw_dir; // user login dir
char *pw_shell; // user login shell
time_t pw_expire; // passwd expiration time
}
#include <fcntl.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define MAX_BUF_SIZE 16
#define PERMS 0644
void fileType(const struct stat *fileinfo) {
if (S_ISREG(fileinfo->st_mode)) {
printf("Regular file");
} else if (S_ISDIR(fileinfo->st_mode))
printf("Directory");
else if (S_ISCHR(fileinfo->st_mode))
printf("Character device");
else if (S_ISSOCK(fileinfo->st_mode))
printf("Socket");
else if (S_ISFIFO(fileinfo->st_mode))
printf("FIFO");
else if (S_ISLNK(fileinfo->st_mode))
printf("Symbolic link");
else if (S_ISBLK(fileinfo->st_mode))
printf("Block device");
else if (S_TYPEISMQ(fileinfo))
printf("Message Queue");
else if (S_TYPEISSHM(fileinfo))
printf("Shared memory");
else if (S_TYPEISSEM(fileinfo))
printf("semaphore");
}
void fileMode(const struct stat *fileinfo) {
if (S_IRUSR & fileinfo->st_mode) {
printf("r");
} else {
printf("-");
}
if (S_IWUSR & fileinfo->st_mode) {
printf("w");
} else {
printf("-");
}
if (S_IXUSR & fileinfo->st_mode) {
printf("x");
} else {
printf("-");
}
if (S_IRGRP & fileinfo->st_mode) {
printf("r");
} else {
printf("-");
}
if (S_IWGRP & fileinfo->st_mode) {
printf("w");
} else {
printf("-");
}
if (S_IXGRP & fileinfo->st_mode) {
printf("x");
} else {
printf("-");
}
if (S_IROTH & fileinfo->st_mode) {
printf("r");
} else {
printf("-");
}
if (S_IWOTH & fileinfo->st_mode) {
printf("w");
} else {
printf("-");
}
if (S_IXOTH & fileinfo->st_mode) {
printf("x");
} else {
printf("-");
}
}
int main(int argc, char const *argv[]) {
struct stat fileinfo;
struct passwd *userinfo;
if (argc != 2) {
printf("Usage : %s [pathname]\\n", argv[0]);
exit(-1);
}
printf("File name or path : %s\\n", argv[1]);
if (stat(argv[1], &fileinfo) == -1) {
perror("stat() error");
exit(-1);
}
printf("File type : ");
fileType(&fileinfo);
printf("File permission : ");
fileMode(&fileinfo);
printf("\\nFile size: %ld\\n", fileinfo.st_size);
userinfo = getpwuid(fileinfo.st_uid);
printf("Owner name : %s\\n", userinfo->pw_name);
return 0;
}
디렉토리
UNIX 계열의 운영체제에서 디렉토리는 파일의 한 종류임
디릭토리는 파일의 목록을 저장하는 파일
파일을 포함하여 하위 디렉토리 또한 포함할 수 있음
디렉토리는 creat() 또는 open() 함수를 이용하여 생성이 불가능함
구조적으로 디렉토리는 일련의 디렉토리 엔트리로 구성됨
각 엔트리는 디렉토리에 포함되어 있는 파일 또는 디렉토리임
각 엔트리는 파일의 inode번호와 파일의 읾을 저장하는 문자 필드로 구성됨
struct dirent{}
#include <dirent.h>
struct dirent{
ino_t d_ino; //inode number of entry
unsigned int d_reclen; //length of this record
unsigned int d_type; // file type
unsigned int d_namlen; //length if string in d_name
char d_name{MAXNAMLEN +1]; //file name
}
chdir() 함수
#include <unistd.h>
int chdir(const char *pathname);