开发者

implementing cd command using chdir() in linux

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 Fol开发者_开发百科low 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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