zoukankan      html  css  js  c++  java
  • 20155222 c语言实现pwd命令

    20155222 c语言实现linux的pwd命令

    • 1.学习pwd命令在Linux层次结构中,用户可以在被授权的任意目录下利用mkdir命令创建新目录,也可以利用cd命令从一个目录转换到另一个目录。然而,没有提示符来告知用户目前处于哪一个目录中。想要知道当前所处的目录,可以用pwd命令,该命令显示整个路径名。

    • 2.研究实现pwd所需的系统调用

    opendir(打开目录)
    相关函数 open,readdir,closedir,rewinddir,seekdir,telldir,scandir
    表头文件

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

    定义函数

    DIR * opendir(const char * name);
    

    函数说明 opendir()用来打开参数name指定的目录,并返回DIR形态的目录流,和open()类似,接下来对目录的读取和搜索都要使用此返回值。
    返回值 成功则返回DIR
    型态的目录流,打开失败则返回NULL。
    错误代码 EACCESS 权限不足
    EMFILE 已达到进程可同时打开的文件数上限。
    ENFILE 已达到系统可同时打开的文件数上限。
    ENOTDIR 参数name非真正的目录
    ENOENT 参数name 指定的目录不存在,或是参数name 为一空字符
    串。
    ENOMEM 核心内存不足。

    readdir(读取目录)
    相关函数 open,opendir,closedir,rewinddir,seekdir,telldir,scandir
    表头文件

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

    定义函数 struct dirent * readdir(DIR * dir);
    函数说明 readdir()返回参数dir目录流的下个目录进入点。
    结构dirent定义如下

    struct dirent
    {
    ino_t d_ino;
    ff_t d_off;
    signed short int d_reclen;
    unsigned char d_type;
    har d_name[256;
    };
    

    d_ino 此目录进入点的inode
    d_off 目录文件开头至此目录进入点的位移
    d_reclen _name的长度,不包含NULL字符
    d_type d_name 所指的文件类型
    d_name 文件名
    返回值 成功则返回下个目录进入点。有错误发生或读取到目录文件尾则返回NULL。
    附加说明 EBADF参数dir为无效的目录流。
     
    范例:

    #include<sys/types.h>
    #include<dirent.h>
    #include<unistd.h>
    main()
    {
    DIR * dir;
    struct dirent * ptr;
    int i;
    dir =opendir(“/etc/rc.d”);
    while((ptr = readdir(dir))!=NULL)
    {
    printf(“d_name: %s
    ”,ptr->d_name);
    }
    closedir(dir);
    }
    执行
    d_name:.
    d_name:..
    d_name:init.d
    d_name:rc0.d
    d_name:rc1.d
    d_name:rc2.d
    d_name:rc3.d
    d_name:rc4.d
    d_name:rc5.d
    d_name:rc6.d
    d_name:rc
    d_name:rc.local
    d_name:rc.sysinit
    

    closedir(关闭目录)
    相关函数 opendir
    表头文件

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

    定义函数 int closedir(DIR *dir);
    函数说明 closedir()关闭参数dir所指的目录流。
    返回值 关闭成功则返回0,失败返回-1,错误原因存于errno 中。
    错误代码 EBADF 参数dir为无效的目录流

    • 3.实现步骤:
      步骤一:查看当前目录"."文件的inode-number
      步骤二:查看当前目录“..”文件的inode-number
      步骤三:比较两个inode-number,若相等,已到根目录,进入步骤四,否则进入“..”,根据步骤一中获取的inode-number,在目录中查找相应的文件名并将其入栈,回到步骤一。
      步骤四:依次出栈输出路径名

    • 4.实现代码

    
    #include<stdio.h>
    #include<stdlib.h>
    #include<sys/types.h>
    #include<dirent.h>
    #include<sys/stat.h>
    #include<fcntl.h>
    #include<string.h>
    int main()
    {
            
           
            char this_name[20]=".",father_name[20]="..";
            char name[16][32];
            struct dirent *this_dir=NULL,*father_dir=NULL,*temp=NULL;
            DIR *dir=NULL;
            int i=0;
            unsigned long int this_ino;
            while(1)
            {
                    dir = opendir(this_name);
                    if(dir == NULL)
                    printf("error open");
                    while(1)
                    {
                            this_dir = readdir(dir);
                            if(strcmp(this_dir->d_name,".")==0)
                            break;
                    }
                    this_ino=this_dir->d_ino;
                    closedir(dir);
                    dir = opendir(father_name);
    
                    while(1)
                    {
                            father_dir = readdir(dir);
                            if(father_dir->d_name[0]=='.'&&father_dir->d_name[1]=='.')
                            break;
                    }
                    if(this_dir->d_ino==father_dir->d_ino)
                    break;
                    closedir(dir);
                    dir=opendir(father_name);
                    while((temp=readdir(dir))!=NULL)
                    {
                            if(temp->d_ino==this_ino)
                            {
                            	bzero(name[i],32);
    			        strcat(name[i++],temp->d_name);
                                    break;
                            }
                    }
                    closedir(dir);
                    strcat(this_name,"/..");
                    strcat(father_name,"/..");
            }
            for(i--;i>=0;i--)
    	printf("/%s",name[i]);
    	printf("
    ");
    }
    
    • 5.测试代码
  • 相关阅读:
    There is no session with id session多人使用一个账号
    记录一次@Autowire和@Resource遇到的坑
    shiro 未认证登录统一处理以及碰到的问题记录
    Realm [*] was unable to find account data for the submitted AuthenticationToken
    springboot项目监听器不起作用
    发送邮件com.sun.mail.util.TraceInputStream.<init>(Ljava/io/InputStream;Lcom/sun/mail
    mysql查询重复数据记录
    使用shiro在网关层解决过滤url
    maven添加jetty插件,同时运行多个实例
    Linux 安装Zookeeper集群
  • 原文地址:https://www.cnblogs.com/20155222lzj/p/7859377.html
Copyright © 2011-2022 走看看