1.파일명으로 inode 정보 검색하기
예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <stdio.h> | |
// | |
//buf | |
// - 파일의 상태 및 정보를 저장할 buf 구조체 | |
//struct stat { | |
// dev_t st_dev; /* ID of device containing file */ | |
// ino_t st_ino; /* inode number */ | |
// mode_t st_mode; /* 파일의 종류 및 접근권한 */ | |
// nlink_t st_nlink; /* hardlink 된 횟수 */ | |
// uid_t st_uid; /* 파일의 owner */ | |
// gid_t st_gid; /* group ID of owner */ | |
// dev_t st_rdev; /* device ID (if special file) */ | |
// off_t st_size; /* 파일의 크기(bytes) */ | |
// blksize_t st_blksize; /* blocksize for file system I/O */ | |
// blkcnt_t st_blocks; /* number of 512B 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 status change */ | |
//}; | |
// | |
int main(void) { | |
//파일의 상태 및 정보를 저장할 구조체 | |
struct stat buf; | |
//stat() 함수를 이용하면 파일의 상태를 알아올수 있다. | |
//첫번째 인자로 주어진 file_name 의 상태를 얻어와서 두번째 인자인 buf 에 채워 넣는다. | |
stat("unix.txt", &buf); | |
printf("Inode = %d\n", (int)buf.st_ino); | |
printf("Mode = %o\n", (unsigned int)buf.st_mode); | |
printf("Nlink = %o\n",(unsigned int) buf.st_nlink); | |
printf("UID = %d\n", (int)buf.st_uid); | |
printf("GID = %d\n", (int)buf.st_gid); | |
printf("SIZE = %d\n", (int)buf.st_size); | |
printf("Atime = %d\n", (int)buf.st_atime); | |
printf("Mtime = %d\n", (int)buf.st_mtime); | |
printf("Ctime = %d\n", (int)buf.st_ctime); | |
printf("Blksize = %d\n", (int)buf.st_blksize); | |
printf("Blocks = %d\n", (int)buf.st_blocks); | |
//printf("FStype = %s\n", buf.st_fstype); | |
return 0; | |
} | |
//참고 | |
/* | |
st_mode : 파일의 종류와 file에 대한 access 권한 정보 | |
st_mode에 따라서 파일의 종류를 알 수 있고, 파일의 퍼미션(permission)도 알 수 있다. | |
S_ISREG(buf.st_mode) : 일반 파일 여부 | |
S_ISDIR(buf.st_mode) : 디렉토리 여부 | |
S_ISCHR(buf.st_mode) : character device 여부 | |
S_ISBLK(buf.st_mode) : block device 여부 | |
S_ISFIFO(buf.st_mode) : FIFO 여부 | |
S_ISLNK(buf.st_mode) : symbolic link 여부 | |
S_ISSOCK(buf.st_mode) : socket 여부 (주로 AF_UNIX로 socket 생성된 경우) | |
*/ |
결과

2.fstat 함수로 파일정보 검색하기
예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
//fstat() 함수는 파일 경로 정보 대신 열린파일 디스크립터를 입력으로 받아 | |
//파일정보를 검색한 후 buf로 지정한 구조체에 저장한다. | |
// | |
//struct stat{ | |
// dev_t st_dev; /* 파일이 위치한 디바이스 ID */ | |
// ino_t st_ino; /* 파일의 i-노드 수 */ | |
// mode_t st_mode; /* 파일 형식과 권한 */ | |
// nlink_t st_nlink; /* 파일의 (하드)링크 수 */ | |
// uid_t st_uid; /* 파일 소유자의 사용자 ID */ | |
// gid_t st_gid; /* 파일 소유자의 그룹 ID */ | |
// dev_t st_rdev; /* 파일 디바이스 특정 파일의 ID */ | |
// off_t st_size; /* 파일의 전체 크기 (BYTE) */ | |
// blksize_t st_blksize; /* I/O의 최적 블록크기 (바이트) */ | |
// blkcnt_t st_blocks; /* 할당된 블록의 수(512B) */ | |
// time_t st_atime; /* 마지막 파일 접근 시간 */ | |
// time_t st_mtime; /* 마지막 파일 수정 시간 */ | |
// time_t st_ctime; /* 마지막 상태 변경 시간 */ | |
//}; | |
int main(void) { | |
//파일 디스크립터 | |
int fd; | |
//파일 구조체 | |
struct stat buf; | |
//파일을 읽기전용으로 열고 파일 디스크립터 리턴 받기. | |
fd = open("unix.txt", O_RDONLY); | |
if (fd == -1) { | |
perror("open: unix.txt"); | |
exit(1); | |
} | |
//열려진 파일의 크기, 파일의 권한, 파일의 생성일시, 파일의 최종 변경일 등, | |
//파일의 상태나 파일의 정보를 얻는 함수입니다. | |
fstat(fd, &buf); | |
printf("Inode = %d\n", (int)buf.st_ino); | |
printf("UID = %d\n", (int)buf.st_uid); | |
close(fd); | |
return 0; | |
} |
결과

