Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Possible problem with readdir() and/or lstat()



I have't tested this, but I wonder if the problem is that you need to prepend the value of dir (i.e., "somedir") to each entry->d_name, and then call lstat on the result. It seems probable that lstat is not able to find the files in "somedir", because "somedir" is not the current working directory.

Jamie

From: Jeff Sparks <email@hidden>
To: email@hidden
Subject: Possible problem with readdir() and/or lstat()
Date: Fri, 30 Mar 2007 13:43:14 -0700 (PDT)


% cat test.c

#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <assert.h>

int main( int argc, char** argv )
{
    DIR* dir ;
    struct dirent* entry ;
    struct stat buf ;

    assert(argc == 2) ;
    dir = opendir(argv[1]) ;
    assert(dir != 0) ;

    while( (entry = readdir(dir)) != 0 )
    {
        lstat(entry->d_name, &buf) ;
        printf("name: %s --- S_ISDIR: %d\n",
               entry->d_name,
               S_ISDIR(buf.st_mode)) ;
    }

    return 0 ;
}

% gcc test.c -o test
% file *
file1:   ASCII text
file2:   ASCII text
somedir: directory
test:    Mach-O executable i386
test.c:  ASCII C program text
% ./test .
name: . --- S_ISDIR: 1
name: .. --- S_ISDIR: 1
name: file1 --- S_ISDIR: 0
name: file2 --- S_ISDIR: 0
name: somedir --- S_ISDIR: 1
name: test --- S_ISDIR: 0
name: test.c --- S_ISDIR: 0
% file somedir/*
somedir/file3:    ASCII text
somedir/file4:    ASCII text
somedir/somedir2: directory
% ./test somedir
name: . --- S_ISDIR: 1
name: .. --- S_ISDIR: 1
name: file3 --- S_ISDIR: 1
name: file4 --- S_ISDIR: 1
name: somedir2 --- S_ISDIR: 1

Note that file3 and file4 are not directories.





____________________________________________________________________________________
Need Mail bonding?
Go to the Yahoo! Mail Q&A for great tips from Yahoo! Answers users.
http://answers.yahoo.com/dir/?link=list&sid=396546091
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-userlevel mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/darwin-userlevel/email@hidden

This email sent to email@hidden

_________________________________________________________________
5.5%* 30 year fixed mortgage rate. Good credit refinance. Up to 5 free quotes - *Terms https://www2.nextag.com/goto.jsp?product=100000035&url=%2fst.jsp&tm=y&search=mortgage_text_links_88_h2a5d&s=4056&p=5117&disc=y&vers=910


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-userlevel mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/darwin-userlevel/email@hidden

This email sent to email@hidden
References: 
 >Possible problem with readdir() and/or lstat() (From: Jeff Sparks <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.