ls -i vs. stat()

Mike { Whats all this then? } Sullivan mike_s at ebay.sun.com
Fri Sep 21 10:01:49 AEST 1990


In <1990Aug23.225411.10266 at rice.edu> jsulliva at killington.prime.com (Jeff Sullivan) writes:

> Running a simple test:
>	$ stattest stat.c
>        i-number = 2953       stat.c
>	$ ln -s stat.c stat.link
>	$ stattest stat.*
>	i-number = 2953       stat.c
>	i-number = 2953       stat.link
>	$ ln -i stat.*
	  ^^ Of course, this is 'ls'
>	  2953 stat.c      38379 stat.link

>Is this the correct output for the 'ls -i' command?  

	Yes

>I would expect all links to have the same i-number as the original.

They do, for a hard link, but a symbolic link is actually another file.
So there are two files here, which ls is showing you, but your program is
not.

The problem is that stat() follows symbolic links, while ls does not. When
you stat("stat.link"), it actually follows the link and gives you the
information about stat.c.

If you want to be like ls, use lstat(). This doesn't follow symbolic
links, it instead gives back information about the link itself, and is
what ls uses.

Mike Sullivan                     Internet: msullivan at Ebay.Sun.COM
Sun Education                     UUCP:     ..!sun!yavin!msullivan
Software Course Developer         Compuserve: 75365,764



More information about the Comp.sys.sun mailing list