tags:

views:

51

answers:

3

Hi,

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

A: 

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
A: 

Currently I have written a function change_dir() which uses chdir() to change a directory and I want to extend this function to include cd commands additional functionality of following symbolic link or actual paths when its specified with -P and -L option.

I would like to know how this can be achieved using chdir ? For example :

/home/my_shell is a symbolic link
lrwxrwxrwx 1 root root 31 2010-06-13 15:35 my_shell -> /home/hita/shell_new/hita_shell

WITH -P OPTION:
hita@hita-laptop:/home$ pwd /home
hita@hita-laptop:/home$ cd -P my_shell
hita@hita-laptop:~/shell_new/hita_shell$ pwd
/home/hita/shell_new/hita_shell (CHANGED DIR)

WITH -L OPTION
hita@hita-laptop:/home$ pwd
/home
hita@hita-laptop:/home$ cd -L my_shell
hita@hita-laptop:/home/my_shell$ pwd
/home/my_shell (CHANGED DIR) (If /home/my_shell is passed as input to chdir() it is resolving the contents of symbolic link)

Is there any other function which will resolve the symbolic path as it is and not the contents of the symbolic link?

Thanks

hits_lucky
A: 

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