3.상수를 이용해 파일 종류 검색하기
예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <stdio.h> | |
//stat 구조체의 st_mode항목에는 파일의 종류와 접근 권한 정보가 저장된다. | |
int main(void) { | |
//파일 정보를 담는 구조체 | |
struct stat buf; | |
//파일종류 | |
int kind; | |
stat("unix.txt", &buf); | |
printf("Mode = %o (16jin soo: %x)\n", (unsigned int)buf.st_mode, (unsigned int)buf.st_mode); | |
//Mode = 100666 (16진수: 81b6) | |
//st_mode는 파일의 유형값으로 직접 bit & 연산으로 여부를 확인가능합니다. | |
//S_IFMT 0170000 파일 유형의 전체의 bit or 값 | |
//st_mode 와 상수 S_IFMT를 and 연산한다. | |
kind = buf.st_mode & S_IFMT; | |
printf("Kind = %x\n", kind); //Kind = 8000 | |
switch (kind) { | |
case S_IFIFO: | |
printf("unix.txt : FIFO\n"); | |
break; | |
case S_IFDIR: | |
printf("unix.txt : Directory\n"); | |
break; | |
case S_IFREG: | |
printf("unix.txt : Regular File\n"); | |
break; | |
} | |
return 0; | |
} |
결과

4.매크로를 이용해 파일 종류 검색하기
예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <stdio.h> | |
int main(void) { | |
//파일 정보를 담는 구조체 | |
struct stat buf; | |
stat("unix.txt", &buf); | |
printf("Mode = %o (16 jin soo: %x)\n", (unsigned int)buf.st_mode, (unsigned int)buf.st_mode); | |
printf("S_ISFIFO = %o (16 jin soo: %x)\n", S_ISFIFO(buf.st_mode) , S_ISFIFO(buf.st_mode)); | |
printf("S_ISDIR = %o (16 jin soo: %x)\n", S_ISDIR(buf.st_mode) , S_ISDIR(buf.st_mode)); | |
printf("S_ISREG = %o (16 jin soo: %x)\n", S_ISREG(buf.st_mode) , S_ISREG(buf.st_mode)); | |
/* 출력 | |
S_ISFIFO = 0 (16진수: 0) | |
S_ISDIR = 0 (16진수: 0) | |
S_ISREG = 1 (16진수: 1) | |
*/ | |
//st_mode 를 S_ISFIFO,S_ISDIR 등 매크로로 확인해 파일 종류를 검색한다. | |
if(S_ISFIFO(buf.st_mode)) printf("unix.txt : FIFO\n"); | |
if(S_ISDIR(buf.st_mode)) printf("unix.txt : Directory\n"); | |
if(S_ISREG(buf.st_mode)) printf("unix.txt : Regualr File\n"); | |
//출력 unix.txt : Regualr File | |
return 0; | |
} |
결과

5.상수를 이용해 파일 접근 권한 검색하기
예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <stdio.h> | |
int main(void) { | |
struct stat buf; | |
stat("unix.txt", &buf); | |
printf("Mode = %o (16 jin soo: %x)\n", (unsigned int)buf.st_mode, (unsigned int)buf.st_mode); | |
//st_moded의 값을 S_IREAD와 and 연산해 소유자의 읽기 권한을 확인한다. | |
if ((buf.st_mode & S_IREAD) != 0) | |
printf("unix.txt : user has a read permission\n"); | |
//s_iread 상수값을 오른쪽으로 3만큼 이동시켜 그룹의 읽기 권한을 확인한다. | |
if ((buf.st_mode & (S_IREAD >> 3)) != 0) | |
printf("unix.txt : group has a read permission\n"); | |
//posix가 정의한 상수 s_iroth를 이용해 기타 사용자의 읽기 권한을 검색한다. | |
if ((buf.st_mode & S_IROTH) != 0) | |
printf("unix.txt : other have a read permission\n"); | |
return 0; | |
} |
결과

6.access 함수를 이용해 접근 권한 검색하기
예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//#include <sys/errno.h> | |
#include <errno.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
extern int errno; | |
int main(void) { | |
int per; | |
//ENOENT : 경로명의 디렉토리로 접근가능하지만 존재하지 않거나 깨진 링크일때 | |
if (access("unix.bak", F_OK) == -1 && errno == ENOENT) | |
printf("unix.bak: File not exist.\n"); | |
per = access("unix.txt", R_OK); | |
if (per == 0) | |
printf("unix.txt: Read permission is permitted.\n"); | |
//요구된 접근이 파일에 의해 거절되거나, 경로명안의 어느 한 디렉토리에 의해 거절되었을경우 | |
else if (per == -1 && errno == EACCES) | |
printf("unix.txt: Read permission is not permitted.\n"); | |
return 0; | |
} | |
/* | |
unix.bak: File not exist. | |
unix.txt: Read permission is permitted. | |
*/ |
결과

