自己写shell命令pwd
思维:(1)得到"."的i节点号,叫n(使用stat)
(2)chdir ..(使用chdir)
(3)找到inode号为n的节点,得到其文件名称。
反复上述操作直到当前文件夹“.”的inode值等于".."的inode值
#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;
}
}
}
自己写shell命令pwd的更多相关文章
- 自己动手写shell命令之write
Linux下write命令同意用户跟其它终端上的用户对话.用c语言实现shell命令write.代码例如以下: #include <stdio.h> #include <fcntl. ...
- 数据仓库001 - 复习Linux shell命令 - pwd mkdir mv tail -f xxx.log 和 ail -F xxx.log
1. [root@localhost ~]# 的含义 ? [登录的用户 机器的名称 家目录] 2. 查看当前光标所在的目录 pwd [root@localhost ~]# pwd /roo ...
- 自己动手写shell命令之ls -R1fF
ls命令的R參数代表递归的列出全部子目录中的全部文件,1表示每一行仅仅显示一个文件或目录,f表示不排序即输出.F表示在每项的输出的最后依据其文件类型对应的加上*/=>@|字符.通过c语言实现ls ...
- 自己动手写shell命令之ls
linux下ls命令(支持-R參数)的c语言实现: #include <stdio.h> #include <sys/types.h> #include <dirent. ...
- 自己动手写shell命令之more
unix下more命令的简单实现: #include <stdio.h> #define PAGELEN 24 #define LINELEN 512 int do_more(FILE * ...
- 写了一个Mac快速设置、打开和关闭Web代理的Shell命令
缘由(痛点) 每次在Mac上设置Web代理,都需要点开"系统偏好设置 -- 网络 -- 高级 -- 代理",然后分别设置Web代理(HTTP)和安全Web代理(HTTPS),设置完 ...
- Shell 命令行,写一个自动整理 ~/Downloads/ 文件夹下文件的脚本
Shell 命令行,写一个自动整理 ~/Downloads/ 文件夹下文件的脚本 在 mac 或者 linux 系统中,我们的浏览器或者其他下载软件下载的文件全部都下载再 ~/Downloads/ 文 ...
- Linux 下 Shell 命令的分类及用法
当你打算真正操纵好你的 Linux 系统,没有什么能比命令行界面更让你做到这一点.为了成为一个 Linux 高手,你必须能够理解 Shell命令的不同类型,并且会在终端下正确的使用它们. 在 Linu ...
- bash shell命令(1)
本文地址:http://www.cnblogs.com/archimedes/p/bash-shell1.html,转载请注明源地址. ls命令 ls用来列出目录的内容,它是用户最常用的命令之一,ls ...
随机推荐
- Android:通知栏的使用
非常久没有使用Android的通知功能了,今天把两年前的代码搬出来一看.发现非常多方法都废弃了,代码中各种删除线看的十分不爽.于是乎,打开Google,查看官方文档.学习最新的发送通知栏消息的方法. ...
- Go成功的项目
nsq:bitly开源的消息队列系统,性能非常高,目前他们每天处理数十亿条的消息docker:基于lxc的一个虚拟打包工具,能够实现PAAS平台的组建.packer:用来生成不同平台的镜像文件,例如V ...
- SQL中EXISTS的用法(转)
比如在Northwind数据库中有一个查询为SELECT c.CustomerId,CompanyName FROM Customers cWHERE EXISTS(SELECT OrderID FR ...
- 初步掌握Yarn的架构及原理(转)
1.YARN 是什么? 从业界使用分布式系统的变化趋势和 hadoop 框架的长远发展来看,MapReduce的 JobTracker/TaskTracker 机制需要大规模的调整来修复它在可扩展性, ...
- timeout connect 10000 # default 10 second time out if a backend is not found
timeout connect <timeout> timeout contimeout <timeout> (deprecated) Set the maximum time ...
- RF+Selenium2Library+Sikuli集成环境搭建
Sikuli是通过截图来编写代码的脚本语言,他是对于Selenium不好处理的一些模态窗口.flash等的利器.废话少说,直接开始安装吧.安装RF+Selenium2Library的环境这里就不说了, ...
- sql plus 抢救数据(測)
有索引 --运行plsql脚本 DECLARE TYPE RowIDTab ISTABLE OF ROWID INDEXBY BINARY_INTEGER; CURSORc1 IS selec ...
- [置顶] Bug 11775332 - cluvfy fails with PRVF-5636 with DNS response timeout error [ID 11775332.8]
Bug 11775332 cluvfy fails with PRVF-5636 withDNS response timeout error but error text is not clear ...
- 新发现2个网站:wedelphi和2pascal studydelphi
http://wedelphi.com/f/6/ http://www.2pascal.com/forum.php http://www.delphigroups.info/ http://www.d ...
- fragment Trying to instantiate a class com.example.testhuanxindemo.MyFragment that is not a Fragmen
在使用fragment的时候,先创建了一个fragment,然后为他创建布局,并在oncreateview中返回载入该视图的后返回的view,在activity的布局文件里,使用xml布局,用frag ...