리눅스 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;
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.