System Programming
Lab #6
Bacha
2019. 11. 16. 23:10
다음과 같은 프로그램을 작성하라 :
- 인자로서 디렉토리 이름과 파일의 접미사(suffix)를 읽어 들인다.
- 주어진 디렉토리를 탐색하여 이름에 인자로 주어진 접미사가 포함된 첫번째 파일을 찾아 그 파일의 i-node 값과 파일 이름을 출력한다.
- 주어진 파일이름(*s1)이 접미사 (*s2)를 포함하는지 check 해 주는 함수 int match(char* s1, char* s2)를 활용할 것
int match(char* s1, char* s2) {
int diff = strlen(s1) - strlen(s2);
if(strlen(s1) > strlen(s2)) return (strcmp(&s1[diff], s2) == 0);
else return 0;
}
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <error.h>
int match(char* s1, char* s2) {
int diff = strlen(s1) - strlen(s2);
if(strlen(s1) > strlen(s2)) return (strcmp(&s1[diff], s2) == 0);
else return 0;
}
int main(int argc, char* argv[]) {
DIR* info = NULL;
struct dirent* entry = NULL;
info = opendir(argv[1]);
if(info != NULL) {
while(entry = readdir(info)) {
if(match(entry->d_name, argv[2]))
printf("i-node : %ld, name : %s\n", entry->d_ino, entry->d_name);
}
closedir(info);
}
return 0;
}
컴파일 :
gcc dir.c
실행 :
예를 들어 아래 사진과 같이 디렉토리와 파일을 만들어놨을 때,

% ./a.out mydir txt