given inode and fs, determine if dir or not?

Guy Harris guy at auspex.UUCP
Fri Nov 25 10:27:42 AEST 1988


 >If I have an inode (and fs root) on hand, how do I tell whether that
 >inode is a directory or not?  The definition of struct inode suggests
 >that it's possible, but I can't find a man entry that says how.
 >
 >If it matters, this would be for SunOS 3.x and Ultrix 2.x

It doesn't matter greatly; both those systems use the 4.2BSD file
system, but Berkeley pretty much left the part of the inode that comes
into play here alone, so the same technique can be used on V7/4.1BSD/S5
file systems.

I presume (since you refer to "struct inode") by "inode" you mean "inode
structure", either on disk or in core, rather than inumber.  If you have
an inumber, you have to get the inode structure first....

The "i_mode" field of the inode is laid out the exact same way the
"st_mode" field of a "stat" structure is.  Even the bits used in that
field are the same in most UNIX implementations; you shouldn't, and
don't have to, rely on this, however.  Just check whether

	(i_mode & IFMT) == IFDIR

(the analogy of checking whether "(st_mode & S_IFMT) == S_IFDIR)" for a
"stat" structure).



More information about the Comp.unix.wizards mailing list