Presentation is loading. Please wait.

Presentation is loading. Please wait.

3 파일과 디렉토리.

Similar presentations


Presentation on theme: "3 파일과 디렉토리."— Presentation transcript:

1 3 파일과 디렉토리

2 학습 목표 유닉스 파일의 특징을 이해한다. 파일에 관한 정보를 검색하는 함수를 사용할 수 있다.
하드 링크와 심볼릭 링크 파일을 이해하고 관련 함수를 사용할 수 있다. 파일 사용권한을 검색하고 조정하는 함수를 사용할 수 있다. 디렉토리의 특징을 이해한다. 디렉토리의 내용을 검색하는 함수를 사용할 수 있다. 디렉토리를 생성하고 삭제하는 함수를 사용할 수 있다.

3 목 차 유닉스 파일의 특징 파일 정보 검색 파일의 종류 및 접근권한 검색 하드링크 및 심볼릭 링크 생성 디렉토리 관련 함수

4 유닉스 파일의 특징 (1) 파일 파일의 종류 일반파일 장치파일 디렉토리
유닉스에서 파일은 데이터 저장, 장치구동, 프로세스 간 통신 등에 사용 파일의 종류 일반파일, 특수파일, 디렉토리(directory) 일반파일 텍스트 파일, 실행파일, 라이브러리, 이미지 등 유닉스에서 사용하는 대부분의 파일 편집기나 다른 응용 프로그램을 통해 생성 및 수정 장치파일 장치를 사용하기 위한 특수 파일 블록장치파일 : 블록단위(0.5~8KB)로 읽고 쓴다. 문자장치파일 : 문자단위로 읽고 쓴다 -> 로우디바이스(Raw Device) 내부적으로는 장치번호(major &minor device numbers)로 식별 예 : /dev 디렉토리 디렉토리도 파일로 취급 디렉토리와 관련된 데이터 블록은 해당 디렉토리에 속한 파일의 목록과 inode 저장

5 유닉스 파일의 특징 (2)

6 유닉스 파일의 특징 (3) 파일 생성 및 제거, 접근 하나의 파일은 inode 구조체와 데이터 블록을 갖는다
데이터 블록 : 실제 데이터를 저장 파일 종류별로 inode 및 데이터 블록에 저장되는 정보가 다름 파일 생성 및 제거, 내용 접근 등을 위한 시스템 호출이 다름 일반 파일 접근 시스템 호출 open, close, remove, read, write 등 장치 파일 접근 시스템 호출 mknod, remove 등 디렉토리 접근 시스템 호출 mkdir, rmdir, readdir, closedir 등

7 유닉스 파일의 특징 (4) 파일의 종류 구분 ls –l 명령으로 파일의 종류 확인 가능 : 결과의 맨 앞글자로 구분
파일 종류 식별 문자 예제 # ls -l /usr/bin/vi -r-xr-xr-x 5 root bin 월 14일 /usr/bin/vi # ls -l / lrwxrwxrwx 1 root root 월 16일 15:22 bin -> ./usr/bin drwxr-xr-x 42 root sys 월 5일 03:26 usr ...... # ls -lL /dev/dsk/c0d0s0 brw-r root sys , 0 8월 3일 10:59 /dev/dsk/c0d0s0 # ls -lL /dev/rdsk/c0d0s0 crw-r root sys , 0 8월 3일 12:12 /dev/rdsk/c0d0s0

8 유닉스 파일의 특징 (5) 파일의 구성 요소 파일명 inode 데이터 블록 파일명, inode, 데이터블록
사용자가 파일에 접근할 때 사용 파일명과 관련된 inode가 반드시 있어야 함 파일명은 최대 255자까지 가능 파일명에서 대소문자를 구분하며, ‘.’으로 시작하면 숨김 파일 inode 외부적으로는 번호로 표시, 내부적으로는 두 부분으로 나누어 정보 저장 파일 정보를 저장하는 부분 데이터 블록의 주소 저장하는 부분 파일의 inode 번호는 ls –i 명령으로 확인 가능 데이터 블록 실제로 데이터가 저장되는 부분

