S_ISDIR returns directories and files?

This is a discussion on S_ISDIR returns directories and files? within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a program where I am testing to see whether something is a file or directroy using S_ISDIR. ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    56

    S_ISDIR returns directories and files?

    Hello,

    I have a program where I am testing to see whether something is a file or directroy using S_ISDIR. Here is my code:
    Code:
    void Find::get_dir()
    {
    	struct dirent *ep;
    	struct stat statbuf;
    	DIR *dp;
    	dp = opendir(path.c_str());
    	if(dp == NULL)
    		throw domain_error("ERROR - " + path + " is not a valid path.");
    		
    	while((ep = readdir(dp)))
    	{
    		stat(ep->d_name, &statbuf);
    		if(S_ISDIR(statbuf.st_mode)) 
    			dir.push_back(ep->d_name);
    	}
    	closedir(dp);
    	
    	sort(dir.begin(), dir.end());
    	
    	typedef vector<string>::const_iterator iter;
    	for(iter i = dir.begin(); i != dir.end(); i++)
    		cout << *i << endl;
    }
    My output:
    .
    ..
    .autofsck
    .dbus
    bin
    boot
    dev
    home
    lib
    lost+found
    media
    mnt
    opt
    proc
    sbin
    selinux
    srv
    sys
    tmp
    usr
    var
    Are files and directories both considered file types? I want to weed out files like .dbus and .autofsck and only keep directories. Any ideas?

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Are you sure that .dbus and .autofsck aren't directories? What does ls say?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    S_ISDIR not working as expected. what is going on? - LinuxQuestions.org
    You should really check your stat() for success as well.
    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.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    56
    Are you sure that .dbus and .autofsck aren't directories? What does ls say?
    This is what I get when I try to cd to .autofsck. The same thing happens with .dbus.
    [hopchewer@hopchewer /]$ ls -a
    . .autofsck boot dev home lost+found mnt proc sbin srv tmp var
    .. bin .dbus etc lib media opt root selinux sys usr
    [hopchewer@hopchewer /]$ cd .autofsck
    bash: cd: .autofsck: Not a directory
    [hopchewer@hopchewer /]$

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    1

    Don't forget to prepend the path.

    I'm thinking that what Salem said should be your clue.

    Note that you are looking in 'path' (which is a global??), but then you are not prepending path to the file names that you are looking at, so most likely the stat is failing, and you are calling S_ISDIR() on memory garbage, which may or may not have that bit set, but which *will* be the same for all files.

    I'd suspect that if you initialized the stat struct to 0 ( memset(&statbuf, 0, sizeof(statbuf)); ) that you would find no files pushed to your vector (which is also global??)

    so yeah ... all that is to say

    1) call stat with the path prepended:
    Code:
    char fullpath[1024];
    snprintf(fullpath, 1024, "%s/%s", path.c_str(), ep->d_name);
    2) check the return of stat to make sure you're doing it right:
    Code:
    if (stat(fullpath, &statbuf) == 0)
    {
    // do stuff
    }
    else
    {
        printf("Huh, can't find and stat %s\n", fullpath);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and deleting directories and deleting files
    By fguy817817 in forum C Programming
    Replies: 1
    Last Post: 04-08-2009, 07:26 AM
  2. Directories and files using dirent.h
    By totalnewbie in forum C Programming
    Replies: 6
    Last Post: 11-19-2008, 04:10 PM
  3. problem while opening files from multiple directories
    By V.G in forum Windows Programming
    Replies: 2
    Last Post: 11-08-2004, 02:29 PM
  4. listing files in directories portably
    By ab384 in forum C Programming
    Replies: 2
    Last Post: 11-03-2004, 09:50 AM
  5. Replies: 4
    Last Post: 06-11-2004, 06:18 PM

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