[cs631apue] ls(1) symbolic link issue

Ramana Nagasamudram rnagasam at stevens.edu
Sat Oct 13 22:27:56 EDT 2018


Hi,

I've tried using readlink(2) and realpath(3) to find the name of
the target of a symbolic link.  readlink(2) fails when the target
doesn't exist.  realpath(3) works perfectly but it is hard to figure
out whether a non-existent target must refer to a file or directory.

For example (directory names changed),

	$ ls -l test/file1 test/file2 test/file3
	lrwxr-xr-x  1 rmn  users  8 Oct 13 05:57 file1 -> /nowhere
	lrwxr-xr-x  1 rmn  users  7 Oct 13 05:58 file2 -> nowhere
	lrwxr-xr-x  1 rmn  users  9 Oct 13 05:59 file3 -> /nowhere/

	$ ./ls -l test/file1 test/file2 test/file3
	lrwxr-xr-x  1 rmn  users  8 Oct 13 05:57 file1 -> /nowhere
	lrwxr-xr-x  1 rmn  users  7 Oct 13 05:58 file2 -> /.../nowhere
	lrwxr-xr-x  1 rmn  users  9 Oct 13 05:59 file3 -> /nowhere

Here file2 is a symlink to a file called `nowhere' in the current
directory, file1 is a symlink to `/nowhere', a non-existent file
and file3 is a symlink to `/nowhere/', a non-existent directory.
Using just realpath(3), I am unable to figure out whether the
non-existent target is meant to be a file or directory.

Has anyone faced this issue?  I'm sure there are better ways of
doing this than using realpath(3) and any pointers or reference
functions would be great.


Thanks,
Ramana


More information about the cs631apue mailing list