Thread: getwd and dirent->d_name

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    8

    getwd and dirent->d_name

    Hi Everyone:

    This problem is puzzling me if I use this code which includes getwd:
    Code:
    while (1) {
    		dirslot=readdir(dir);
    		
    		
    		if (dirslot==NULL) return;
    		if (strcmp(dirslot->d_name, ".")==0) ||(strcmp(dirslot->d_name, "..")==0) continue;
    		stat(dirslot->d_name, &statbuff);
     		if (S_ISDIR(statbuff.st_mode))  {
    				
    				if (getwd(pathname) == NULL )
    						{ printf("Error getting path\n");
    								exit(0);
    						}
    				printf("\n\n %s\n",pathname);
    					
               		printf("______________________________________\n");
    					 	if (chdir(dirslot->d_name)==-1) {
    						perror("FAIL: Change Directory");
    						return;}	
                    	recls(".");
                    	chdir("..");
                    	}
    		}
    	closedir(dir);
    	}
    It will display the full filepath of directories and directories within directories but not the next level down e.g dir----dir-----dir(no display), if you understand what I mean. But if I use this code:

    Code:
    while (1) {
    		dirslot=readdir(dir);
    		
    		
    		if (dirslot==NULL) return;
    		if (strcmp(dirslot->d_name, ".")==0) ||(strcmp(dirslot->d_name, "..")==0) continue;
    		stat(dirslot->d_name, &statbuff);
     		if (S_ISDIR(statbuff.st_mode))  {
    				printf("\n\n %s\n",dirslot_d_name)
    					printf("______________________________________\n");
    					 	if (chdir(dirslot->d_name)==-1) {
    						perror("FAIL: Change Directory");
    						return;}	
                    	recls(".");
                    	chdir("..");
                    	}
    		}
    	closedir(dir);
    	}
    It will print out the full directory structure but only the directory names which is not what I want.

    My Question is, can anybody suggest where Im going wrong with the getcw directory code and how can I fix it so that it will display all directory paths

    Please dont flame my code, I know theres not to many error catchment statements but I am fairly new to this C coding.

    Thanks for any help.
    Christian

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I have no idea why you see the differences you describe in directory traversal but the recls() doesn't make sense to me. Try replacing it with a recursive call to your function. As for flaming your code, that is my prerogative: indent carefully and don't mix spaces and tabs. Give that a try and if it doesn't work one of the eunuchs here will probably help you. (This must be their day off.)

Popular pages Recent additions subscribe to a feed