9 파일 정보 검색 (1) 파일명으로 파일 정보 검색 : stat(2) inode에 저장된 파일 정보 검색
path에 검색할 파일의 경로를 지정하고, 검색한 정보를 buf에 저장 stat 구조체 : #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> int stat(const char *restrict path, struct stat *buf); struct stat { dev_t st_dev; ino_t st_ino; mode_t st_mode; nlink_t st_nlink; uid_t st_uid; gid_t st_gid; dev_t st_rdev; off_t st_size; time_t st_atime; time_t st_mtime; time_t st_ctime; blksize_t st_blksize; blkcnt_t st_blocks; char st_fstype[_ST_FSTYPSZ]; };

10 [예제 3-1] 파일명으로 inode 정보 검색하기
ex3_1.c 01 #include <sys/types.h> 02 #include <sys/stat.h> 03 #include <stdio.h> 04 05 int main(void) { 06 struct stat buf; 07 08 stat("unix.txt", &buf); 09 10 printf("Inode = %d\n", (int)buf.st_ino); 11 printf("Mode = %o\n", (unsigned int)buf.st_mode); 12 printf(“# of link = %o\n",(unsigned int) buf.st_nlink); 13 printf("UID = %d\n", (int)buf.st_uid); 14 printf("GID = %d\n", (int)buf.st_gid); 15 printf("SIZE = %d\n", (int)buf.st_size); 16 printf("Atime = %d\n", (int)buf.st_atime); 17 printf("Mtime = %d\n", (int)buf.st_mtime); 18 printf("Ctime = %d\n", (int)buf.st_ctime); 19 printf("Blksize = %d\n", (int)buf.st_blksize); 20 printf("Blocks = %d\n", (int)buf.st_blocks); 21 printf("FStype = %s\n", buf.st_fstype); 22 23 return 0; 24 } # ex3_1.out Inode = 192 Mode = # of link = 1 UID = 0 GID = 1 SIZE = 24 Atime = Mtime = Ctime = Blksize = 8192 Blocks = 2 FStype = ufs

11 파일 정보 검색 (2) 파일 기술자로 파일 정보 검색 : fstat(2) fd로 지정한 파일의 정보를 검색하여 buf에 저장
#include <sys/types.h> #include <sys/stat.h> int fstat(int fd, struct stat *buf); 01 #include <sys/types.h> 02 #include <sys/stat.h> 03 #include <fcntl.h> 04 #include <unist.h> 05 #include <stdio.h> 06 #include <stdlib.h> 07 08 int main(void) { 09 int fd; struct stat buf; 11 fd = open("unix.txt", R_DONLY); [예제 3-2] 명령행 인자 출력하기 ex3_2.c

12 [예제 3-2] fstat으로 파일 정보 검색하기
ex3_2.c 13 if (fd == -1) { perror(“open: unix.txt”); exit(1); 16 } 17 fstate(fd, &buf); 19 printf(“Inode = %d\n", (int)buf.st_ino); printf(“UID = %d\n", (int)buf.st_uid); close(fd); 23 return 0; 25 } # ex3_2.out Inode = 192 UID = 0

13 파일 접근권한 제어 stat 구조체의 st_mode 항목에 파일의 종류와 접근권한 정보 저장 st_mode 값의 구조 :
<sys/stat.h>에 매크로 상수 정의 매크로 상수와 AND 연산를 이용하여 사용

14 파일 종류 검색 (1) 매크로 상수(Macro Constant)를 이용한 파일 종류 검색 파일의 종류 검색 관련 상수
st_mode 값과 상수값을 AND(&) 연산하면 파일의 종류 부분만 남게 된다.

15 [예제 3-3] 상수를 이용해 파일 종류 검색하기 ex3_3.c 01 #include <sys/types.h>
02 #include <sys/stat.h> 03 #include <stdio.h> 04 05 int main(void) { struct stat buf; int kind; 08 stat("unix.txt", &buf); 10 11 printf("Mode = %o (16진수: %x)\n", (unsigned int)buf.st_mode, (unsigned int)buf.st_mode); 12 kind = buf.st_mode & S_IFMT; printf("Kind = %x\n", kind); 15 switch (kind) { case S_IFIFO: printf("unix.txt : FIFO\n"); break; case S_IFDIR: printf("unix.txt : Directory\n"); break;

