C linux pwd command to achieve

C linux pwd command to achieve

Pwd command function introduced

linux pwd command to display the working directory
execute pwd command immediately know where the absolute path name of the current working directory.
Example :

Inquiry System Manual

  • As shown, the getcwddescription is "get current working directory", which we want to achieve pwd command function is very similar, so I researchgetcwd

  • As shown, for usage and description getcwd mentioned in its function is to return a null character string to the end of the location where the calling program contains the absolute path , which is obviously what we need to implement functionality!

Pwd command to achieve (a)

So far, the first generation mypwd has been formed, and used in the program as long as the getcwd()function can be achieved function to get the absolute path.

The following is the realization of C language code mypwd

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    char buf[1024];

    char *cwd =getcwd(buf, sizeof(buf));

    if (NULL == cwd) {
        perror("Get cerrent working directory fail.\n");
        exit(-1);
    } else {
        printf("%s\n", cwd);
    }

    return 0;
}

Test Results

Achieve pwd command (b)

The first generation mypwd pwd command is already achieved functionality, but this is clearly only fulfill its function, and can not understand how it works, too bullying, so I continued reference to the linux directory of information

Linux way of organizing files in a directory

In a file system, an inode represents a file, and an integer value used to represent the inode, called inode-Number , the value is unique to a file system, which can be found via the corresponding inode value . Under normal circumstances, a file has only one inode information to describe it .

This semester, we have been emphasizing a concept, "system under linux, everything is a file" , so there is no doubt that the file is a directory, it must be organized by the inode.

因此,我们通常所说的目录a“包含”文件b,其实现层面上的意思是,目录a的内容列表里有一个关于文件b的列表项,即“b的inode-number+b的filename”。综上,Linux中,一个文件(包括目录)的文件名,及文件名与inode的对应关系,都是由包含该文件的目录所描述的。

伪代码

现在我们了解了可以实现工作目录与上级目录inode值的比对来判断是否追溯到根目录,为了实现这个上溯目录的功能,我们可以使用如图所示这个函数:

chdir("..")即可实现向上级目录跳转

定义用来存储路径的字符数组
通过特殊文件名“.”获得当前工作目录名称
chdir()返回上级目录
if(本目录inode-number和上级inode-number不同),本目录不是根目录,继续向上查找
else
是根目录,可以输出绝对路径了

详细代码

有了上述的思路,我们可以写出详细代码:

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

ino_t get_inode(char*);
void printpathto(ino_t);
void inum_to_name(ino_t,char*,int);
int main()
{
    printpathto(get_inode("."));  //打印当前目录绝对路径
    putchar('\n');
    return 0;
}

void printpathto(ino_t this_inode)
{
    ino_t my_inode;
    char its_name[BUFSIZ];
    /*如果本目录的inode-number与上级目录不同,即本目录不是根目录*/
    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);
    }
}
void inum_to_name(ino_t inode_to_find,char* namebuf,int buflen)   //找到inode-number节点对应的文件名,并放在字符数组里
{
    DIR* dir_ptr;
    struct dirent* direntp;
    dir_ptr = opendir(".");
    if (dir_ptr == NULL)
    {
        perror(".");
        exit(1);
    }
 
    while((direntp = readdir(dir_ptr)) != NULL)
    {
        if(direntp->d_ino == inode_to_find)
        {
            strncpy(namebuf,direntp->d_name,buflen);
            namebuf[buflen-1] = '\0';
            closedir( dir_ptr);
            return;
        }
    }
    fprintf( stderr , "error looking for inum % d\n" ,inode_to_find);
    exit (1) ;
}
ino_t get_inode(char* fname)            //根据文件名,返回inode-number
{
    struct stat info;
    if ( stat( fname, &info) == -1){
        fprintf( stderr , "Cannot stat ");
        perror(fname);
        exit (1);
    }
    return info.st_ino;
}

测试结果

参考资料

《Unix环境高级编程》
博客园连接

Guess you like

Origin www.cnblogs.com/darklord0604/p/12010196.html