Funktion einbinden



  • Hey Leute, also wir sind schon weiter und die Funktionen eingebunden. Jetzt mal eine Frage zu ls
    Also wir können nur einmal ls machen und dann kommt immer "no such file or directory" also das sagt er bei der Funktion list.h bei uns bei scandir.

    Wie kann ich dieses Problem lösen?

    Hier mal die beiden Quellcodes, einmal wo man befehle eingibt und einmal die list.h
    Quellcode: [url]http://paste.pocoo.org/show/157588/ [/url]
    list.h:

    void list(char *argv)
    {
    
        struct dirent **namelist;
        int n;
        int i;
    
        n = scandir(argv, &namelist, 0, alphasort);
    
        if (n < 0)
            perror("scandir");
        else
        {
          for (i = 0; i < n; i++)
          {
        printf("%s\n", namelist[i]->d_name);
          }
        }
    
        return;
    }
    


  • Wenn ich in die funktion ls vor return; ein free(namelist) einsetze, dann kommt das hier:

    scandir: No such file or directory
    *** glibc detected *** ./Shell: double free or corruption (out): 0x00007fffc3e0afa0 ***
    ======= Backtrace: =========
    /lib/libc.so.6[0x7f9781f1ddd6]
    /lib/libc.so.6(cfree+0x6c)[0x7f9781f2270c]
    ./Shell[0x401255]
    ./Shell[0x401c82]
    ./Shell[0x4024c0]
    /lib/libc.so.6(__libc_start_main+0xfd)[0x7f9781ec6abd]
    ./Shell[0x400f39]
    ======= Memory map: ========
    00400000-00403000 r-xp 00000000 08:01 5398571                            /home/marcel/Desktop/Shell-Programmierung/Shell/Shell
    00602000-00603000 r--p 00002000 08:01 5398571                            /home/marcel/Desktop/Shell-Programmierung/Shell/Shell
    00603000-00604000 rw-p 00003000 08:01 5398571                            /home/marcel/Desktop/Shell-Programmierung/Shell/Shell
    0159b000-015bc000 rw-p 00000000 00:00 0                                  [heap]
    7f977c000000-7f977c021000 rw-p 00000000 00:00 0 
    7f977c021000-7f9780000000 ---p 00000000 00:00 0 
    7f9781c91000-7f9781ca7000 r-xp 00000000 08:01 6938638                    /lib/libgcc_s.so.1
    7f9781ca7000-7f9781ea6000 ---p 00016000 08:01 6938638                    /lib/libgcc_s.so.1
    7f9781ea6000-7f9781ea7000 r--p 00015000 08:01 6938638                    /lib/libgcc_s.so.1
    7f9781ea7000-7f9781ea8000 rw-p 00016000 08:01 6938638                    /lib/libgcc_s.so.1
    7f9781ea8000-7f978200e000 r-xp 00000000 08:01 6938741                    /lib/libc-2.10.1.so
    7f978200e000-7f978220d000 ---p 00166000 08:01 6938741                    /lib/libc-2.10.1.so
    7f978220d000-7f9782211000 r--p 00165000 08:01 6938741                    /lib/libc-2.10.1.so
    7f9782211000-7f9782212000 rw-p 00169000 08:01 6938741                    /lib/libc-2.10.1.so
    7f9782212000-7f9782217000 rw-p 00000000 00:00 0 
    7f9782217000-7f9782236000 r-xp 00000000 08:01 6938663                    /lib/ld-2.10.1.so
    7f9782412000-7f9782414000 rw-p 00000000 00:00 0 
    7f9782430000-7f9782435000 rw-p 00000000 00:00 0 
    7f9782435000-7f9782436000 r--p 0001e000 08:01 6938663                    /lib/ld-2.10.1.so
    7f9782436000-7f9782437000 rw-p 0001f000 08:01 6938663                    /lib/ld-2.10.1.so
    7fffc3df8000-7fffc3e0d000 rw-p 00000000 00:00 0                          [stack]
    7fffc3e14000-7fffc3e15000 r-xp 00000000 00:00 0                          [vdso]
    ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
    Aborted
    

    Ich kann ls immer nur einmal ausführen 😡



  • ich schrieb:

    Frag die Unix-Leute, die wissen sowas.

    Nicht viel los hier. War nicht schwer zu googlen:
    http://www.linuxquestions.org/questions/programming-9/c-list-files-in-directory-379323/

    Keine Ahnung, was scandir() macht, aber der macht das mit readdir(), das sieht einfach aus. Jedenfalls bleibt dir so relativ sicher der Coredump erspart.
    🙂



  • okay hab das Problem lösen können.


Anmelden zum Antworten