16 [예제 3-3] 상수를 이용해 파일 종류 검색하기 # ex3_3.out Mode = 100644 (16진수: 81a4)
case S_IFREG: printf("unix.txt : Regular File\n"); break; } 27 return 0; 29 } # ex3_3.out Mode = (16진수: 81a4) Kind = 8000 unix.txt : Regular File

17 파일 종류 검색 (2) 매크로를 이용한 파일 종류 검색 각 매크로는 인자로 받은 mode 값을 0xF000과 AND연산 수행
이 매크로는 POSIX 표준

18 [예제 3-4] 매크로를 이용해 파일 종류 검색하기
ex3_4.c 01 #include <sys/types.h> 02 #include <sys/stat.h> 03 #include <stdio.h> 04 05 int main(void) { struct stat buf; 07 08 stat("unix.txt", &buf); 09 printf("Mode = %o (16진수: %x)\n",(unsigned int)buf.st_mode, (unsigned int)buf.st_mode); 11 12 if(S_ISFIFO(buf.st_mode)) printf("unix.txt : FIFO\n"); 13 if(S_ISDIR(buf.st_mode)) printf("unix.txt : Directory\n"); 14 if(S_ISREG(buf.st_mode)) printf("unix.txt : Regualr File\n"); 15 return 0; 17 } # ex3_4.out Mode = (16진수: 81a4) unix.txt : Regular File

19 파일 접근 권한 검색 (1) 상수를 이용한 파일 접근 권한 검색 소유자의 접근권한 추출과 관련된 상수만 정의
소유자 외 그룹과 기타사용자의 접근권한은? st_mode의 값을 왼쪽으로 3비트 이동시키거나 상수값을 오른쪽으로 3비트 이동시켜 AND 수행 st_mode & (S_IREAD >> 3)

20 시프트 연산없이 직접 AND 연산이 가능한 상수 정의
파일 접근 권한 검색 (2) POSIX에서 정의한 접근권한 검색 관련 상수 시프트 연산없이 직접 AND 연산이 가능한 상수 정의

21 [예제 3-5] 상수를 이용해 파일 접근 권한 검색하기
ex3_5.c 01 #include <sys/types.h> 02 #include <sys/stat.h> 03 #include <stdio.h> 04 05 int main(void) { struct stat buf; 07 stat("unix.txt", &buf); 09 printf("Mode = %o (16진수: %x)\n", (unsigned int)buf.st_mode, (unsigned int)buf.st_mode); 10 if ((buf.st_mode & S_IREAD) != 0) printf("unix.txt : user has a read permission\n"); 13 if ((buf.st_mode & (S_IREAD >> 3)) != 0) printf("unix.txt : group has a read permission\n"); 16 if ((buf.st_mode & S_IROTH) != 0) printf("unix.txt : other have a read permission\n"); 19 return 0; 21 } # ex3_5.out Mode = (16진수: 81a4) unix.txt : user has a read permission unix.txt : group has a read permission unix.txt : other have a read permission

22 파일 접근 권한 검색 (3) 함수를 사용한 파일 접근 권한 검색 : access(2)
path에 지정된 파일이 amode로 지정한 권한을 가졌는지 확인하고 반환 접근권한이 있으면 0을, 오류가 있으면 -1을 반환 오류메시지 ENOENT : 파일이 없음 EACCESS : 접근권한이 없음 amode 값 R_OK : 읽기 권한 확인 W_OK : 쓰기 권한 확인 X_OK : 실행 권한 확인 F_OK : 파일이 존재하는지 확인 #include <unistd.h> int access(const char *path, int amode);

23 [예제 3-6] access 함수를 이용해 접근 권한 검색하기
ex3_6.c 01 #include <sys/errno.h> 02 #include <unistd.h> 03 #include <stdio.h> 04 05 extern int errno; 06 07 int main(void) { int per; 09 if (access("unix.bak", F_OK) == -1 && errno == ENOENT) printf("unix.bak: File not exist.\n"); 12 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"); 18 return 0; 20 } # ls -l unix* -rw-r--r root other 24 1월 8일 15:47 unix.txt # ex3_6.out unix.bak: File not exist. unix.txt: Read permission is permitted.