7.chmod 함수 사용하기
예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <stdio.h> | |
int main(void) { | |
struct stat buf; | |
//chmod 함수는 접근권한을 변경할 파일의 경로를 받아 mode에 지정한 상수값으로 권한을 변경한다. | |
//접근권한을 더할 때는 or 연산 | |
chmod("unix.txt", S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH); | |
stat("unix.txt", &buf); | |
printf("1.Mode = %o\n", (unsigned int)buf.st_mode); | |
//위에서 추가한 파일에 쓰기권한 추가 | |
buf.st_mode |= S_IWGRP; | |
//접근권한을 제거하려면 권한의 상수값을 not연산 후 and 연산실행. | |
buf.st_mode &= ~(S_IROTH); | |
chmod("unix.txt", buf.st_mode); | |
stat("unix.txt", &buf); | |
printf("2.Mode = %o\n", (unsigned int)buf.st_mode); | |
//결과 1.mode 와 2mode 의 값이 달라졌다. | |
return 0; | |
} |
결과

8.하드링크생성
예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
/* | |
링크를 생성합니다. | |
link()는 파일에 대해서만 새 이름을 생성하며, | |
생성된 이름으로 같은 파일을 사용할 수 있습니다. | |
즉, 리눅스에서는 하나의 파일에 여러 이름을 지정할 수 있으며, | |
생성된 이름 어느 것으로 파일 내용을 수정하면 | |
다른 이름으로 열어 보아도 수정된 내용으로 볼 수 있습니다. | |
*/ | |
int main(void) { | |
struct stat buf; | |
stat("unix.txt", &buf); | |
printf("Before Link Count = %d\n", (int)buf.st_nlink); | |
//하드링크 생성 | |
//기존에 파일과 같은 inode 사용 , inode내 링크 개수 증가 | |
if ( -1 == link("unix.txt", "unix222.txt")) | |
printf( "fail hard link create \n"); | |
stat("unix.txt", &buf); | |
printf("After Link Count = %d\n", (int)buf.st_nlink); | |
return 0; | |
} |
결과

9.심볼릭 링크생성
예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <unistd.h> | |
/* | |
심볼릭 링크는 기존 파일에 접근할 수 있는 다른 파일을 만든다. | |
기존 파일과 다른 inode를 사용하며, 기존 파일의 경로를 저장한다. | |
심볼릭 링크의 정보를 검색할 때는 stat 함수가 아닌, lstat 함수를 사용하다. | |
심볼릭 링크 자체가 담고 있는 내용은 readlink()함수로 읽어 올 수 있다. | |
*/ | |
int main(void) { | |
//심볼릭 링크 생성 | |
symlink("unix.txt", "unix.sym"); | |
return 0; | |
} |
결과

10.lstat 함수 사용하기
예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
/* | |
stat 함수는 심볼릭 링크 자체의 파일 정보를 검색한다. | |
심볼릭 링크를 stat 함수로 검색하면 원본 파일에 대한 정보가 검색된다. | |
*/ | |
int main(void) { | |
struct stat buf; | |
printf("1. stat : unix.txt ---\n"); | |
//stat() 함수를 이용하면 파일의 상태를 알아올수 있다. | |
//첫번째 인자로 주어진 file_name 의 상태를 얻어와서 두번째 인자인 buf 에 채워 넣는다. | |
stat("unix.txt", &buf); | |
printf("unix.txt : Link Count = %d\n", (int)buf.st_nlink); | |
printf("unix.txt : Inode = %d\n", (int)buf.st_ino); | |
printf("2. stat : unix.sym ---\n"); | |
stat("unix.sym", &buf); | |
printf("unix.sym : Link Count = %d\n", (int)buf.st_nlink); | |
printf("unix.sym : Inode = %d\n", (int)buf.st_ino); | |
printf("3. lstat : unix.sym ---\n"); | |
//lstat() 함수는 심볼릭링크 파일의 원본파일의 상태를 얻어온다는 것을 제외하고는 stat() 함수와 동일하다. | |
lstat("unix.sym", &buf); | |
printf("unix.sym : Link Count = %d\n", (int)buf.st_nlink); | |
printf("unix.sym : Inode = %d\n", (int)buf.st_ino); | |
return 0; | |
} |
결과

예제파일
send3 2.zip
0.03MB
참고 : 유닉스시스템 프로그래밍(한빛미디어)
'컴퓨터 기초 > 운영체제 실습' 카테고리의 다른 글
[운영체제 실습] 8.시스템 정보 다루기 - (그룹, 시간) (0) | 2020.06.27 |
---|---|
[운영체제 실습] 7.시스템 정보 다루기 - (로그인, 패스워드 정보) (0) | 2020.06.27 |
[운영체제 실습] 5.파일 다루기(라이브러리) (0) | 2020.06.23 |
[운영체제 실습] 4.에러처리 및 파일 다루기(low level) (0) | 2020.06.23 |
[운영체제 실습] 3.리눅스 커멘트라인 인자와 응용 (0) | 2020.06.22 |