Directory search

twins99의 이미지

Directory search 를 구현하려 합니다.

구현 내용은, 특정 Directory Path가 주어지면 그 하위 Directory들의 Path를 string 배열로 저장해 return해 주는 것인데요.

어떻게 구현해야 할까요.. 사용하려는 언어는 Linux C++입니다.

될수있으면 일반적인 라이브러리 말고는 사용하지 않으려 합니다.(임베디드로 올라가는 소스라서요..)

참고할만한 source코드나 조언 부탁드립니다.

(제 생각엔 DFS를 구현하면 될 것 같기는 한데 막막하네요.)

ktd2004의 이미지

제가 얼마전에 사용했던 코드에서 재귀함수부분만 잘라낸것입니다.
이런식이면 안될까요?

void printdir( const char *dir, int depth)
{
	DIR *dp;
	struct dirent *entry;
	struct stat statbuf;

	if( ( dp = opendir(dir)) == NULL) {
		/* 디렉토리를 열 수 없음. 존재하지 않는다. */
		return;
	}

	chdir(dir);

	while( ( entry = readdir( dp)) != NULL) {

		lstat( entry->d_name, &statbuf);

		if( S_ISDIR( statbuf.st_mode)) {
			/* 디렉토리이다. */

			if( strcmp( ".", entry->d_name) == 0 ||
					strcmp( "..", entry->d_name) == 0) {
				continue;
			}
                         printf("%s\n", entry->d_name);
			/* 채널 디렉토리이다. 이동한다. */
			printdir( entry->d_name, depth+4);
		} else {
			/* 파일이다. */
		}
	}
	chdir("..");
	closedir(dp);
}