24 파일 생성 마스크 설정 (1) 파일명으로 접근권한 변경 : chmod(2)
파일 생성시에 지정된 초기 접근권한 중에 설정되지 않아야 할 항목을 제한할 때에 사용 변경 전의 mask 값을 반환 새로 지정된 mask 값은 프로세스가 종료될 때까지 유지 사용 예: #include <sys/types.h> #include <sys/stat.h> mode_t umask(mode_t mask); mode_t oldmask; oldmask = umask(037); filedes = open("data.txt", O_CREAT, 0777); 새로 생성된 파일의 접근권한? 파일 생성 시 적용하는 초기 접근 권한 ( 0777) umask로 제한한 접근 권한 & (~0037) 실제로 적용되는 초기 접근 권한 ( 0740)

25 파일 생성 마스크 설정 (2) 01 #include <unistd.h>
02 #include <sys/types.h> 03 #include <sys/stat.h> 04 05 int main() 06 { 07     int filedes; 08     mode_t oldmask; 09 10     oldmask = umask(023); 11     filedes = open("test.txt", O_CREAT, 0777); 12     close(filedes); 13 } $ ls -l test.dat -rwxr-xr--   1 usp      student        0  3월 22일  21:13 test.txt*

26 파일 접근권한 변경 파일명으로 접근권한 변경 : chmod(2) 파일 기술자로 접근 권한 변경 : fchmod(2)
path에 지정한 파일의 접근권한을 mode값에 따라 변경 접근권한을 더할 때는 OR연산자를, 뺄 때는 NOT연산 후 AND 연산자 사용 chmod(path, S_ORWXU); chmod(path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH); mode |= S_IWGRP; mode &= ~(S_IROTH); 파일 기술자로 접근 권한 변경 : fchmod(2) #include <sys/types.h> #include <sys/stat.h> int chmod(const char *path, mode_t mode); mode 값 설정 후에 chmod(path, mode) 잊지말기! #include <sys/types.h> #include <sys/stat.h> int fchmod(int fd, mode_t mode);

27 [예제 3-7] chmod 함수 사용하기 01 #include <sys/types.h>
ex3_7.c 01 #include <sys/types.h> 02 #include <sys/stat.h> 03 #include <stdio.h> 04 05 int main(void) { struct stat buf; 07 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); 11 buf.st_mode |= S_IWGRP; 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); 17 return 0; 19 } mode값에 따라 권한이 어떻게 바뀌었나? # ls -l unix.txt -rw-r--r root other 24 1월 8일 15:47 unix.txt # ex3_7.out 1.Mode = 2.Mode = -rwxrwx root other 24 1월 8일 15:47 unix.txt

28 링크 파일 생성 (1) 링크 이미 있는 파일이나 디렉토리에 접근할 수 있는 새로운 이름
같은 파일/디렉토리지만 여러 이름으로 접근하는 것을 가능하게 함 복잡한 이름이나 버전에 따라 달라지는 이름 등을 쉽게 다른 이름을 지정하여 사용하는 것을 가능하게 함. 하드링크(Hard Link) 기존 파일을 접근할 수 있는 디렉토리 엔트리 항목을 새롭게 생성 기존 파일과 동일한 inode 사용, inode에 저장된 링크 수 증가 같은 파일시스템 내에서만 생성 가능 심볼릭 링크 기존 파일에 접근하는 다른 파일 생성(다른 inode 사용) 새롭게 생성된 파일에는 기존 파일에 대한 패스 정보를 저장 cf) 웹(www)에서 사용하는 하이퍼링크(hyperlink) 파일시스템 범위에 제약없이 생성 가능

29 링크 파일 생성 (2) 심볼릭링크 생성 하드링크 생성

