linux编程实践:实现pwd命令

  • 内核为每个目录都设置了一个指向自己的i节点入口,即".",还有一个指向其父目录i节点的入口,即”..",我们首先获取当前目录的i节点编号,但是并不能知道当前目录的名称,我们切换到其的父目录,在里面寻找当前i节点编号对应的文件名即可。这样我们就很容易联想到使用递归来实现,但是终止条件是什么呢?在Unix文件系统的根目录中,“."和“..”指向同一个i节点,我们可以以此判断是否发到达了根目录。

  • pwd以绝对路径打印当前的工作目录。因为整个系统的文件组织是树形的,所以,可以从当前目录逐层向根目录进行查找,当找到根目录,即可得到完全路径。

  • 1.如何逐层查找?
    系统通过inode节点来管理文件,所以每个文件会有一个inode号。目录是比较特殊的文件,通过<inode, name>的列表组织目录下的文件。每个目录下有两个特殊的文件名".", "..",分别表示当前目录和父目录。

  • 如何打破文件系统的限制,形成完整的绝对路径?
    查看系统所有的挂载路径,将所有挂载路径和上面求得的路径拼接起来,如果是合法路径且inode号与最初目录的inode号相同,即是想要的最终的路径。

  • Linux中用 pwd 命令来查看”当前工作目录“的完整路径。 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录。
    在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的确切位置。


1.命令格式:

pwd [选项]  

2.命令功能:

查看”当前工作目录“的完整路径  

3.常用参数:

一般情况下不带任何参数
如果目录是链接时:
格式:pwd -P 显示出实际路径,而非使用连接(link)路径。

4.常用实例:

实例1:用 pwd 命令查看默认工作目录的完整路径

命令:

pwd  

输出:

[root@localhost ~]# pwd    
/root    
[root@localhost ~]#    

实例2:使用 pwd 命令查看指定文件夹

命令:

pwd

输出:

[root@localhost ~]# cd /opt/soft/  
[root@localhost soft]# pwd    
/opt/soft    
[root@localhost soft]#  

实例三:目录连接链接时,pwd -P 显示出实际路径,而非使用连接(link)路径;pwd显示的是连接路径

命令:

pwd -P

输出:

[root@localhost soft]# cd /etc/init.d 
[root@localhost init.d]# pwd
/etc/init.d
[root@localhost init.d]# pwd -P
/etc/rc.d/init.d
[root@localhost init.d]#

实例4:/bin/pwd

命令:

/bin/pwd [选项]

选项:
-L 目录连接链接时,输出连接路径
-P 输出物理路径
输出:

[root@localhost init.d]# /bin/pwd 
/etc/rc.d/init.d
[root@localhost init.d]# /bin/pwd --help
[root@localhost init.d]# /bin/pwd -P
/etc/rc.d/init.d
[root@localhost init.d]# /bin/pwd -L
/etc/init.d
[root@localhost init.d]#

总代码完善如下:

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<dirent.h>
#include<stdlib.h>
#include<string.h>

#define SIZE 128

ino_t get_inode(char *dirname);
void get_work_dir(ino_t inode_num);
void inode_to_dirname(ino_t inode_num, char *buf, int buflen);

int main(void)
{
    get_work_dir(get_inode("."));   
    printf("\n");
    return 0;

}

ino_t get_inode(char *dirname)
{
    struct stat info;
    if (stat(dirname, &info) == -1)
    {
        perror("dirname");
        exit(1);
    }

    return info.st_ino;
}

void get_work_dir(ino_t inode_num)
{
    ino_t parent_inode;
    char buf[SIZE];
    if (get_inode("..") != inode_num)
    {
        chdir("..");
        inode_to_dirname(inode_num, buf, SIZE);
        parent_inode = get_inode(".");
        get_work_dir(parent_inode);
        printf("/%s", buf);
    }
}

void inode_to_dirname(ino_t inode_num, char *buf,int buflen)
{
    DIR *dir_ptr;
    struct dirent *dire;
    if ((dir_ptr = opendir(".")) == NULL)
    {
        perror(".");
        exit(1);
    }

    while ((dire = readdir(dir_ptr)) != NULL)
    {
        if (dire->d_ino == inode_num)
        {
            strncpy(buf, dire->d_name, buflen);
            buf[strlen(buf)] = '\0';
            closedir(dir_ptr);
            return ;
        }
    }
    fprintf(stderr, "error looking for inode number %d\n", (int)inode_num);
    exit(1);
}
posted @ 2017-11-19 18:40  Heineken  阅读(621)  评论(0编辑  收藏  举报