Index:
[thread]
[date]
[subject]
[author]
From: Garrett Mitchener <wgm2@acpub.duke.edu>
To :
Date: 03 Mar 1999 22:17:35 -0500
Re: S_IFLNK and lstat
Ted Shion Hung <tsh5@duke.edu> writes:
> I've been using lstat() and checking the S_IFLNK flag to figure
> out whether the file is a symbolic link. The only problem is
> that it is not working. It seems to flag anything that is not
> a directory as a symbolic link. Is there anything else I need
> to do to distinguish a symbolic link? Any and all help would
> be greatly appreciated.
>
> Ted Hung
I've looked at your code, and I see that there are two places in your
directory.cc file where you call lstat() and decode the information.
In the past, people have had problems because they got it right in one
place, but not in another. As a coding suggestion, factor that code
out into a function and call it from the two places.
My advice is to use the S_ISLNK(mode) macro, documented in the C
library, which you can find off the 108 help page. The permission
bits of the st_mode field are just flags: if bit i is true, it means
the file has permission i in addition to whatever others are set.
That means myIsUserRead = st_mode & S_IRUSR works. The type bits are
somewhat different. A file can only have one of a handfull of types.
The type is stored as say a 4 or 5 bit integer in some bits of
st_mode, and you must check that all those bits line up with the type
contstant. Maybe the code for a plain file is 0001, for a link is
0010, for a directory is 0011, for a socket is 0100, etc. in bits 11,
12, 13, and 14 of st_mode. Some of them have several bits set.
That's why myIsLink = st_mode & S_IFLNK doesn't work. Use S_ISLNK()
instead.
-- Garrett :-)
Index:
[thread]
[date]
[subject]
[author]