30 링크 파일 생성 (3) 하드링크 생성 : link(2)
exiting : 기존 파일의 경로, new : 새로 생성되는 링크의 경로 두 경로는 같은 파일시스템에 존재해야 함 #include <unistd.h> int link(const char *existing, const char *new);

31 [예제 3-8] link 함수 사용하기 01 #include <sys/types.h>
ex3_8.c 01 #include <sys/types.h> 02 #include <sys/stat.h> 03 #include <unistd.h> 04 #include <stdio.h> 05 06 int main(void) { struct stat buf; 08 stat("unix.txt", &buf); printf("Before Link Count = %d\n", (int)buf.st_nlink); 11 link("unix.txt", "unix.ln"); 13 stat("unix.txt", &buf); printf("After Link Count = %d\n", (int)buf.st_nlink); 16 return 0; 18 } # ls -l unix* -rwxrwx root other 월 8일 15:47 unix.txt # ex3_8.out Before Link Count = 1 After Link Count = 2 -rwxrwx root other 월 8일 15:47 unix.ln -rwxrwx root other 월 8일 15:47 unix.txt

32 링크 파일 생성 (4) 심볼릭 링크 생성 : symlink(2) #include <unistd.h>
int symlink(const char *name1, const char *name2); 01 #include <sys/types.h> 02 #include <sys/stat.h> 03 #include <unistd.h> 04 05 int main(void) { symlink("unix.txt", "unix.sym"); 07 return 0; 09 } [예제 3-9] symlink 함수 사용하기 ex3_9.c # ls -l unix* -rwxrwx root other 월 8일 15:47 unix.ln -rwxrwx root other 월 8일 15:47 unix.txt # ex3_9.out lrwxrwxrwx 1 root other 월 11일 18:48 unix.sym ->unix.txt

33 심볼릭 링크 정보 검색 심볼릭 링크 정보 검색 : lstat(2) 심볼릭 링크의 내용 읽기 : readlink(2)
심볼릭 링크의 데이터 블록에 저장된 내용 읽기 원본 파일의 경로 읽기 : realpath(3) 심볼릭 링크가 가리키는 원본 파일의 실제 경로명 출력 #include <sys/types.h> #include <sys/stat.h> int lstat(const char *path, struct stat *buf); #include <unistd.h> ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsiz); #include <stdlib.h> char *realpath(const char *restrict file_name, char *restrict resolved_name);

34 [예제 3-10] lstat 함수 사용하기 01 #include <sys/types.h>
ex3_10.c 01 #include <sys/types.h> 02 #include <sys/stat.h> 03 #include <unistd.h> 04 #include <stdio.h> 05 06 int main(void) { struct stat buf; 08 printf("1. stat : unix.txt ---\n"); 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); 13 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); 18 printf("3. lstat : unix.sym ---\n"); lstat("unix.sym", &buf);

35 [예제 3-10] lstat 함수 사용하기 printf("unix.sym : Link Count = %d\n", (int)buf.st_nlink); printf("unix.sym : Inode = %d\n", (int)buf.st_ino); 23 return 0; 25 } # ls -li unix* 192 -rwxrwx root other 월 8일 15:47 unix.ln 202 lrwxrwxrwx 1 root other 월 11일 18:48 unix.sym->unix.txt 192 -rwxrwx root other 월 8일 15:47 unix.txt # ex3_10.out 1. stat : unix.txt --- unix.txt : Link Count = 2 unix.txt : Inode = 192 2. stat : unix.sym --- unix.sym : Link Count = 2 unix.sym : Inode = 192 3. lstat : unix.sym --- unix.sym : Link Count = 1 unix.sym : Inode = 202

36 [예제 3-11] readlink 함수 사용하기 ex3_11.c 01 #include <sys/stat.h>
02 #include <unistd.h> 03 #include <stdlib.h> 04 #include <stdio.h> 05 06 int main(void) { char buf[BUFSIZ]; int n; 09 n = readlink("unix.sym", buf, BUFSIZ); if (n == -1) { perror("readlink"); exit(1); } 15 buf[n] = '\0'; printf("unix.sym : READLINK = %s\n", buf); 18 return 0; 20 } # ex3_11.out unix.sym : READLINK = unix.txt # ls -l unix.sym lrwxrwxrwx 1 root other 8 1월 11일 18:48 unix.sym ->unix.txt

