2017-2018-1 20155336 《信息安全系统设计基础》加分作业:实现mypwd

2017-2018-1 20155336 《信息安全系统设计基础》加分作业:实现mypwd

什么是PWD?

  • 用man pwd查看:

1071513-20171206224529597-1604371589.jpg

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

实现mypwd

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
ino_t get_inode(char*);
void pwd(ino_t);
void name(ino_t,char*,int);

int main()
{
    pwd(get_inode("."));  
    printf("\n");
    return 0;
}

void pwd(ino_t this_inode)
{
    ino_t my_inode;
    char its_name[BUFSIZ];
    if (get_inode("..")!=this_inode)                                 
    {
        chdir(".."); 
        name(this_inode,its_name,BUFSIZ);
        my_inode = get_inode(".");
        pwd(my_inode);
        printf("/%s",its_name);
    }
}

void name(ino_t inode,char* namebuf,int buflen)   //找到i-节点对应的文件名
{
    DIR* cdir;
    struct dirent* direntp;
    cdir = opendir(".");
    while((direntp = readdir(cdir)) != NULL)
    {
        if(direntp->d_ino == inode)
        {
            strncpy(namebuf,direntp->d_name,buflen);
            namebuf[buflen-1] = '\0';
            closedir(cdir);
            return;
        }
    }
    printf("error looking for inode\n");
}

ino_t get_inode(char* fname)            //根据文件名,返回-i节点
{
    struct stat info;
    stat( fname, &info);
    return info.st_ino;
}

测试mypwd

1071513-20171206225641363-1479319.jpg

转载于:https://www.cnblogs.com/hxl681207/p/7995357.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值