Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!batcomputer!caen!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!mp.cs.niu.edu!ux1.cso.uiuc.edu!phil From: phil@ux1.cso.uiuc.edu (Phil Howard KA9WGN) Newsgroups: comp.unix.programmer Subject: Re: hard links: how to tell with system call under BSD4.3? Message-ID: <1991May10.210452.28889@ux1.cso.uiuc.edu> Date: 10 May 91 21:04:52 GMT References: <1991May10.013347.8621@athena.mit.edu> Distribution: usa Organization: University of Illinois at Urbana Lines: 47 mlevin@jade.tufts.edu writes: > Is there any way, from a system call on BSD4.3, to determine if a >file is a hard link? I know lstat() will describe soft links, but >none of my Unix books seem to say how one can figure out if a file is >a hard link or not. Is this impossible? Please email me at >mlevin@jade.tufts.edu with any responses. Thanks in advance. If it is not a soft link then it is a hard link. In fact, soft links themselves involve a hard link as well. What a hard link really is, is a name in a directory that points to an inode which describes a file. Every file has that. People often refer to a file as being hard linked only after it has TWO OR MORE such names. Keep in mind that EVERY name is equivalent. There is no "primary" name. If you have a file called "abc" and you hard link "xyz" to it: ln abc xyz then you now have TWO names pointing to the same file. Both "abc" and "xyz" are equal. If you remove "abc": rm abc Then "xyz" will still point to the file, and the file will still exist. In fact you can even restore the name "abc" by doing: ln xyz abc So this is why there is no indicator of hard links. EVERY name is in fact a hard link. However you cannot do multiple hard links to the same inode if that inode is a symlink (soft link) or a directory (unless your system is broken as mine is). Check for the link count. That will tell you how many names the file has. For a directory it should always be 2, one for the parent pointing to it and one for it's own "." file. -- /***************************************************************************\ / Phil Howard -- KA9WGN -- phil@ux1.cso.uiuc.edu | Guns don't aim guns at \ \ Lietuva laisva -- Brivu Latviju -- Eesti vabaks | people; CRIMINALS do!! / \***************************************************************************/