drkcore

2009/10/04 09:06:36

Unix/Linuxプログラミング理論と実践 4章 (pwdを書く)

pwdコマンドはinodeを親ディレクトリに辿っていく。ルートディレクトリは. と..が同じinodeを指す。

macbookでls -ia。

$ ls -ia /
      2 ./                                               31452 Users/
      2 ../                                              22730 Volumes/

ので.と..のinodeをチェックしながら再帰的に辿るという実装。

void printpathto( ino_t this_inode )
{
  ino_t my_inode;
  char  its_name[BUFSIZ];

  if ( get_inode( ".." ) != this_inode )
    {
      chdir( ".." );

      inum_to_name(this_inode, its_name, BUFSIZ);

      my_inode = get_inode( "." );
      printpathto( my_inode );
      printf("/%s",its_name );
    }
}

ProductName Unix/Linuxプログラミング理論と実践
Bruce Molay
アスキー・メディアワークス / ¥ 6,090 ()
在庫あり。

ダブル間接ブロックとトリプル間接ブロック

Comments