zoukankan      html  css  js  c++  java
  • 20155333 实现mypwd

    20155333 实现mypwd

    学习pwd命令

    Linux中用 pwd 命令来查看”当前工作目录“的完整路径。

    1. 命令格式:pwd [选项]
    2. 命令功能:查看”当前工作目录“的完整路径
    3. 用参数:一般情况下不带任何参数,如果目录是链接时,格式:
      pwd -P 显示出实际路径,而非使用连接(link)路径。
      pwd -L:与pwd -P完全相反,显示链接路径

    研究pwd实现需要的系统调用(man -k; grep),写出伪代码

    实现pwd打印当前目录
    • 所需函数 getcwd
    • 函数原型:char *getcwd( char *buffer, int maxlen );
    • 功 能:获取当前工作目录
    • 参数说明:getcwd()会将当前工作目录的绝对路径复制到参数buffer所指的内存空间中,参数maxlen为buffer的空间大小。
    • 返 回 值:成功则返回当前工作目录,失败返回 FALSE。
    • 在某些 Unix 的变种下,如果任何父目录没有设定可读或搜索模式,即使当前目录设定了,getcwd()还是会返回 FALSE。有关模式与权限的更多信息见 chmod()。
    • 头文件:unistd.h(windows下为direct.h)
    #include <unistd.h>
    #include <stdio.h>
    int main(void){
        printf("%s
    ",getcwd(NULL,0));  //输出获取路径
        return 0;
    }
    

    实现mypwd

    mypwd.c
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<dirent.h>
    
    //获得一个目录的inode号
    //以便于配合readdir筛选
    ino_t get_inode(char *);
    
    //利用递归,由根目录向外
    //依次显示各级目录
    void printpathto(ino_t);
    
    //在当前目录中
    //根据inode号,寻找对应的
    //文件或文件夹名称,保存在数组里
    //注意:inode有对应类型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];
    //未到达根目录前都满足这个条件
    if(get_inode("..") != this_inode)
    {
    //换到其上级目录
    chdir("..");
    //把上级目录的文件夹名存到its_name里
    inum_to_name(this_inode,its_name,BUFSIZ);
    //将当前目录(刚才切换的“上级目录”)
    //的内容给my_inode
    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);
    }
    //显示不停读取目录中的内容,if将每条内容加以判断
    //判断inode号是否是我们需要的
    //如果是,
    while((direntp = readdir(dir_ptr)) != NULL)
    if(direntp->d_ino == inode_to_find)
    {
    //将文件夹名复制到namebuf中,并添加''
    strncpy(namebuf, direntp->d_name,buflen);
    namebuf[buflen-1] = '';
    closedir(dir_ptr);
    return;
    }
    //如果程序正常,在上面就return了
    fprintf(stderr,"error looking for inum %dn",inode_to_find);
    exit(1);
    }
    
    //怎么获得的?
    //stat把文件名(目录文件)信息放到结构体
    //返回结构体st_ino元素
    //这里增加了一步出错处理
    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;
    }
    

    测试mypwd

  • 相关阅读:
    POJ 2752 Seek the Name, Seek the Fame
    POJ 2406 Power Strings
    KMP 算法总结
    SGU 275 To xor or not to xor
    hihocoder 1196 高斯消元.二
    hihoCoder 1195 高斯消元.一
    UvaLive 5026 Building Roads
    HDU 2196 computer
    Notions of Flow Networks and Flows
    C/C++代码中的笔误
  • 原文地址:https://www.cnblogs.com/rh123456/p/7868357.html
Copyright © 2011-2022 走看看