2

I am about to write a command that shows the current directory in linux. I know I can use the "pwd" command, but that's what I need to implement myself!... in other words, when entering the so called "findme" command, I want to return back the directory that I am in at the moment. I have managed to create the my "findme" command (which is very simple,I know), but how am I supposed to know where in I am after executing the command, in order to show the whole directory?

lilalinux
  • 2,903
  • 3
  • 43
  • 53

2 Answers2

2

It seems a rather odd requirement: 'implement pwd' (this isn't homework, is it?). Can you give a little more context?

Probably relevant bits of information are:

  • the directory .. is linked to the parent directory, and . to the current one; so...
  • changing directory to .. goes up one level in the filesystem (unless you're at the top); plus
  • every directory will have an 'inode number', so if you consider a directory foo, then both it and the directory foo/. will have the same inode number.

I don't know how pwd actually does it, but I'd lay money you could reimplement it with this information.

Norman Gray
  • 11,978
  • 2
  • 33
  • 56
  • Indeed it's possible, but it's way easier to just expose a new syscall. – Ismael Luceno Sep 22 '12 at 09:17
  • This is how FreeBSD's `getcwd()` works if the system call to query the pathname fails. The system call may fail if the pathname is too long, one of the directory names fell out off the name cache or it has been disabled for debugging purposes. – jilles Sep 22 '12 at 12:31
  • well actually, it is a homework,though is it a crime to get some help for homework when u reach a dead end? this is how it should look like: when u r not in a certain directory it should only show "/" character, and if u create a temp directory, "/temp" should be shown. what i think i should do is using the "cd" command and copying each buf element>=4 of its array to a new array so that when the "pwd" command is called the new array gets printed. am i right? – Selina Saibot Sep 24 '12 at 09:50
  • No, homework-related questions are no crime, but as the linked page describes, there are certain guidelines about asking and answering them. The bits of information I mentioned describe how to walk up the filesystem, how to know when to stop, and how to know where you are at each step. Can you see how to use that to get your solution? – Norman Gray Sep 25 '12 at 14:41
0

XV6 isn't at all the same as GNU/Linux. pwd was not implemented in UNIX V6, that's why it's a good exercise. You would probably want to implement getcwd() as a syscall.

Ismael Luceno
  • 2,055
  • 15
  • 26