리눅스 c언어 cwd(current working directory) 구현 문제 도와주세요 ㅠ

tlqkdto89의 이미지

struct path_entry{ 
    char* name; 
    struct path_entry* next; 
}; 
 
 int push_path_entry(struct path_entry** entry, char* name){ 
 
} 
 
 
 void free_path_entry(struct path_entry* head){ 
 
} 
 
 char* mycwd(char* name, size_t size){ 
 
   DIR* dp; 
  struct stat cstatbuf; 
  struct stat pstatbuf; 
  struct dirent* dep; 
  struct path_entry* head = NULL; 
  char* path = NULL; 
 
   //////////////////////// major routine  start ////////////////////////////////////////////////////// 
  while(1){ 
 
     lstat(".", &cstatbuf); //cstatbuf.st_ino =>3 
    lstat("..",&pstatbuf);  
 
     //check whether or not current diretory is root! 
    if(cstatbuf.st_ino == pstatbuf.st_ino){ 
        push_path_entry(&head, "");    //Write your push_path_entry() 
        break; 
    } 
 
    dp = opendir(".."); 
 
     while(dep=readdir(dp)){ 
        if(dep->d_ino == cstatbuf.st_ino) 
            push_path_entry(&head, dep=>d_name); //Write your push_path_entry() 
        break; 
    } 
    closedir(dp); 
    chdir(".."); 
  } 
  //////////////////////// major routine  end ////////////////////////////////////////////////////// 
 
 
   path = make_path(head);   
   free_path_entry(head); 
 
   if(strlen(path) > size) { 
    errno = ERANGE; 
    name = NULL; 
  } else { 
    strcpy(name, path); 
  } 
 
  if(path) free(path); //free allocated memory for path 
 
 
   return name; 
} 
 
 int main(){ 
    char pathbuf[1024]; 
    mycwd(pathbuf, 1024); 
 
    printf("cwd : %s\n", pathbuf); 
 
} 

cwd 즉 current working directory 찾아가는 소스코드 작성하는 것인데..
도저히 모르겠습니다... 도와주세요 ㅠㅠ
push_path_entry와 free_path_entry를 완성하지 못하겠습니다.. 도와주세요 ㅜ
jick의 이미지

이미 있는 함수를 왜 구현하시려구요?

익명 사용자의 이미지

이러시면 교수님께 신고합니다...

bushi의 이미지

본문 코드에 버그가 하나 있으니 수정하셔야 돌아갑니다.
알아서 잘 찾아 수정하시고.

평범한 B 학점짜리, A 학점은 절대로 받지 못한다는 게 장점.

int push_path_entry(struct path_entry** entry, char* name){
        char *a = (char *)*entry;
        int l = a ? strlen(a) : 0;
        char *b;
        b = malloc(l + strlen(name) + 2);
        sprintf(b, "%s/%s", name, l ? a : "");
        free(a);
        *entry = (struct path_entry *)b;
        return 0;
}
 
void free_path_entry(struct path_entry* head){
        free(head);
}
 
char *make_path(struct path_entry* head) {
        return strdup((char *)head);
}

그럭저럭 돌아가기만 하는 D 학점짜리, 까칠한 교수님은 F 학점 주실지도.

int push_path_entry(struct path_entry** entry, char* name){ 
        struct path_entry *pe;
        pe = malloc(sizeof(*pe) + strlen(name) + 1);
        pe->name = (void*)(pe + 1);
        pe->next = *entry;
        strcpy(pe->name, name);
        *entry = pe;
        return 0;
} 
 
void free_path_entry(struct path_entry* head){ 
        while (head) {
                void *cur = head;
                head = head->next;
                free(cur);
        }
} 
 
char *make_path(struct path_entry* head) {
        char *p = NULL;
        int l = 0;
        while (head) {
                p = realloc(p, l + strlen(head->name) + 2);
                strcpy(p + l, head->name);
                strcat(p, "/");
                l = strlen(p);
                head = head->next;
        }
        return p;
}