zoukankan      html  css  js  c++  java
  • 2017-2018-1 20155215 第九周 加分项 PWD命令的实现

    1 学习pwd命令

    • Linux中用 pwd 命令来查看”当前工作目录“的完整路径。 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录。
      在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的确切位置。
    1. 命令格式:
      pwd [选项]

    2. 命令功能:
      查看”当前工作目录“的完整路径

    3.常用参数:

    • 一般情况下不带任何参数
    • 如果目录是链接时:
      • 格式:pwd -P 显示出实际路径,而非使用连接(link)路径。
    man pwd
    


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

    man -k pwd 
    

    man -k pwd|grep 1
    


    3 实现mypwd

    • 代码如下所示:
    #include<stdio.h>  
    #include<sys/stat.h>  
    #include<dirent.h>  
    #include<stdlib.h>  
    #include<string.h>  
    #include<sys/types.h> 
    #define BUFFER 10
    #define BUFFERSIZE 50
    int process(char str[][BUFFERSIZE],int h) ;
    int get_name(int inode,char str[][BUFFERSIZE],int h) ;
    int get_inode(char inode[]);
    int main()  
    {  
        char path[BUFFER][BUFFERSIZE];
        int h=0;
        h = process(path,h);  
      //  printf("h = %d",h);
        int i2;
        for(i2=h-2;i2>=0;i2--)
        printf("/%s",path[i2]);
        printf("
    ");
        return 0;  
    }  
    int process(char path[][BUFFERSIZE],int h)  
    {  
        int inode,father_inode;  
        inode = get_inode(".");  
        father_inode = get_inode("..");  
        chdir("..");  
        h = get_name(inode,path,h);  
        if(inode == father_inode) {  
            return h;  
        }  
        h = process(path,h);  
        return h;
    }   
    int get_inode(char inode[])
    {
        struct stat buf;
        stat(inode, &buf);
        return buf.st_ino;
    }
    int get_name(int inode,char path[][BUFFERSIZE],int h)  
    {   
        DIR *dir;  
        struct dirent *dir2;  
        if((dir = opendir(".")) == NULL){  
            perror(".");  
            exit(-1);  
        }   
        while((dir2 = readdir(dir)) != NULL)  
        {  
            if(dir2->d_ino == inode)
               { 
                strcpy(path[h],dir2->d_name); 
                break;
                } 
                  
        }  
        h = h+1;
        return h; 
    } 
    

    4 测试mypwd

  • 相关阅读:
    Shell脚本编程(三):shell参数传递
    Java代码里利用Fiddler抓包调试设置
    Shell脚本编程(二):shell变量
    Shell脚本编程(一):初识shell script
    JAVA使用SCANNER接收中文并输出时出现乱码
    RandomAccessFile类理解
    Vue(九):样式绑定v-bind示例
    Dockerfiles ENV和ARG的应用
    dockerfile中设置python虚拟环境+gunicorn启动
    Docker容器 暴露多个端口
  • 原文地址:https://www.cnblogs.com/xuanyan/p/7862686.html
Copyright © 2011-2022 走看看