[comp.sys.sun] ls -i vs. stat

jsulliva@killington.prime.com (Jeff Sullivan) (08/16/90)

I have noticed that 'ls -i' is giving different i-numbers for linked
files, while stat() says that they are the same.  This was noticed on
SunOS 4.0.3.

 The man page for 'ls' says:
     -i   For each file, print the i-number in the  first  column
	  of the report.

 The simple program I that called the stat() system call did this:
            stat(filename,&sbuf);
            printf("\ti-number = %-10d %s\n",sbuf.st_ino,filename);

 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.*
	  2953 stat.c      38379 stat.link

Is this the correct output for the 'ls -i' command?  I would expect all
links to have the same i-number as the original.

  Any help appreciated,

Jeff Sullivan | Computervision/Prime | jsulliva@cvbnet.prime.com
CADDS R&D     | Bedford, MA    01730 | sun!cvbnet!jsulliva

mike_s@ebay.sun.com (Mike { Whats all this then? } Sullivan) (09/21/90)

In <1990Aug23.225411.10266@rice.edu> jsulliva@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@Ebay.Sun.COM
Sun Education                     UUCP:     ..!sun!yavin!msullivan
Software Course Developer         Compuserve: 75365,764

merlyn@iwarp.intel.com (Randal Schwartz) (09/21/90)

In article <1990Aug23.225411.10266@rice.edu>, jsulliva@killington (Jeff Sullivan) writes:
| I have noticed that 'ls -i' is giving different i-numbers for linked
| files, while stat() says that they are the same.  This was noticed on
| SunOS 4.0.3.
[program deleted]
| 	$ stattest stat.*
| 	i-number = 2953       stat.c
| 	i-number = 2953       stat.link
| 	$ ln -i stat.*
| 	  2953 stat.c      38379 stat.link
| 
| Is this the correct output for the 'ls -i' command?  I would expect all
| links to have the same i-number as the original.

My guess is that stat.link is a symlink pointing at stat.c.  (I can't tell
from the info you give.)  The stat(2) syscall gives you information on the
pointed-at file, while "ls -i" is giving you the real inode information.
If you want different numbers in your C program, use lstat(2) instead of
stat(2).  If you want the same number from "ln -i", use real links, not
symlinks.

Just another Unix hacker,

/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/