Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 동기 함수 내에서 비동기 함수 호출
- @observedobject 프로퍼티 래퍼
- featured-sliced-design
- @published 프로퍼티 래퍼
- 페이지이동함수
- GridItem
- 리렌더링최적화
- CSS
- 세로모드끄기
- 리액트최적화
- LazyHGrid
- react-router-dom
- 비동기함수
- zustand란
- 반응형 css
- LazyVGrid
- 컴퓨터네트워크
- react hook
- @environmentobject 프로퍼티 래퍼
- SwiftUI Font
- navigationBar 숨기기
- BFS
- C++
- 페이지전환
- react fsd
- 블로그업로드확인
- 가로모드끄기
- 상단 빈공간 제거
- react상태관리라이브러리
- zustand
Archives
- Today
- Total
leebaek
[UNIX프로그래밍] 5강 - 시스템 정보 본문
- uid, guid 검색
- directory tree의 산책
■ uid, guid ( 현재 실행 중인 프로세스의 소유자 및 그룹 정보를 가져오는 데 사용 )
□ uid
- getuid() : 파일 소유권을 확인할 때 주로 사용 ( return 값 실제 사용자 ID : 성공 | -1 : 실패 )
- geteuid() : 파일 접근 권한을 결정할 때 사용 ( return 값 유효 사용자 ID : 성공 | -1 : 실패 )
> 헤더 파일
#include <unistd.h>
#include <sys/types.h>
uid_t getuid(void);
uid_t geteuid(void);
uid_t real_uid = getuid();
uid_t effective_uid = geteuid();
printf("Real UID: %d\n", real_uid);
printf("Effective UID: %d\n", effective_uid);
□ guid
- getgid() : ( return 값 실제 그룹 ID : 성공 | -1 : 실패 )
- getegid() : ( return 값 유효 그룹 ID : 성공 | -1 : 실패 )
> 헤더 파일
#include <unistd.h>
#include <sys/types.h>
gid_t getgid(void);
gid_t getegid(void);
gid_t real_gid = getgid();
gid_t effective_gid = getegid();
printf("Real GID: %d\n", real_gid);
printf("Effective GID: %d\n", effective_gid);
■ directory tree의 산책 ( 파일 트리 내의 파일과 디렉터리를 순회하면서 지정된 함수 호출함 )
□ ftw : Path에서 시작해서 재귀적으로 하위 디렉터리와 파일에 대해 func() 함수를 적용
- func()가 0을 반환하면 계속해서 파일 트리 탐색
- 0이 아닌 값을 반환하면 ftw()가 즉시 중단
- 에러가 발생하면, ftw() 함수는 중단하고 -1 반환
> 헤더 파일
#include <ftw.h>
int ftw(const char *path, int(*func)(), int depth); // depth : ftw에 의해 사용 가능한 file descriptor 수
□ func() 함수
int func(const char *name, const struct stat *sptr, int type) {}
name : 파일 또는 디렉터리의 경로 이름
sptr : 파일 또는 디렉터리에 대한 정보를 저장하는 struct stat 구조체에 대한 포인터
type :
- FTW_F: 일반 파일
- FTW_D: 디렉터리
- FTW_DNR: 읽을 수 없는 디렉터리 ( 디렉터리 접근 권한이 없는 경우 )
- FTW_SL: 심볼릭 링크
- FTW_NS: 파일 상태(stat)를 얻을 수 없는 경우 ( 파일 정보 접근이 불가능하거나 에러가 발생한 경우 )
문제 :
Current working directory와 그의 descendent directory들 중 비어있는 디렉토리의 이름을 출력하는 프로그램을 작성하시오.
- main()에서 ftw() 호출
- ftw()가 현재 디렉터리(.)부터 시작하여 하위 디렉터리를 순회
- 디렉터리나 파일을 만날 때마다 list() 함수 호출
- list() 함수가 디렉터리일 경우 그 안의 항목 수를 확인
- 항목이 2개(기본 항목인 .과 ..) 이하일 경우, 비어 있는 디렉터리로 간주하고 출력
- 탐색이 끝나면 프로그램 종료
#include <stdio.h>
#include <sys/stat.h>
#include <ftw.h>
#include <dirent.h>
int list(const char *name, const struct stat *status, int type) {
int cnt;
struct dirent *d;
DIR *dp;
if ( type == FTW_D) {
dp = opendir(name);
cnt = 0;
while((d = readdir(dp)) != NULL) {
cnt++;
}
if ( cnt <= 2 )
printf("%s\n", name);
closedir(dp);
}
return 0;
}
int main() {
ftw(".", list, 1);
return 0;
}
문제 :
Directory 이름을 main 함수의 인자로 받아, 해당 directory와 그의 descendent directory들 안에 저 장된 모든 파일과 서브디렉토리들에 대해, 디렉토리와 실행 파일들만 선택하여 그 이름과 크기를 출력 하는 프로그램을 작성하시오. 또한, 실행파일 출력 후에, 실행 중인 프로그램의 프로세스 id, 프로세스 그룹 id, session id를 출력한다.
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ftw.h>
int list(const char *name, const struct stat *status, int type) {
if ( type == FTW_D || FTW_DNR ) {
printf("%s : %d\n", name, status->st_size);
}
else if ( type == FTW_F ){
if ( S_IXUSR & status->st_mode || S_IXGRP & status->st_mode || S_IXOTH & status->st_mode )
printf("%s : %d\n", name, status->st_size);
}
return 0;
}
int main(int argc, char **argv) {
ftw(argv[1], list, 1);
printf("%d, %d, %d", getpid(), getpgid(0), getsid(getpid()));
return 0;
}
'수업 정리 > UNIX 프로그래밍' 카테고리의 다른 글
[UNIX프로그래밍] 7강 - 프로세스 생성과 실행 (0) | 2024.10.19 |
---|---|
[UNIX프로그래밍] 6강 - 프로세스 정보 (7) | 2024.10.16 |
[UNIX프로그래밍] 4강 - 파일 입출력 (0) | 2024.10.15 |
[UNIX프로그래밍] 3강 - File 다루기 (0) | 2024.10.12 |
[UNIX프로그래밍] 2강 - Directory 다루기 (0) | 2024.10.12 |