파일의 유형
주로 파일의 유형에는 Regular file, Directory file, Device file, FIFO, Socket 등이 있다.
- Regular file
Binary or text file
- Directory file
A file that contains the names and locations of other files
- Device file - Character special and block special files (문자 장치 파일과 블록 장치 파일)
- Character soecial file (문자 장치 파일)
데이터를 바이트 단위로 전송하는 장치
ex) terminal, mouse, keyboard etc.
- block sepcial file
데이터를 블록 단위로 전송하는 장치
ex) hard disk, ssd, usb etc.
- FIFO (named pipe)
One of the mechanisms used for inter-process communication (IPC)
And is also referred to as a Named Pipe(이름 있는 파이프)
- Socket
A file type used for network commuication between process
파일 유형 확인
파일의 유형을 확인하는 함수들은 <sys/stat.h>에 아래 코드와 같이 정의되어 있다.
int S_ISREG(mode_t mode); // 정규 파일인지 확인
int S_ISDIR(mode_t mode); // 디렉터리인지 확인
int S_ISCHR(mode_t mode); // 문자 장치 파일인지 확인
int S_ISBLK(mode_t mode); // 블록 장치 파일인지 확인
int S_ISFIFO(mode_t mode); // FIFO(이름 있는 파이프)인지 확인
int S_ISSOCK(mode_t mode); // 소켓 파일인지 확인
int S_ISLNK(mode_t mode); // 심볼릭 링크인지 확인
위 함수들은 인자로 stat 구조체의 mode_t를 인자로 요구한다.
위 함수들은 만약 해당하는 파일이 맞다면 1을 반환하고 그렇지 않다면 0을 반환한다.
따라서 아래 코드와 같이 사용하는 것이 가능하다.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
struct stat fileStat;
char filePath[100]; // 파일 경로를 저장할 배열
char* type;
char* readok;
// 사용자에게 파일 경로 입력 받기
printf("Enter the file path: ");
scanf("%s", filePath);
// stat 호출 (파일 상태 정보 얻기)
if (stat(filePath, &fileStat) < 0) {
perror("stat");
exit(1);
}
// 파일 유형 확인
if (S_ISREG(fileStat.st_mode))
type = "regular";
else if (S_ISDIR(fileStat.st_mode))
type = "directory";
else
type = "other";
// 읽기 권한 확인
if (fileStat.st_mode & S_IRUSR)
readok = "yes";
else
readok = "no";
// 결과 출력
printf("File type: %s\n", type);
printf("Read permission: %s\n", readok);
return 0;
}
'C언어 > 시스템 소프트웨어' 카테고리의 다른 글
[시스템 소프트웨어] User Mode, Kernal Mode, System Call (0) | 2024.10.08 |
---|---|
[시스템 소프트웨어/C언어] stat(), fstat() (0) | 2024.10.08 |
[시스템 소프트웨어/C언어] lseek, 파일 오프셋 변경 (파일 작업 위치 변경) (0) | 2024.10.08 |
[C언어] perror, errno, exit (0) | 2024.10.07 |
[C언어] 파일 입출력 (open, close, write, read) (0) | 2024.10.07 |