Notice
Recent Posts
Recent Comments
Today
Total
05-04 00:14
Archives
관리 메뉴

Jeongchul Kim

리눅스 access - 리눅스 시스템 프로그래밍 본문

Linux

리눅스 access - 리눅스 시스템 프로그래밍

김 정출 2016. 2. 29. 14:11


리눅스 access - 리눅스 시스템 프로그래밍


프로세스가 지정한 파일을 오픈하려 할 때 허가되지 않은 접근 권한을 적용하면 오픈이 실패하게 된다.

access는 프로세스가 지정한 파일에 대해 어떤 접근 권한을 가질 수 있는지 검사한다.



int access(const char* pathname, int mode);


> pathname : 파일에 대한 경로명이다.

> mode : 검사하려는 접근 권한으로

- R_OK(읽을 수 있는가?),

- W_OK(쓸 수 있는가?),

- X_OK(실행되는가?)

- F_OK(존재하는가?)

> return 값 : access 호출이 성공하면 0을 반환, 실패하면 -1을 반환한다.

> 헤더파일 : <unistd.h>



#include <unistd.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

int main() {

char *filename = "access.txt";

if( access(filename, R_OK) == -1) {

perror("User can't read file");

return 1;

}

if( access(filename, W_OK) == -1) {

perror("User can't write file");

return 1;

}

if( access(filename, X_OK) == -1) {

perror("User can't execute file");

return 1;

}

if( access(filename, F_OK) == -1) {

perror("User can't exist file");

return 1;

}

}




Comments