zoukankan      html  css  js  c++  java
  • 20155308 加分项——C语言实现Linux的pwd命令

    20155308 加分项——C语言实现Linux的pwd命令

    实现要求

    学习pwd命令

    什么是pwd

    • pwd‘ 代表的是‘Print Working Directory’(打印当前目录)。如它的名字那样,‘pwd’会打印出当前工作目录,或简单的来说就是当前用户所位于的目录。它会打印出以根目录 (/)为起点的完整目录名(绝对目录)。

    基本语法

    • pwd [OPTION]

    详细用法

    • 利用man pwd查看pwd的用法

    常用实例

    参考
    http://www.cnblogs.com/peida/archive/2012/10/24/2737730.html
    学习了pwd命令

    • 实例1:用pwd命令查看默认工作目录的完整路径

    • 实例2:使用 pwd 命令查看指定文件夹

    • 实例3:/bin/pwd

    命令:/bin/pwd [选项]

    选项:

    -L 目录连接链接时,输出连接路径

    -P 输出物理路径

    输出:

    [root@localhost init.d]# /bin/pwd 
    
    /etc/rc.d/init.d
    
    [root@localhost init.d]# /bin/pwd --help
    
    [root@localhost init.d]# /bin/pwd -P
    
    /etc/rc.d/init.d
    
    [root@localhost init.d]# /bin/pwd -L
    
    /etc/init.d
    

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

    首先输入命令

    man -k directory | grep 2
    

    查看

    发现getcwd()函数符合条件后,查看其用法

    • 伪代码为:
    定义一个字符串数组储存绝对路径
    调用函数getcwd()
    if (返回的指针==NULL)
        调用函数出错,发出错误报告
    else
        打印结果
    

    代码实现

    #include<stdio.h>  
    #include<sys/stat.h>  
    #include<dirent.h>  
    #include<stdlib.h>  
    #include<string.h>  
    #include<sys/types.h>  
    void printpath();  
    char *inode_to_name(int);  
    int getinode(char *);  
    int main()  
    {  
        printpath();  
        putchar('
    ');  
        return ;  
    }  
    void printpath()  
    {  
        int inode,up_inode;  
        char *str;  
        inode = getinode(".");  
        up_inode = getinode("..");  
        chdir("..");  
        str = inode_to_name(inode);  
        if(inode == up_inode) {  
        //  printf("/%s",str);  
            return;  
        }  
        printpath();  
        printf("/%s",str);  
    }  
    int getinode(char *str)  
    {  
        struct stat st;  
        if(stat(str,&st) == -1){  
            perror(str);  
            exit(-1);  
        }  
        return st.st_ino;  
    }  
    char *inode_to_name(int inode)  
    {  
        char *str;  
        DIR *dirp;  
        struct dirent *dirt;  
        if((dirp = opendir(".")) == NULL){  
            perror(".");  
            exit(-1);  
        }  
        while((dirt = readdir(dirp)) != NULL)  
        {  
            if(dirt->d_ino == inode){  
                str = (char *)malloc(strlen(dirt->d_name)*sizeof(char));  
                strcpy(str,dirt->d_name);  
                return str;  
            }  
        }  
        perror(".");  
        exit(-1);  
    
    

    代码测试

    实验感想

    上课老师讲过了pwd的用法,下课我们用过这个学习,让我更好地明白了pwd的各种功能以及代码实现过程。

  • 相关阅读:
    无锁队列以及ABA问题
    bigworld源码分析(3)——dbMgr分析
    bigworld源码分析(4)——BaseAppMgr分析
    bigworld源码分析(5)——BaseApp分析
    bigworld源码分析(2)—— loginApp分析
    bigworld源码分析(1)—— 研究bigworld的意义和目标
    C++嵌入Python,以及两者混用
    B-Tree算法分析与实现
    通过sqlserver日志恢复误删除的数据
    win7启动时怎么自动进入桌面
  • 原文地址:https://www.cnblogs.com/JIUSHA/p/7860303.html
Copyright © 2011-2022 走看看