pwd的实现
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.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;
} void printpathto(ino_t this_inode)
{
ino_t my_inode;
char its_name[BUFSIZ];
if (get_inode("..") != this_inode)
{
chdir("..");
inum_to_name(this_inode, its_name, BUFSIZ);
my_inode = get_inode(".");
printpathto(my_inode);
printf("/%s", its_name);
} } void inum_to_name(ino_t inode_to_find, char *namebuf, int buflen)
{
DIR *dir_ptr;
struct dirent *direntp;
dir_ptr = opendir(".");
if (dir_ptr == NULL)
{
perror(".");
exit(1);
}
while ((direntp = readdir(dir_ptr)) != NULL)
if (direntp->d_ino == inode_to_find)
{
strncpy(namebuf, direntp->d_name, buflen);
namebuf[buflen-1] = '\0';
closedir(dir_ptr);
return;
}
fprintf(stderr, "error looking for inum %d\n", inode_to_find);
exit(1);
} ino_t get_inode(char *fname)
{
struct stat info;
if (stat(fname, &info) == -1)
{
fprintf(stderr, "Cannot Stat");
perror(fname);
exit(1);
}
return info.st_ino;
}
pwd的实现的更多相关文章
- linux常用命令(2)pwd命令
pwd 命令1 命令格式:pwd [选项]2 命令功能查看当前工作目录的完整路径3 常用参数一般不带任何参数如果目录是链接时:pwd -P 显示实际路径,而非使用链接路径4 常用实例:4.1 用pwd ...
- pwd命令
[pwd] 打印当前的工作目录 pwd==print work director 命令格式: pwd [OPTION]... 命令功能: 打印当前工作目录的全路径 命 ...
- Linux命令学习总结:pwd命令
命令简介: 该命令用来显示目前所在的工作目录.指令英文原义:print work directory 执行权限 :All User 指令所在路径:/usr/bin/pwd 或 /bin/pwd ...
- 每天一个linux命令(3):pwd命令
Linux中用 pwd 命令来查看”当前工作目录“的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的确切位置. ...
- 【初级】linux pwd 命令详解及使用方法实战
pwd:查看当前工作目录 前言: Linux中用 pwd 命令来查看”当前工作目录“的完整路径,就是经常提及的所在目录,多用在生产环境多级目录中查看当前所在路径,使用此命令能给运维人员/操作人员带来很 ...
- linux命令(3):pwd命令
Linux中用 pwd 命令来查看”当前工作目录“的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的确切位置. ...
- linux-11 基本命令之 -工作期目录切换命令-pwd,cd,
pwd 命令用于显示当前的工作目录,格式为:pwd[选项] @1.查看当前的工作路径: [root@localhost /]# pwd cd 命令用于切换工作路径 格式为:"cd 目录名称& ...
- Linux2:vi、ls、cd、pwd、mkdir、rm、mv、cp、cat、tail
前言 从本篇文章开始,每篇文章将写10个Linux命令,个人的写作想法是: 1.常用的Linux命令,那些生僻的.不常用的就不写了 2.从实际考虑,只列出每个命令常见的用法和参数选项,有兴趣了解进一步 ...
- pwd, cd, ls, touch, mkdir, rmdir, rm
学习Shell命令最好的资料当然的是$man, 绝对是查找命令的第一大杀器,但是我们有时只是想实现某个功能,甚至连这个命令是什么都不知道,又或者不想淹没在man里大段大段的英文里,大家可以参考Linu ...
- 每天一个Linux命令(3):pwd命令
Linux中用 pwd 命令来查看"当前工作目录"的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判定当前目录在文 ...
随机推荐
- 用C语言怎么实现复制自己
#include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { char str[80]; ...
- crm高速开发之QueryExpression
/* 创建者:菜刀居士的博客 * 创建日期:2014年07月06号 */ namespace Net.CRM.OrganizationService { using System; ...
- BZOJ 3173: [Tjoi2013]最长上升子序列( BST + LIS )
因为是从1~n插入的, 慢插入的对之前的没有影响, 所以我们可以用平衡树维护, 弄出最后的序列然后跑LIS就OK了 O(nlogn) --------------------------------- ...
- Mysql zip 安装(windows)
Mysql Windows zip包安装 Mysql 下载地址: http://dev.mysql.com/downloads/mysql/ 下载windows 版本对应的zip,之后解压 在C:\P ...
- itextSharp 对pdf的每个页面添加footer/header
static void SetAllHeaderFooter(string inputPath) { PdfReader reader=new PdfReader(inputPath); PdfSta ...
- 通过cmd命令安装、卸载、启动和停止Windows Service(InstallUtil.exe)-大壮他哥
步骤: 1.运行--〉cmd:打开cmd命令框 2.在命令行里定位到InstallUtil.exe所在的位置 InstallUtil.exe 默认的安装位置是在C:/Windows/Microsoft ...
- jquery mobile左右滑动切换页面
jquery mobile左右滑动切换页面 $(function() {$("body").bind('swiperight', function() { $.mobile.ch ...
- PHP中遍历stdclass object 及 json 总结[中国航天神舟十号以json形式向地面返回数据]
$test=Array ( [0] => stdClass Object ( [tags] => 最快的车,Bloodhound,SSC [id] => 48326888 11 从网 ...
- Endnote从头开始五:修改output style(转载)
Endnote从头开始五:修改output style Endnote中虽然有大量的期刊格式,但是并不能囊括所有我们需要的style,所以学会自己制作或编辑已有的期刊格式是很重要的,本节内容是Endn ...
- 基于visual Studio2013解决面试题之0503取最大数字字符串
题目