• pwd的实现


    #include <string.h>
    #include <stdlib.h>
    #include <dirent.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    
    ino_t get_inode(char *);
    void printpathto(ino_t);
    void inum_to_name(ino_t, char *, int);
    
    int main()
    {
        printpathto(get_inode("."));
        printf("
    ");
        return 0;
    }
    
    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);
        }   
    
    }
    
    void inum_to_name(ino_t inode_to_find, char *namebuf, int buflen)
    {
        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] = '';
                closedir(dir_ptr);
                return;
            }
        fprintf(stderr, "error looking for inum %d
    ", inode_to_find);
        exit(1);
    }
    
    ino_t get_inode(char *fname)
    {
        struct stat info;
        if (stat(fname, &info) == -1)
        {
            fprintf(stderr, "Cannot Stat");
            perror(fname);
            exit(1);
        }
        return info.st_ino;
    }
    



  • 相关阅读:
    html5 标签
    开发中常见问题
    This Android SDK requires Andriod Developer Toolkit version 23.0.0 or above
    onActivityResult 要加判断
    expandlistview child 不可点击
    自定义dialog布局
    mediaPlayer 播放
    转载 收藏 listview判断是否在底部
    mediaPlayer
    org.apache.http.legacy.jar 兼容
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3151271.html
Copyright © 2020-2023  润新知