Ask a Question related to UNIX Programming, Design and Development.
-
Sam Zoghaib #1
nftw(3) and FTW_D
Hello,
I'm having a problem finding out whether a particular entry stat'ed by the
nftw(3) function is a directory checking the value of flag. nftw() is defined
as such:
int nftw(const char *dir, int (*fn)(const char *file, const struct stat *sb,
int flag, struct FTW *s), int depth, int flags);
It calls fn() on each directory entry, and flag is supposed to be FTW_D if the
entry is a directory.
If in fn() I check whether the entry is a directory with if (flag == FTW_D)
the condition is never met (flag equals 5 if the entry is indeed a directory).
In ftw.h, FTW_D is not #define'd as 5.
I know I can check if the entry is a directory with the value of sb->st_mode &
S_IFMT and it works, but I find it strange that checking FTW_DIR fails.
Am I missing something?
Thanks,
Sam
--
"People sometimes ask me if it is a sin in the Church of Emacs to use vi.
Using a free version of vi is not a sin; it's a penance".
- Richard Stallman
Sam Zoghaib Guest
-
Sam Zoghaib #2
Re: nftw(3) and FTW_D
Måns Rullgård wrote in article <yw1x65lc4i36.fsf@users.sourceforge.net> on
Tuesday 05 August 2003 10:35 in comp.unix.programmer:
[excerpt from ftw.h snipped]
Ok, got it. In my case, though, checking for FTW_DP should be enough, because> Notice that there are several values indicating a directory. In
> particular, the value 5 that you are seeing means that all
> subdirectories have been visited. You'll need to check for several
> values, not only FTW_D.
>
I'm calling nftw with flags == FTW_DEPTH, so fn() is called on all entries in
directories before being called on the directory. I think fn() will never be
called with flag == FTW_D
Thank you,
Sam
--
"Don't be afraid, I'm gonna give you the choice I never had..."
- Lestat in "Interview with the Vampire" (Ann Rice, 1976)
Sam Zoghaib Guest
-
Måns Rullgård #3
Re: nftw(3) and FTW_D
Sam Zoghaib <NOsam@zep.homedns.orgSPAM> writes:
Unless you are sure, it's always wisest to check for all possible>>> Notice that there are several values indicating a directory. In
>> particular, the value 5 that you are seeing means that all
>> subdirectories have been visited. You'll need to check for several
>> values, not only FTW_D.
>>
> Ok, got it. In my case, though, checking for FTW_DP should be enough, because
> I'm calling nftw with flags == FTW_DEPTH, so fn() is called on all entries in
> directories before being called on the directory. I think fn() will never be
> called with flag == FTW_D
values, even if you think some are impossible.
--
Måns Rullgård
[email]mru@users.sf.net[/email]
Måns Rullgård Guest
-
Måns Rullgård #4
Re: nftw(3) and FTW_D
Sam Zoghaib <NOsam@zep.homedns.orgSPAM> writes:
That would seem reasonable. However, I'd check for it anyway, just in>>>>>> Ok, got it. In my case, though, checking for FTW_DP should be enough,
>>> because I'm calling nftw with flags == FTW_DEPTH, so fn() is called on all
>>> entries in directories before being called on the directory. I think fn()
>>> will never be called with flag == FTW_D
>> Unless you are sure, it's always wisest to check for all possible
>> values, even if you think some are impossible.
> the man page says:
>
> FTW_DEPTH
>
> If set, do a depth-first search, that is, call the function for the
> directory itself only after handling the contents of the directory
> and its subdirectories.
>
> I guess that means flag never equals FTW_D. When fn() is called on
> the directory, all its entries will have already been processed, so
> it should det flag to FTW_DP.
case the program is ever run on some machine with a broken ftw
implementation. For almost every function, there is some version of
some Unix system where that function is broken. Doing the extra check
doesn't take much time compared to a file access anyway, so it's not
going to slow things down.
--
Måns Rullgård
[email]mru@users.sf.net[/email]
Måns Rullgård Guest



Reply With Quote

