C语言实现pwd—关于linux文件系统

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
/*
    DIR *opendir(const char *pathname),即打开文件目录,返回的就是指向DIR结构体的指针
    struct __dirstream   
    {   
        void *__fd;    
        char *__data;    实施
        int __entry_data;    
        char *__ptr;    
        int __entry_ptr;    
        size_t __allocation;    
        size_t __size;    
        __libc_lock_define (, __lock)    
    };   
  
typedef struct __dirstream DIR;  
    int stat(const char *file_name, struct stat *buf);
    的作用就是获取文件名为d_name的文件的详细信息,存储在stat结构体中。以下为stat结构体的定义:
    struct stat {   
        mode_t     st_mode;       //文件访问权限   
        ino_t      st_ino;       //索引节点号   
        dev_t      st_dev;        //文件使用的设备号   
        dev_t      st_rdev;       //设备文件的设备号   
        nlink_t    st_nlink;      //文件的硬连接数   
        uid_t      st_uid;        //所有者用户识别号   
        gid_t      st_gid;        //组识别号   
        off_t      st_size;       //以字节为单位的文件容量   
        time_t     st_atime;      //最后一次访问该文件的时间   
        time_t     st_mtime;      //最后一次修改该文件的时间   
        time_t     st_ctime;      //最后一次改变该文件状态的时间   
        blksize_t st_blksize;    //包含该文件的磁盘块的大小   
        blkcnt_t   st_blocks;     //该文件所占的磁盘块   
      };  
*/
/*根据文件名获取文件inode-number*/
ino_t get_ino_byname(char *filename)
{
    struct stat file_stat;
    if (0 != stat(filename, &file_stat)) {
        perror("stat");
        exit(-1);
    }
    return file_stat.st_ino;
}

/*根据inode-number ,在当前目录中查找对应的文件名*/
char* find_name_byino(ino_t ino)
{
    DIR *dp = NULL;
    struct dirent *dptr = NULL;
    char *filename = NULL;
    if (NULL == (dp = opendir("."))) {
        fprintf(stderr, "Can not open Current Directory\n");
        exit(-1);
    } else {
        while (NULL != (dptr = readdir(dp))) {
            if (dptr->d_ino == ino) {
                filename = strdup(dptr->d_name);
                break;
            }
        }
        closedir(dp);
    }
    return filename;
}

/*限制最大的目录深度*/
#define MAX_DIR_DEPTH (256)

int main(int argc, char *argv[])
{
    /*记录目录名的栈*/
    char *dir_stack[MAX_DIR_DEPTH];
    unsigned current_depth = 0;

    for(;;) {
        /*1.通过特殊的文件名“.”获取当前目录的inode-number*/
        ino_t current_ino = get_ino_byname(".");
        /*2.通过特殊的文件名“..”获取当前目录的父级目录的inode-number*/
        ino_t parent_ino = get_ino_byname("..");

        /*3.判断当前目录和上级目录的inode-number是否一样*/
        if (current_ino == parent_ino)
            break; /*4.如果两个inode-number一样说明到达根目录*/

        /*5.如果两个inode-number不一样*/
        /*切换至父级目录,根据步骤1获取的inode-number,在父级目录中搜索对应的文件名并记录下来, 重新回到步骤1*/
        chdir("..");
        dir_stack[current_depth++] = find_name_byino(current_ino);
        if (current_depth>=MAX_DIR_DEPTH) { /*路径名太深*/
             fprintf(stderr, "Directory tree is too deep.\n");
             exit(-1);
        }
    }

    /*输出完整路径名*/
    int i = current_depth-1;
    for (i = current_depth-1; i>=0; i--) {
       fprintf(stdout, "/%s", dir_stack[i]);
    }
    fprintf(stdout, "%s\n", current_depth==0?"/":"");

    return 0;
}

在linux 中的文件系统中,文件=N(N>=1)个inode +M(M>=1)个数据块。

数据块,存放文件的内容数据,数据块的数目根据文件内容的大小而定。

inode称为信息节点,其作用有二:1、存储跟文件相关的属性信息,如修改时间、所有者、文件类型和文件长度,注意这些信息里并没有文件名;2、存储指向文件内容数据块的指针信息。

在一个文件系统中,一个inode代表一个文件,并使用一个整数值来标志该inode,称为inode-number,该值对于一个文件系统而言是唯一的,即通过该值可以找到其对应的inode。一般情况下,一个文件只有一个inode信息用来描述它。

转载于:https://www.cnblogs.com/xiaohengheng/p/6654843.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值