20155211实现mypwd

关于pwd

在Linux层次结构中,用户可以在被授权的任意目录下利用mkdir命令创建新目录,也可以利用cd命令从一个目录转换到另一个目录。然而,没有提示符来告知用户目前处于哪一个目录中。想要知道当前所处的目录,可以用pwd命令,该命令显示整个路径名。

  • -L

    如果 PWD 环境变量包含了不包含文件名 .(点)或 ..(点点)的当前目录的绝对路径名,则显示 PWD 环境变量的值。否则,-L 标志与 -P 标志一样运行。
  • -P

    显示当前目录的绝对路径名。与 -P 标志一起显示的绝对路径不包含在路径名的绝对路径中涉及到符号链接类型的文件的名称。

    退出状态
  • 该命令返回以下出口值:

    0 成功完成。 >0 发生错误。

pwd实现

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
ino_t get_inode(char *);
void printpathto(ino_t);
void inum_to_name(ino_t, char *, int); int main() {
printpathto(get_inode(".")); /* print path to here */
putchar('\n'); /* then add newline */
return 0;
} /*
* prints path leading down to an object with this inode
* kind of recursive
*/
void printpathto(ino_t this_inode) {
ino_t my_inode;
char its_name[BUFSIZ];
if (get_inode("..") != this_inode) {
chdir(".."); /* up one dir */
inum_to_name(this_inode, its_name, BUFSIZ);/* get its name*/
my_inode = get_inode("."); /* print head */
printpathto(my_inode); /* recursively */
printf("/%s", its_name); /* now print name of this */
}
}
void inum_to_name(ino_t inode_to_find, char *namebuf, int buflen) {
DIR *dir_ptr; /* the directory */
struct dirent *direntp; /* each entry */
dir_ptr = opendir(".");
if (dir_ptr == NULL) {
perror(".");
exit(1);
}/* search directory for a file with specified inum */
while ((direntp = readdir(dir_ptr)) != NULL)
if (direntp->d_ino == inode_to_find) {
strncpy(namebuf, direntp->d_name, buflen);
namebuf[buflen - 1] = '\0'; /* just in case */
closedir(dir_ptr);
return;
}
fprintf(stderr, "error looking for inum %d\n", inode_to_find);
exit(1); } /*
* returns inode number of the file
*/
ino_t get_inode(char *fname) {
struct stat info; if (stat(fname, &info) == -1) {
fprintf(stderr, "Cannot stat ");
perror(fname);
return 1;
}
return info.st_ino;
}

最新文章

  1. Jmeter实现登录bugfree、新建bug、解决bug脚本(抓包工具实现)
  2. ASP.NET MVC 过滤器(四)
  3. 在eclipse之中使用Junit
  4. JAVA魔法堂:折腾Mybatis操作SQLite的SQLException:NYI异常
  5. Winform开发框架主界面设计展示
  6. JS案例之6——瀑布流布局(1)
  7. python解无忧公主数学题107.py
  8. asp 下拉框二级联动
  9. BZOJ3530: [Sdoi2014]数数
  10. 如何让虚拟目录里面的webconfig不继承网站的设置
  11. C#基础知识01(continue、break 和 return、ref 和 out)
  12. BZOJ 1708: [Usaco2007 Oct]Money奶牛的硬币
  13. ssm整合说明与模板-Spring Spring MVC Mybatis整合开发
  14. 前端页面中如何在窗口缩放时让两个div始终在同一行显示
  15. WebSocket和Socket
  16. CDH 报错:under replicated blocks
  17. MySQL 8.0常见问题
  18. Python学习之旅(八)
  19. entity framework 缓存干扰的数据不一致问题
  20. HTML中元素的position属性详解

热门文章

  1. pyenv - python版本管理
  2. 利用describe( )中的count来检查数据是否缺省
  3. 对volatile不具有原子性的理解
  4. MySQL提权之user.MYD中hash破解方法
  5. Java虚拟机13:Java类加载机制
  6. Django settings.py 的media路径设置
  7. Django中模型(三)
  8. chrome下载离线安装包的方法
  9. 搞懂JVM类加载机制
  10. oracle 查看表空间以及剩余量