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 ...
随机推荐
- 如何利用Xshell在windows与linux之间互传文件
如何利用Xshell在windows与linux之间互传文件 第一步: 安装Xshell. 第二步: 打开Xshell,若出现默认的对话框,则选择关闭,因为下面将演示如何将本地文件传输至远程linux ...
- Exception.StackTrace
Exception中的StackTrace属性 执行堆栈跟踪在给定时刻正在执行的所有方法. 对方法调用的跟踪称为堆栈跟踪. 堆栈跟踪列表提供了一种循着调用堆叠跟踪到方法中异常发生处行号的手段.Stac ...
- jmeter 网速
有人知道在jmeter 哪个里面哦 JMeterPlugins里面 network
- mysql更改数据文件目录及my.ini位置
步骤: 1.查找my.ini位置,可通过windows服务所对应mysql启动项,查看其对应属性->可执行文件路径,获取my.ini路径. "C:\MySQL\MySQL Server ...
- npm package.json中的dependencies和devDependencies的区别
转载:http://www.cnblogs.com/jes_shaw/p/4497836.html 一个node package有两种依赖,一种是dependencies一种是devDependenc ...
- linux三尖刀
序 我们都知道,一个可执行程序的基本的生命过程是如此的: (编辑)源文件--->(编译)目标文件--->(链接)可执行文件--->(调试排错)稳定执行 所以,在这个过程中,我们很容易 ...
- laravel框架中使用Validator::make()方法报错
在控制器中用到了Validator::make(),它默认是use Dotenv\Validator; 但这样会出现 FatalErrorException错误 call to undefined m ...
- python自动化运维之路2
list列表 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 #!/usr/bin/env python # _*_ encoding:utf-8 _*_ # a ...
- Delphi 简体 繁体 转换
http://delphi.ktop.com.tw/board.php?cid=30&fid=69&tid=104986 試看看 這個是豬寶寶從網路上抄來的 檢視純文字版列印? fun ...
- 《转》深入理解Activity启动流程(一)–Activity启动的概要流程
本文原创作者:Cloud Chou. 原文地址:http://www.cloudchou.com/android/post-788.html Android中启动某个Activity,将先启动Acti ...