파일(:12)의 상태를 얻어온다.
#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> int stat(const char *file_name, struct stat *buf); int fstat(int filedes, struct stat *buf); int lstat(const char *file_name, struct stat *buf); |
stat() 함수를 이용하면 파일의 상태를 알아올수 있다. 첫번째 인자로 주어진 file_name 의 상태를 얻어와서 두번째 인자인 buf 에 채워 넣는다.
lstat() 함수는 심볼릭:::링크(:12)파일의 원본파일의 상태를 얻어온다는 것을 제외하고는 stat() 함수와 동일하다.
fstat() 는 open(2) 등을 통해서 만들어진 파일지시자를 인자로 받아들인다는 점 외에는 stat() 와 동일한 일을 수행한다.
이들 함수는 성공적으로 수행될경우 파일의 정보를 stat구조체에 복사한다. stat구조체는 다음과 같이 정의되어 있다.
struct stat {
dev_t st_dev; /* device */
ino_t st_ino; /* inode */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device type (if inode device) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for filesystem I/O */
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 */
};
|
잘못된 파일 지정자
파일이름을 찾기 위한 경로의 구성요수중 존재하지 않는 경로가 있을 경우
읽기 권한이 없다.
구성요소중 디렉토리(:12)가 아닌게 있다.
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <pwd.h>
#include <grp.h>
int main(int argc, char **argv)
{
int return_stat;
char *file_name;
struct stat file_info;
struct passwd *my_passwd;
struct group *my_group;
mode_t file_mode;
if (argc != 2 )
{
printf("Usage : ./file_info [file name]\n");
exit(0);
}
file_name = argv[1];
if ((return_stat = stat(file_name, &file_info)) == -1)
{
perror("Error : ");
exit(0);
}
file_mode = file_info.st_mode;
printf("파일이름 : %s\n", file_name);
printf("=======================================\n");
printf("파일 타입 : ");
if (S_ISREG(file_mode))
{
printf("정규파일\n");
}
else if (S_ISLNK(file_mode))
{
printf("심볼릭 링크\n");
}
else if (S_ISDIR(file_mode))
{
printf("디렉토리\n");
}
else if (S_ISCHR(file_mode))
{
printf("문자 디바이스\n");
}
else if (S_ISBLK(file_mode))
{
printf("블럭 디바이스\n");
}
else if (S_ISFIFO(file_mode))
{
printf("FIFO\n");
}
else if (S_ISSOCK(file_mode))
{
printf("소켓\n");
}
my_passwd = getpwuid(file_info.st_uid);
my_group = getgrgid(file_info.st_gid);
printf("OWNER : %s\n", my_passwd->pw_name);
printf("GROUP : %s\n", my_group->gr_name);
printf("FILE SIZE IS : %d\n", file_info.st_size);
printf("마지막 읽은 시간 : %d\n", file_info.st_atime);
printf("마지막 수정 시간 : %d\n", file_info.st_mtime);
printf("하드링크된 파일수 : %d\n", file_info.st_nlink);
}
|