37 [예제 3-12] realpath 함수 사용하기 01 #include <sys/stat.h>
ex3_12.c 01 #include <sys/stat.h> 02 #include <stdlib.h> 03 #include <stdio.h> 04 05 int main(void) { char buf[BUFSIZ]; 07 realpath("unix.sym", buf); printf("unix.sym : REALPATH = %s\n", buf); 10 return 0; 12 } # ex3_12.out unix.sym : REALPATH = /export/home/jw/syspro/ch3/unix.txt

38 디렉토리 관련 함수 (1) 유닉스에서는 디렉토리를 하나의 파일로 다룬다 디렉토리 생성: mkdir(2)
path에 지정한 디렉토리를 mode 권한에 따라 생성한다. 디렉토리 삭제: rmdir(2) 디렉토리명 변경: rename(2) #include <sys/types.h> #include <sys/stat.h> int mkdir(const char *path, mode_t mode); #include <unistd.h> int rmdir(const char *path); #include <stdio.h> int rename(const char *old, const char *new);

39 [예제 3-13] 디렉토리 생성/삭제/이름 변경하기
ex3_13.c 01 #include <sys/stat.h> 02 #include <unistd.h> 03 #include <stdlib.h> 04 #include <stdio.h> 05 06 int main(void) { if (mkdir("han", 0755) == -1) { perror("han"); exit(1); } 11 if (mkdir("bit", 0755) == -1) { perror("bit"); exit(1); } 16 if (rename("han", "hanbit") == -1) { perror("hanbit"); exit(1); } 21 han -> hanbit로 변경

40 [예제 3-13] 디렉토리 생성/삭제/이름 변경하기
if (rmdir("bit") == -1) { perror("bit"); exit(1); } 26 return 0; 28 } bit는 생성했다 삭제 # ex3_13.out # ls -l drwxr-xr-x 2 root other 월 12일 18:06 hanbit

41 디렉토리 관련 함수 (2) 현재 작업 디렉토리 위치 : getcwd(3) 디렉토리 이동: chdir(2)
현재 작업 디렉토리 위치를 알려주는 명령은 pwd, 함수는 getcwd 디렉토리 이동: chdir(2) #include <unistd.h> char *getcwd(char *buf, size_t size); #include <unistd.h> int chdir(const char *path);

42 [예제 3-14] 작업 디렉토리 위치 검색/디렉토리 이동하기
01 #include <unistd.h> 02 #include <stdio.h> 03 04 int main(void) { char *cwd; char wd[BUFSIZ]; 07 cwd = getcwd(NULL, BUFSIZ); printf("1.Current Directory : %s\n", cwd); 10 chdir("hanbit"); 12 getcwd(wd, BUFSIZ); printf("2.Current Directory : %s\n", wd); 15 return 0; 17 } ex3_14.c # ex3_14.out 1.Current Directory : /export/home/jw/syspro/ch3 2.Current Directory : /export/home/jw/syspro/ch3/hanbit

43 디렉토리 정보 검색 (1) 디렉토리 열기: opendir(3) 디렉토리 닫기: closedir(3)
디렉토리 정보 읽기: readdir(3) 디렉토리의 내용을 한 번에 하나씩 읽어옴 #include <sys/types.h> #include <dirent.h> DIR *opendir(const char *dirname); #include <sys/types.h> #include <dirent.h> int closedir(DIR *dirp); typedef struct dirent { ino_t d_ino; off_t d_off; unsigned short d_reclen; char d_name[1]; } dirent_t; #include <sys/types.h> #include <dirent.h> struct dirent *readdir(DIR *dirp);

44 [예제 3-15] 디렉토리 열고 정보 읽기 01 #include <dirent.h>
ex3_15.c 01 #include <dirent.h> 02 #include <stdlib.h> 03 #include <stdio.h> 04 05 int main(void) { DIR *dp; struct dirent *dent; 08 if ((dp = opendir("hanbit")) == NULL) { perror("opendir: hanbit"); exit(1); } 13 while ((dent = readdir(dp))) { printf("Name : %s ", dent->d_name); printf("Inode : %d\n", (int)dent->d_ino); } 18 closedir(dp); 20 return 0; 22 } # ex3_15.out Name : . Inode : 208 Name : .. Inode : 189

