写一个linux系统,linux系统编程:自己动手写一个pwd命令

pwd命令:打印当前的工作目录

我们都知道每个目录下面都有两个特殊的目录( . 和 .. ), .: 当前目录, ..: 上层目录,  每个目录都有一个i节点与之相关联

ghostwu@ubuntu:~$ ls -i3677860 bak 3670018 examples.desktop 1507python3678042 core 3670033 Music 1506shell_script1505 c_program 3672560 note 3670169software3672551 data 3675147 php 3678095tags3670028 Desktop 150874 php_study 3670030Templates3670032 Documents 3670034 Pictures 3677997unix3670029 Downloads 3670031 Public 3670035 Videos

通过ls -i就可以显示每个文件和目录的inode值,比如下面我用ls -ia显示所有文件的inode

1,当工作在basic目录下面的时候,   当前目录basic( 也就是. )他的inode值为1573909,   ..: 1507

2,当把路径切换到python时候, .: 1507 刚好就跟basic的 .. 相等。后面依次类推

通过inode的关联就把目录的层级关系给找出来了,下一个问题:如何知道,已经到达根目录?

ghostwu@ubuntu:~/python/basic$ ls -ia1573909 . 8913 person2.class.py 8405test1.py1507 .. 3427 person3.class.py 8897test2.py8910 func2.py 8916 person4.class.py 4537test3.py8911 func3.py 8912 person.class.py 8908test4.py8909 func.py 8915 superlist.class.py

ghostwu@ubuntu:~/python/basic$ cd ..

ghostwu@ubuntu:~/python$ ls -ia1507 . 3670017 .. 1573911 advance 1573909 basic 151172django

ghostwu@ubuntu:~/python$ cd ..

ghostwu@ubuntu:~$ ls -ia3670017 . 3672499.mysql_history2 .. 3677054.navicat643695 .adobe 3672560note1050432 .atom 3675147php...

在根目录(/)下面的. 和 ..,他们的inode节点有个特点, 都是相等的,所以只要判断当前目录的inode等于上层目录的inode,就可以断定,到达根目录了

ghostwu@ubuntu:/$ ls -1ia2.2..915713bin2boot130818cdrom3dev523265etc2home

...

1,第一个要解决的问题: 如果通过路径/文件名,得到对应的inode值,通过stat函数,获得文件/目录的struct stat结构体,文件信息都在这里保存,包括inode

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 /*================================================================2 * Copyright (C) 2018 . All rights reserved.3 *4 * 文件名称:pwd.c5 * 创 建 者:ghostwu(吴华)6 * 创建日期:2018年01月10日7 * 描 述:pwd命令编写8 *9 ================================================================*/

10

11 #include

12 #include

13 #include

14 #include

15 #include

16

17

18 //读取当前文件的i节点

19 ino_t get_inode( char*name );20

21 int main(int argc, char *argv[])22 {23 printf( "当前目录.的inode=%ld", get_inode( ".") );24 printf( "上层目录..的inode=%ld", get_inode( "..") );25 return 0;26 }27

28

29 ino_t get_inode( char*name ) {30 structstat statinfo;31 if( -1 == stat( name, &statinfo ) ) {32 printf( "文件%s打开失败", name );33 exit( -1);34 }35 returnstatinfo.st_ino;36 }

View Code

2,完整的pwd源码

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 /*================================================================2 * Copyright (C) 2018 . All rights reserved.3 *4 * 文件名称:pwd.c5 * 创 建 者:ghostwu(吴华)6 * 创建日期:2018年01月10日7 * 描 述:pwd命令编写8 *9 ================================================================*/

10

11 #include

12 #include

13 #include

14 #include

15 #include

16 #include

17 #include

18

19 #ifndef BUFSIZE20 #define BUFSIZE 100

21 #endif

22

23

24 //读取当前文件的i节点

25 ino_t get_inode( char*name );26 voidprintpathto( ino_t cur_node );27 //根据当前inode节点,找到它对应的路径名称

28 void inode_to_name( ino_t cur_node, char* str, intbufsize );29

30 int main(int argc, char *argv[])31 {32 //printf( "当前目录.的inode=%ld

", get_inode( "." ) );33 //printf( "上层目录..的inode=%ld

", get_inode( ".." ) );

34 printpathto( get_inode( ".") );35 putchar( '');36 return 0;37 }38

39 voidprintpathto( ino_t cur_node ) {40

41 chardir_name[BUFSIZE];42 ino_t my_node;43 //如果当前节点不等于..,说明没有到达根目录

44 if( cur_node != get_inode( "..") ) {45 //切换到上层目录, 当前目录(.)的名称在上层目录(..)46 //所以找名称之前,先要切换到上层目录

47 chdir( "..");48 inode_to_name( cur_node, dir_name, BUFSIZE );49 //chdir( ".." );//不能放在这里,放在这里 找不到目录的名称

50 my_node = get_inode( ".");51 printpathto( my_node );52 printf( "/%s", dir_name );53 }54 }55

56 void inode_to_name( ino_t cur_node, char* str, intbufsize ) {57 DIR*dir_entry;58 struct dirent*pCurDir;59 if( ( dir_entry = opendir( "." ) ) ==NULL ) {60 printf( "open cur directory error");61 exit( -1);62 }63 //printf( "cur inode=%ld

", cur_node );

64 while( ( pCurDir = readdir( dir_entry ) ) !=NULL ) {65 if( cur_node == pCurDir->d_ino ) {66 //printf( "%s

", pCurDir->d_name );

67 strncpy( str, pCurDir->d_name, bufsize );68 str[bufsize-1] = '

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值