c언어 연습문제인데요... 재귀호출....
하는데 각각의 디렉토리 합은 구하겟는데요..
전체 디렉토리 합을 구하는데... 잘.. ^^;
많은 조언 부탁드려요
문제가 문장이 매끄럽지않아서 제가 이해를 못하는것두 있는데.. ^^ 설명좀
#############################################################################################
책에서 정해진 함수 원형
// 그런데 밑에 함수선언할때요 함수포인터를 이렇게 쓰나요?
int depthfirstapply(char *path, int pathfun(char *path1));
int sizepathfun(char *path);
1번
sizepathfun 함수를 매개변수 pathfun으로 사용하는 depthfirstapply를 이용하여 다음 명령어를 구현하여라
showtreesize pathname
showtreesize 명령어는 pathnamed의 총 크기를 표준출력으로 출력한다.
pathname이 디렉토리라면 총 크기는 pathname을 기준으로 한 하위 트리의 총 크기이다.
=> (여기서 한 하위면 바로 밑에 트리에 전체 크기를 말하는거겠지요? )
pathname => /home/aaa 이면
/home/aaa 의 list 내용이 이렇게 되있었으면 bbb 하위는 표시는 안하고 용량계산해서
bbb에 포함하라는말인가요?(2번문제에서는 제외하러고 했으니깡..)
그리구 pathnamed이 파일이면? 그냥 용량만 출력인가??
아.. 제가 이해력이 떨어지나봐요 ㅠㅠ
----------------------------------
list 내용
----------------------------------
1.txt -> file
2.txt -> file
bbb -> dir
ccc -> dir
----------------------------------
2번
커맨드라인 인자 rootpath와 함께 호출되는 mydu라는 명령어를 작성하여라.
mydu rootpath
mydu 프로그램은 매개변수로 sizepathfun 함수를 가지는 수정된 depthfirstapply함수를 호출한다.
경로명의 디렉토리의 크기를 각각 출력한다.
디렉토리의 크기에서 하위트리의 크기는 계산하지 않는다.
프로그램은 트리의 총 크기를 출력하고 종료한다.
3번
depthfirstapply 함수와 비슷하지만 넓이 우선방식을 사용하는 depthfirstapply 함수를 작성하여라.
#include
#include
#include
#include
#include
#include
int depthfirstapply(char *path, int pathfun(char *path1));
int sizepathfun(char *path);
int main(int argc, char *argv[])
{
long dusize = 0;
dusize = depthfirstapply(argv[1], sizepathfun);
printf("dusize : %ld\n", dusize);
return 0;
}
int depthfirstapply(char *path, int pathfun(char *path1))
{
int size = 0;
long filesize = 0;
long dirsize = 0;
long totalsize = 0;
int pathsize = 0;
char *pbuf = NULL;
DIR *pdir;
struct dirent *pdirent;
struct stat statbuf;
if((pdir = opendir(path)) == NULL)
{
perror("opendir error ");
return -1;
}
if((pathsize = pathconf(path, _PC_PATH_MAX)) == -1)
{
perror("pathconf error ");
return -1;
}
if((pbuf = (char*)malloc(pathsize + 1)) == NULL)
{
perror("malloc memory error ");
return -1;
}
while((pdirent = readdir(pdir)) != NULL)
{
memset(pbuf, 0x00, pathsize + 1);
if(!strncmp(pdirent->d_name, ".", 1) || !strncmp(pdirent->d_name, "..", 2))
continue;
strcpy(pbuf, path);
strcat(pbuf, "/");
strcat(pbuf, pdirent->d_name);
if(stat(pbuf, &statbuf) == -1)
{
perror("stat error ");
return -1;
}
if(S_ISDIR(statbuf.st_mode))
{
if((dirsize = depthfirstapply(pbuf, pathfun)) == -1)
{
printf("depthfirstapply error\n");
return -1;
}
printf("Directory : %s\n", pbuf);
printf("dirsize : %ld\n\n", dirsize);
}else
{
if((filesize = sizepathfun(pbuf)) == -1)
{
printf("sizepathfun error\n");
return -1;
}
#ifdef DEBUG
printf("size : %10ld\t file : %s\n", filesize, pbuf);
#endif
filesize += size;
}
}
return filesize;
}
int sizepathfun(char *path)
{
struct stat statbuf;
if(stat(path, &statbuf) == -1)
{
perror("stat error ");
return -1;
}
if(S_ISREG(statbuf.st_mode))
return statbuf.st_size;
else if(S_ISBLK(statbuf.st_mode))
printf("block specail file\n");
else if(S_ISCHR(statbuf.st_mode))
printf("char specail file\n");
else if(S_ISFIFO(statbuf.st_mode))
printf("pipe or FIFO specail file\n");
else if(S_ISSOCK(statbuf.st_mode))
printf("socket\n");
else if(S_TYPEISMQ(statbuf.st_mode))
printf("message queue\n");
else if(S_TYPEISSEM(statbuf.st_mode))
printf("semaphore\n");
else if(S_TYPEISSHM(statbuf.st_mode))
printf("shard memory object\n");
else
printf("unknow\n");
return -1;
}

