1

I am writing my own shell program. I am currently implementing the cd command using chdir. I want to implement the chdir with the below options :

-P Do not follow symbolic links
-L Follow symbolic links (default)

I posted a question here previously asking to know if a path is a symbolic link or actual path. But with that info I am unable to get any ideas on how to proceed with the above problem.

Thanks

Community
  • 1
  • 1
hits_lucky
  • 327
  • 2
  • 9
  • 18
  • 2
    Do you have a good use case for this functionality? I don't think any existing shell does this nor do I think any users are requesting that, but perhaps they are. Just seems like arcane unix complexity without much end user benefit. When an end user cares whether a directory is or is not a symlink, the symlink "feature" is failing. End users shouldn't care. While I can see more valid use cases for recursive file traversal utilities like tar and scp having options around not following symlinks, for an interactive shell doing a cd, it's not obvious what the value is. Just curious. – Peter Lyons Jun 30 '10 at 03:49
  • Bash provides `cd -P` and `cd -L` (and defaults to `cd -L`). When you've done a `cd -P`, `cd ..` takes you up the physical file system hierarchy even if you followed a symlink to get you there, but if you've done a `cd -L` to get you there, then `cd ..` chops one element off the name of the symlink to take you 'up' a level. – Jonathan Leffler Feb 11 '14 at 05:51

2 Answers2

0

Maybe I'm misunderstanding, but you just want (pseudocode):

is_symlink = method_from_other_question();
if(is_symlink and arg(-P))
    fail("Can't switch directory -- is a symlink");

If you've already tried something like this and it doesn't work, include the code in your question and we can help debug it

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
0

Shells generally do a lot of trickery to find the real dir name. And they also fake a lot of stuff for our user's convenience. E.g.:

$ pwd
/home/auser
$ ls -l
adir -> some/place/
some/
$ cd adir
$ pwd
/home/auser/adir
$ cd ..
$ pwd
/home/auser

Looks logical? Then look again: the .. in /home/auser/some/place points to /home/auser/some, not /home/auser yet cd .. took you to the later.

IOW, there is no other way but to always keep in memory the absolute path of the current directory and parse it fully (and check every element of it) when doing cd.

And yes, it is not reliable. In past on one occasion I have managed to fool bash and it was showing totally wrong absolute path for my current directory.

Dummy00001
  • 16,630
  • 5
  • 41
  • 63