build tree of directories sub-directories using linked-lists

This is a discussion on build tree of directories sub-directories using linked-lists within the C Programming forums, part of the General Programming Boards category; Hello. I'm trying to design an algorithm that I could use in various programs under linux using readdir(), opendir(), etc. ...

  1. #1
    Registered User geekoftheweek's Avatar
    Join Date
    Mar 2003
    Location
    maine
    Posts
    8

    build tree of directories sub-directories using linked-lists

    Hello.

    I'm trying to design an algorithm that I could use in various programs under linux using readdir(), opendir(), etc. and create linked-lists of all the files in directories and sub-directories and am kind of frustrated with HOW to do it. For instance, I can use a readdir() to read all the files in one directory and build a linked-list of all of its files and this could be also a function on its own. Where I seem to get confused is when it comes to processing sub-directories. Should I process an entire directory and build a linked-list of all of its files and then scan that linked-list for sub-directories to open and build linked-lists of those sub-directories, or should I process those sub-directories as I'm building the linked-list of a particular directory using recursion? i.e. As I'm building a directory's linked-list of files and come across a sub-directory open it and build a linked-list of its files, etc. Any ideas?

    Thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You really don't need a "linked-list" for this, the content is not dynamic. The only variable factor is the length of the list.

    Since readdir already supplies struct dirent, just use an array of them.

    Code:
    #include <stdio.h>
    #include <dirent.h>
    #include <stdlib.h>
    
    int main (int argc, char *argv[]) {
            short int i=0,n;
            struct dirent *dentry;
            struct dirent *Dray;
            DIR *dir = opendir(argv[1]);
            if (dir == NULL) {perror("opendir fail");return -1;}
    
            Dray=malloc(sizeof(*Dray));
            while((dentry=readdir(dir)) != NULL) {
                    if (i>0) Dray=realloc(Dray,(i+1)*sizeof(*Dray));
                    Dray[i]=*dentry;
                    i++;
            }   
            closedir(dir);
    
            for (n=0;n<i;n++) printf("%s: %d\n",Dray[n].d_name,Dray[n].d_type);
    }
    Alternately, you could make Dray it's own kind of struct and just retain the fields from dirent that you want.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User geekoftheweek's Avatar
    Join Date
    Mar 2003
    Location
    maine
    Posts
    8
    thanks MK27. Will that algorithm also handle all sub-directories infinitely underneath the directory in question?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    *cough* tree walker in the FAQ *cough*
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM
  2. Replies: 2
    Last Post: 01-18-2003, 01:32 AM
  3. Search and Build Tree
    By 1999grandamse in forum C++ Programming
    Replies: 17
    Last Post: 11-14-2002, 01:36 PM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 08:11 AM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 09:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21