학교 강의를 복습하는 차원에서 작성하는 글입니다.
학부생 수준의 글이므로, 오류가 있을 수 있는 점 양해바람니다.
unlink, remove
pathname으로 지정한 파일 삭제
비어 있는 디렉터리는 remove만 사용 가능
비어 있지 않은 디렉터리는 삭제 불가
#include <unistd.h>
int unlink(const char * pathname);
#include <stdio.h>
int remove(const char * pathname);
// pathname => 삭제할 파일의 경로 이름
// return => 작업이 성공시 0 반환, 실패시 -1 반환
chmod, fchmod
chmod는 개방하지 않은 파일을 경로명으로 지정
fchmod는 개방한 파일을 파일 기술자로 지정한다.
#include <sys/types.h>
#include <sys/stat.h>
int chmod(const char * path, mode_t mode);
int fchmod(int fileds, mode_t mode);
// path 파일에 대한 경로 이름
// filedes 개방된 파일의 파일 기술자
// mode 파일에 새롭게 적용하려는 접근 권한
// return 호출 성공시 0, 실패시 -1 반환
접근 권한 표현은 8진수로 하고. 앞에 0으로 시작해 8진수임을 알린다.
Ex) 0444, 0200
Macro 상수 사용
mode_t mode;
mode = S_lRUSR | S_lWUSR | S_lRGRP | S_lROTH;
chmod("test.txt", mode);
// 위 아래와 동일
chmod("test.txt", mode);_
//
mode = S_IRUSR | S_IXUR;/* mode = 0500 */
mode = mode | S_IRUSR; /* 실수로 S_IWUSR 대신에 S_IRUSR를 적음, mode=0500 */
...mode = S_IRUSR + S_IXUR; /* mode = 0500 */
mode = mode + S_IRUR; /* mode = 1000 in octal */
예제
#include <sys/types.h>
#include <sys/stat.h>
int main()
{
mode_t mode1, mode2;
mode1 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
mode2 = 0644;
if(chmod("test1.txt", mode1) == -1)
exit(1);
if(chmod("test2.txt", mode2) == -1)
exit(1);
printf("rest of program ... \n");
}
stat / fstat
파일 상태 확인
stat은 filename 사용
fstat은 file descriptor 사용
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *path, struct stat * buf);
int fstat(int filedes, struct stat * buf);
// path 파일에 대한 경로 이름
// filedes 개방된 파일의 파일 기술자
// stat 파일의 상태 혹은 권한에 대한 데이터를 넘길 구조체 포인터
// return 호출 성공하면 0 반환 실패시 -1 반환
*callByValue | callByReference
call by reference는 값이 복사가 안 일어나서
임베디드에서 좋음. 메모리 사용이 적어 훨씬 빠르다.
mkdir, rmdir
새 디렉터리를 생성 , 지정한 빈 디렉터리를 삭제(빈 디렉터리만 가능)
#include <sys/types.h>
#include <sys/stat.h>
int mkdir(const char *pathname, mode_t mode);
#include <unistd.h>
int rmdir(const char *pathname);
// pathname 디렉터리의 경로 이름
// mode 생성하려는 디렉터리의 초기 접근 권한
// return 호출 성공하면 0 반환 , 실패시 -1 반환
예제
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main()
{
mkdir("test_dir1", 0755);
mkdir("test_dir2", 0755);
rmdir("test_dir2");
}
chdir
현재 작업 디렉터리를 변경한다.
path => 변경하려는 새로운 디렉터리 경로
변경된 디렉터리 경로는 현재 프로세스에만 적용되며, 프로세스 종료 후 사용자가 보게 되는 셀에는 영향 주지 앟는다.
#include <unistd.h>
int chdir(const char *path);
// path 변경하려는 새로운 디렉터리 경로
// return 호출 성공시 0 반환, 실패하면 -1 반환
getcwd
현재 작업 디렉터리의 절대 경로를 지정한 버퍼에 저장
#include <unistd.h>
char *getcwd(char *buf, size_t size);
// buff 현재 작업 디렉터리의 경로를 저장할 버퍼
// size 버퍼의 최대 크기
// return 호출 성공시 buf의 포인터를 반환, 실패시 NULL 반환
예제
#include <unistd.h>
#define BUF_SIZE 256
void printcwd()
{
char buffer[BUF_SIZE];
if(getcwd(buffer, BUF_SIZE) == NULL)
exit(1);
printf("%s\n", buffer);
}
int main()
{
printcwd();
chdir("/usr/include");
printcwd(); chdir("..");
printcwd();
}
'프로그래밍 > Linux' 카테고리의 다른 글
[Linux]프로세스 처리 (1) | 2023.10.13 |
---|---|
[Linux]파일입출력(2) (0) | 2023.10.08 |
[Linux]파일입출력(1) (0) | 2023.10.08 |
[Linux]파일시스템 (1) | 2023.10.08 |
[Linux]Debug (0) | 2023.10.08 |