2017-2018-1 20155338 加分项目——PWD的实现
2017-2018-1 20155338 加分项目——PWD的实现
项目要求
1 学习pwd命令
2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码
3 实现mypwd
4 测试mypwd
实现过程
首先通过man 命令了解了一下pwd的用法

试试pwd命令的用法:

代码实现:
需要用到readdir函数
可以用man 命令了解了一下readdir函数的用法。
代码如下:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include<string.h>
ino_t get_inode(char* file);
void get_inode_name(ino_t i_node,char *file_name,int length);
void print_direct(ino_t i_node);
void main()
{
ino_t i_node;
print_direct(get_inode("."));
printf("\n");
}
void print_direct(ino_t i_node)
{
ino_t n_inode;
char *file_name[256];
if(get_inode("..")!=get_inode(".")){
chdir("..");
get_inode_name(i_node,file_name,256);
n_inode=get_inode(".");
print_direct(n_inode);
printf("/%s",file_name);
}
}
void get_inode_name(ino_t i_node,char *file_name,int length)
{
DIR* dir_ptr;
struct dirent* direntp;
dir_ptr = opendir(".");
while((direntp = readdir(dir_ptr)) != NULL)
{
if(direntp->d_ino==i_node)
{
strncpy(file_name,direntp->d_name,length);
file_name[length-1]='\0';
closedir(dir_ptr);
}
}
}
ino_t get_inode(char* file)
{
struct stat buf;
if(stat(file,&buf)!=-1)
{
return buf.st_ino;
}
else{
printf("failed to get inode");
}
}
运行结果为:

2017-2018-1 20155338 加分项目——PWD的实现的更多相关文章
- 2017-2018-1 20155320加分项目——pwd的实现
2017-2018-1 20155320加分项目--pwd的实现 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd 4 测试mypwd ...
- 2017-2018-1 20155330 《信息安全系统设计基础》加分项目--实现mypwd
2017-2018-1 20155330 <信息安全系统设计基础>加分项目--实现mypwd pwd命令 命令功能:查看"当前工作目录"的完整路径. 通过man命令查看 ...
- 2017 年度码云新增项目排行榜 TOP 50,为它们打“call”
2017 年度码云新增项目排行榜 TOP 50 正式出炉 !2017 结束了,我们来关注一下这一年里码云上新增的最热门的开源项目吧.此榜单根据 2017 年在码云上新增开源项目的 Watch.Star ...
- 2017 码云最火爆开源项目 TOP 50,你都用过哪些
本文转自:https://share.html5.qq.com/fx/u?r=JdjvzwC 2017 年度码云热门项目排行榜 TOP 50 出炉啦!我们根据所有开源项目在码云的用户关注度.活跃度.访 ...
- MyEclips 2017/2018 (mac 版)安装与破解
MyEclips 2017/2018 (mac 版)安装与破解 现在在学J2EE,然后使用的工具就是 MyEclipse,现在就抛弃 Eclipse 了,我就不多说它俩的区别了,但是 MyEclips ...
- MyEclipse 2017/2018 安装与破解 图文教程
SSM 框架-02-MyEclipse 2017/2018 安装与破解 现在在学J2EE,然后使用的工具就是 MyEclipse,现在就抛弃 Eclipse 了,我就不多说它俩的区别了,但是 MyEc ...
- </2017><2018>
>>> Blog 随笔原始文档及源代码 -> github: https://github.com/StackLike/Python_Note >>> 统计信 ...
- 我的2017&2018
最近项目进入验收阶段,所以上班没那么忙碌了,但是怎么说呢,我可能天生是闲不住的主,觉得浑身不自在(我这样的人是不是特别不会享福),此处应该有个笑脸哈. 翻看了博客园好几个大牛写的技术文章,感慨大牛不愧 ...
- 【分享】2017 开源中国新增开源项目排行榜 TOP 100
2017 年开源中国社区新增开源项目排行榜 TOP 100 新鲜出炉! 这份榜单根据 2017 年开源中国社区新收录的开源项目的关注度和活跃度整理而来,这份最受关注的 100 款开源项目榜单在一定程度 ...
随机推荐
- 编写带有点击特效的UIButton
编写带有点击特效的UIButton 效果: 源码: // // ViewController.m // Button // // Created by XianMingYou on 15/1/18. ...
- 设计标签选择器TitleSwitch
设计标签选择器TitleSwitch 效果如下: 源码如下: TitleSwitch.h 与 TitleSwitch.m // // TitleSwitch.h // TitleSwitch // / ...
- Linux at命令详解
at 只能执行一次,在一个指定的时间执行一个指定任务,只能执行一次,且需要开启atd进程 anacron: 适合于非 7*24 类型的服务器,以天为周期或者在系统开机后执行任务的工作 它会定时检测服务 ...
- Squid安装配置和使用
文:铁乐与猫 环境 centos 6.5 x64 安装 最简单的一种就是yum安装. yum install squid 版本 rpm -qa | grep squid squid-3.1.23-16 ...
- file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known
请求页面时候报错 file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not know ...
- 索引&切片 切割split
索引 s[n] # 中括号里n为一个数字 切片 s[0:9] ...
- C++ Circle
作业链接 https://github.com/How-Come/object-oriented/tree/master/Circle
- 查询包含指定字段的所有表名的SQL脚本
select [name] from sysobjects where [id] in (select [id] from syscolumns where [name]='ReceiptNbr') ...
- 记一次爬虫经历(友话APP的Web端)
背景:学校为迎接新生举办了一个活动,在友话APP的校园圈子内发布动态即可参与活动,最终抽取数名同学赠送福利. 分析:动态的数量会随着迎新的开始逐渐增加,人工统计显然不现实,因此可以使用爬虫脚本在友话A ...
- [转]SVN服务器搭建和使用(二)
上一篇介绍了VisualSVN Server和TortoiseSVN的下载,安装,汉化.这篇介绍一下如何使用VisualSVN Server建立版本库,以及TortoiseSVN的使用. 首先打开Vi ...