depth-first traversal of directories

This is a discussion on depth-first traversal of directories within the C Programming forums, part of the General Programming Boards category; Some code I whipped up in about an hour to do a depth-first traversal of given directory. Was wondering if ...

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

    depth-first traversal of directories

    Some code I whipped up in about an hour to do a depth-first traversal of given directory. Was wondering if anyone can critique this code and maybe suggest some things to do differently, etc. Am I correct in assuming that depth-first traversal has possible bugs due to running out of memory, etc ? Also, this program is just a template for the do_dir() function itself that would traverse directories, and does nothing useful at the moment save print the directories that it currently opens. In the future I could use it for any number of utilities that would have a directory traversal need.

    Thanks

    Code:
    /* show.c */
    #include <stdio.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <dirent.h>
    #include <string.h>
    #include <unistd.h>
    #include <errno.h>
    #include <stdlib.h>
    
    #define VALIDDIR 1
    #define SUBTRACT 0
    #define ADD 1
    
    char fullpath[PATH_MAX];
    char *curpath = &fullpath[0]; 
    
    int do_stat(const char *dname);
    void do_dir(const char *dirname);
    void update_fpath(const char *dname, int method);
    
    int main(int argc, char *argv[])
    {
    	if (argc != 2) {
    		fprintf(stderr, "Usage: show directory\n");
    		exit(1);
    	}
    
    	do_dir(argv[1]);
    	return 0;
    }
    
    void do_dir(const char *dirname)
    {
    	DIR *p;
    	struct dirent *t;
    	char lastdir[PATH_MAX];
    
    	getcwd(lastdir, PATH_MAX);
    	update_fpath(dirname, ADD);
    
    	if ((p = opendir(dirname)) == NULL) {
    	       fprintf(stderr, "Couldn't open %s: %s\n",
    		       fullpath, strerror(errno));
    	} else {
    		printf("opening %s\n", fullpath);
    		chdir(dirname);
     		while (t = readdir(p)) 
    			if ((do_stat(t->d_name)) == VALIDDIR)
    				do_dir(t->d_name);
    		chdir(lastdir);
    		closedir(p);
    	}
    	update_fpath(dirname, SUBTRACT);
    
    }
    
    int do_stat(const char *dname)
    {
    	struct stat t;
    
    	lstat(dname, &t);
    
    	switch(t.st_mode & S_IFMT) {
    	case S_IFDIR:
    		if (strcmp(dname, ".")  != 0 &&
    		    strcmp(dname, "..") != 0)
    			return VALIDDIR;
    		break;
    	default:
    		break;
    	}
    
    	return 0;
    }
    			
    void update_fpath(const char *dname, int method)
    {
    	switch (method) {
    	case ADD:
    		strcpy(curpath, dname);
    		curpath += strlen(dname);
    		strcpy(curpath, "/");
    		curpath += strlen("/");
    	break;
    	case SUBTRACT:
    		curpath -= (strlen(dname) + 1);
    		*curpath = '\0';
    	break;
    	default:
    	break;
    	}
    }
    Last edited by geekoftheweek; 03-31-2009 at 02:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 11
    Last Post: 05-09-2009, 03:34 AM
  2. Simple 2D rubiks cube algorithm.
    By jeanmarc in forum Game Programming
    Replies: 19
    Last Post: 11-11-2008, 07:40 PM
  3. plain tree traversal
    By kmel in forum C++ Programming
    Replies: 2
    Last Post: 02-18-2008, 12:15 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. C++ in Depth or Java In Depth... ?
    By jawwadalam in forum C++ Programming
    Replies: 8
    Last Post: 10-07-2002, 09:38 AM

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