45 [예제 3-16] 디렉토리 항목의 상세 정보 검색하기
ex3_16.c 01 #include <sys/types.h> 02 #include <sys/stat.h> 03 #include <dirent.h> 04 #include <stdlib.h> 05 #include <stdio.h> 06 07 int main(void) { DIR *dp; struct dirent *dent; struct stat sbuf; char path[BUFSIZ]; 12 if ((dp = opendir("hanbit")) == NULL) { perror("opendir: hanbit"); exit(1); } while ((dent = readdir(dp))) { if (dent->d_name[0] == '.') continue; else break; } 22

46 [예제 3-16] 디렉토리 항목의 상세 정보 검색하기
sprintf(path, "hanbit/%s", dent->d_name); stat(path, &sbuf); 25 printf("Name : %s\n", dent->d_name); printf("Inode(dirent) : %d\n", (int)dent->d_ino); printf("Inode(stat) : %d\n", (int)sbuf.st_ino); printf("Mode : %o\n", (unsigned int)sbuf.st_mode); printf("Uid : %d\n", (int)sbuf.st_uid); 31 32 closedir(dp); 33 34 return 0; 35 } 디렉토리의 항목을 읽고 다시 stat 함수로 상세 정보 검색 # ls -ai hanbit han.c # ex3_16.out Name : han.c Inode(dirent) : 213 Inode(stat) : 213 Mode : Uid : 0

47 디렉토리 정보 검색 (2) 디렉토리 오프셋: telldir(3), seekdir(3), rewinddir(3)
seekdir : 디렉토리 오프셋을 loc에 지정한 위치로 이동시킨다. rewinddir : 디렉토리 오프셋을 디렉토의 시작인 0으로 이동시킨다. #include <dirent.h> long telldir(DIR *dirp); void seekdir(DIR *dirp, long loc); void rewinddir(DIR *dirp);

48 [예제 3-17] 디렉토리 오프셋 변화 확인하기 01 #include <sys/stat.h>
ex3_17.c 01 #include <sys/stat.h> 02 #include <dirent.h> 03 #include <stdlib.h> 04 #include <stdio.h> 05 06 int main(void) { DIR *dp; struct dirent *dent; 09 if ((dp = opendir("hanbit")) == NULL) { perror("opendir"); exit(1); } printf("** Directory content **\n"); printf("Start Offset : %ld\n", telldir(dp)); while ((dent = readdir(dp))) { printf("Read : %s ", dent->d_name); printf("Cur Offset : %ld\n", telldir(dp)); printf("** Directory Pointer Rewind **\n"); rewinddir(dp); printf("Cur Offset : %ld\n", telldir(dp));

49 [예제 3-17] 디렉토리 오프셋 변화 확인하기 25 printf("** Move Directory Pointer **\n"); seekdir(dp, 24); 28 printf("Cur Offset : %ld\n", telldir(dp)); 29 dent = readdir(dp); printf("Read %s ", dent->d_name); printf("Next Offset : %ld\n", telldir(dp)); 33 closedir(dp); return(0); 36 37 } # ex3_17.out ** Directory content Start Offset : 0 Read : . Cur Offset : 12 Read : .. Cur Offset : 24 Read : han.c Cur Offset : 512 ** Directory Pointer Rewind ** Cur Offset : 0 ** Move Directory Pointer ** Read han.c Next Offset : 512

50 과제물 #2 교재 pp.166, 연습문제 #1에서 제시한 프로그램을 작성하여라.
현재 디렉토리에 있는 항목들을 나열하고 , 각 항목이 파일인지 디렉토 리인지 식별하여 출력하고, 파일인 경우에는 어떤 종류를 파일이 식별 하여 출력하는 프로그램을 작성하여라.

51


Download ppt "3 파일과 디렉토리."

Similar presentations


Ads by Google