2018-2019-1 20165226《信息安全系统设计基础》 pwd命令的实现
2018-2019-1 20165226《信息安全系统设计基础》 pwd命令的实现
一、学习pwd
查看pwd

得知一个嫩过去文件路径的函数——
getcwd

i节点值
通过
ls -i -a查看.、..目录对应的值
- stat结构体
struct stat {
mode_t st_mode; //文件对应的模式,文件,目录等
ino_t st_ino; //inode节点号
dev_t st_dev; //设备号码
dev_t st_rdev; //特殊设备号码
nlink_t st_nlink; //文件的连接数
uid_t st_uid; //文件所有者
gid_t st_gid; //文件所有者对应的组
off_t st_size; //普通文件,对应的文件字节数
time_t st_atime; //文件最后被访问的时间
time_t st_mtime; //文件内容最后被修改的时间
time_t st_ctime; //文件状态改变时间
blksize_t st_blksize; //文件内容对应的块大小
blkcnt_t st_blocks; //文件内容对应的块数量
};
由此可知通过
ino_t返回i-Node值
二、编写代码
- 思路1
(1)得到"."的i节点号,称其为n(使用stat)
(2)chdir ..(使用chdir)
(3)找到inode号为n的节点,得到其文件名。重复上述操作直到当前目录“.”的inode值等于".."的inode值
- 代码1
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
ino_t get_inode(char *);
void printpathto(ino_t);
void inum_to_name(ino_t ,char *,int);
int main()
{
printpathto(get_inode("."));
printf("\n");
return 0;
}
ino_t get_inode(char * filename)
{
struct stat buffer;
if(lstat(filename,&buffer) == -1)
{
perror("can't stat");
exit(1);
}
return buffer.st_ino;
}
void printpathto(ino_t ino)
{
ino_t ino_parent = get_inode("..");
if(ino_parent == ino)
return;
else
{
struct stat s;
char buffer[255];
chdir("..");
inum_to_name(ino,buffer,255);
printpathto(ino_parent);
printf("/%s",buffer);
}
}
void inum_to_name(ino_t ino,char * buffer,int buffer_length)
{
DIR * dir;
struct dirent * direntp;
struct stat stat_buffer;
dir = opendir(".");
if(dir == NULL)
{
perror("can't open dir .");
exit(1);
}
while((direntp = readdir(dir)) != NULL)
{
lstat(direntp->d_name,&stat_buffer);
if(stat_buffer.st_ino == ino)
{
strncpy(buffer,direntp->d_name,buffer_length);
buffer[buffer_length-1] = '\0';
closedir(dir);
return;
}
}
}
- 代码2(使用getcwd)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
char *filepath=NULL;
filepath=getcwd(NULL,0);
puts(filepath);
free(filepath);
return 0;
}
- 测试结果

2018-2019-1 20165226《信息安全系统设计基础》 pwd命令的实现的更多相关文章
- 2017-2018-1 20155201 《信息安全系统设计基础》 pwd命令的实现
2017-2018-1 20155201 <信息安全系统设计基础> pwd命令的实现 一.对pwd命令的学习 在终端中输入man pwd查看手册中对pwd这一命令的解释: 以绝对路径的方式 ...
- 2017-2018-3 20155337《信息安全系统设计基础》 pwd指令学习
2017-2018-3 20155337<信息安全系统设计基础> pwd指令学习 任务要求 学习pwd指令 研究pwd实现需要的系统调用(man -k:grep),写出伪代码 实现mypw ...
- 2018-2019-1 20165212 《信息安全系统设计基础》第八周学习总结(pwd)
2018-2019-1 20165212 <信息安全系统设计基础>第八周学习总结 一.知识点总结 1.三种并发方式 构造并发程序的方法有三种: 进程 线程 I/O多路复用 进程:用内核来调 ...
- 20135328信息安全系统设计基础第一周学习总结(Linux应用)
学习计时:共xxx小时 读书: 代码: 作业: 博客: 一.学习目标 1. 能够独立安装Linux操作系统 2. 能够熟练使用Linux系统的基本命令 3. 熟练使用Linux中用户管理命令/ ...
- 20155326 2017-2018-1 《信息安全系统设计基础》课下加分项mypwd实现
20155326 2017-2018-1 <信息安全系统设计基础>课下加分项mypwd实现 pwd命令能做什么 在虚拟机中输入pwd查看其返回的是什么 通过上图得知pwd命令用来显示目录. ...
- 2017-2018-1 20155210 《信息安全系统设计基础》 实现mypwd
2017-2018-1 20155210 <信息安全系统设计基础> 实现mypwd 作业要求: 1.学习pwd命令 2.研究pwd实现需要的系统调用(man -k; grep),写出伪代码 ...
- 2017-2018-1 20155214 《信息安全系统设计基础》 第9周课下测试-mypwd
2017-2018-1 20155214 <信息安全系统设计基础> 第9周课下测试-mypwd(深入版) 题目要求: 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; ...
- 2017-2018-1 20155216 《信息安全系统设计基础》 实现mypwd
2017-2018-1 20155216 <信息安全系统设计基础> 实现mypwd 作业要求: 1.学习pwd命令 2.研究pwd实现需要的系统调用(man -k; grep),写出伪代码 ...
- 2017-2018-1 20155226 《信息安全系统设计基础》课下实践——实现mypwd
2017-2018-1 20155226 <信息安全系统设计基础>课下实践--实现mypwd 1 学习pwd命令 输入pwd命令 发现他是给出当前文件夹的绝对路径. 于是 man 1 pw ...
随机推荐
- HttpContext.Current and Web Api
Using HttpContext.Current in WebApi is dangerous because of async HttpContext.Current gets the curre ...
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] E. Weakness and Poorness 三分
E. Weakness and Poorness time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- python 清空列表
# lst = ["篮球","排球","乒乓球","足球","电子竞技","台球" ...
- js 获取地址栏域名以及URL
console.log(window.location.host) console.log(document.domain) console.log(window.location.href) con ...
- HDU1565 方格取数 &&uva 11270 轮廓线DP
方格取数(1) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- Kubernetes实践--hello world 示例
本文所说的Hello world是一个web留言板应用,并且是基于PHP+Redis的两层分布式架构的web应用,前端PHP web网站通过访问后端Redis数据库完成用户留言的查询和添加功能,具备读 ...
- ExtJS 6 如何引入Dashboard模版
最近很多人问我在ext js 6+的版本中怎么引入官方的dashboard模版,正好我好久没写博客了,这里我写一篇博客来说明一下. 在这里以ext js 6.2.1版本为例(注:需要安装Sencha ...
- 在Firefox中发现一个在Linux下查看chm文档的插件
在Firefox浏览器插件中搜索插件chmfox插件,安装后就可以在linux下通过Firefox浏览器阅读chm文档了.
- 重写vector类,完成基本功能,不含迭代器
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- java的代理(编程思想)
代理 第三种关系被称为代理,java并没有提供对它的直接支持.这是继承和组合之间的中庸之道,因为我们将一个对象置于所要构造的类中(就像组合),但与此同时我们在新类中暴露了该成员对象的所有方法(就像继承 ...