views:

48

answers:

2

Hi,

I am writing my own shell program. I am currently implementing the cd command using chdir. I want to implement the cd with the below options : -P Do not follow symbolic links -L Follow symbolic links (default)

My query is that , when a given path is entered on the shell how to figure out if the path is a symbolic link or an absolute path progamatically?

Thanks

+2  A: 

Check out the lstat() function , you need to use S_ISLNK on the st_mode field.

Unknown
That should be `S_IFLNK`
R Samuel Klatchko
I think you mean `lstat()`, `stat()` will follow the symlink
Hasturkun
You are right, corrected.
Unknown
+1  A: 
if [ -L /path/to/file ]; then
  echo "is a symlink!"
else
  echo "not a symlink! maybe a directory or regular file, or does not exist"
end
zed_0xff