20155228 2017-11-19 实现mypwd(选做,加分)
20155228 2017-11-19 实现mypwd(选做,加分)
题目和要求
- 学习pwd命令
- 研究pwd实现需要的系统调用(man -k; grep),写出伪代码
- 实现mypwd
- 测试mypwd
分析和设计
PWD命令
pwd [ -L | -P ]
描述
pwd 命令将当前目录的全路径名称(从根目录)写入标准输出。全部目录使用 /(斜线)分隔。第一个 / 表示根目录,最后一个目录是当前目录。
参数
- -L:如果 PWD 环境变量包含了不包含文件名 .(点)或 ..(点点)的当前目录的绝对路径名,则显示 PWD 环境变量的值。否则,-L 标志与 -P 标志一样运行。
- -P:显示当前目录的绝对路径名。与 -P 标志一起显示的绝对路径不包含在路径名的绝对路径中涉及到符号链接类型的文件的名称。
- 退出状态:该命令返回以下出口值:0 成功完成;>0 发生错误。
使用man和grep研究pwd涉及的系统调用
man -k pwd:没有找到任何系统调用

man -k current directory:以pwd的描述作为关键字进行查找

man -k current directory | grep 2:筛选系统调用命令,发现一个叫做
getcwd的系统调用,从描述来看,完全符合需求

系统调用命令:Getcwd
头文件和参数
#include <unistd.h>
char *getcwd(char *buf, size_t size);
描述
These functions return a null-terminated string containing an absolute pathname that is thecurrent working directory of the calling process. The pathname is returned as the functionresult and via the argument buf, if present.If the current directory is not below the root directory of the current process (e.g., becausethe process set a new filesystem root using chroot(2) without changing its current directoryinto the new root), then, since Linux 2.6.36, the returned path will be prefixed with thestring "(unreachable)". Such behavior can also be caused by an unprivileged user by changingthe current directory into another mount namespace. When dealing with paths from untrustedsources, callers of these functions should consider checking whether the returned path startswith '/' or '(' to avoid misinterpreting an unreachable path as a relative path.
这些函数返回一个以null结尾的字符串,其中包含绝对路径名调用进程的当前工作目录。 路径名作为函数返回结果和通过参数buf,如果存在。如果当前目录不在当前进程的根目录下(例如,因为该进程使用chroot(2)设置新的文件系统根目录,而不更改其当前目录进入新的根目录),那么,从Linux 2.6.36开始,返回的路径将以前缀字符串“(无法访问)”。 这种行为也可能是由非特权用户通过改变而引起的将当前目录转换为另一个挂载名称空间。 处理来自不受信任的路径来源,这些函数的调用者应该考虑检查返回的路径是否开始用'/'或'('来避免将不可达路径误解为相对路径。
代码和测试
- 代码编写
#include "unistd.h"
#include "stdio.h"
int main(void)
{
printf("%s\n",getcwd(NULL,0));
return 0;
}
#include<stdio.h>
#include<sys/stat.h>
#include<dirent.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
void printpath();
char *inode_to_name(int);
int getinode(char *);
int main()
{
printpath();
putchar('\n');
return ;
}
void printpath()
{
int inode,up_inode;
char *str;
inode = getinode(".");
up_inode = getinode("..");
chdir("..");
str = inode_to_name(inode);
if(inode == up_inode) {
// printf("/%s",str);
return;
}
printpath();
printf("/%s",str);
}
int getinode(char *str)
{
struct stat st;
if(stat(str,&st) == -1){
perror(str);
exit(-1);
}
return st.st_ino;
}
char *inode_to_name(int inode)
{
char *str;
DIR *dirp;
struct dirent *dirt;
if((dirp = opendir(".")) == NULL){
perror(".");
exit(-1);
}
while((dirt = readdir(dirp)) != NULL)
{
if(dirt->d_ino == inode){
str = (char *)malloc(strlen(dirt->d_name)*sizeof(char));
strcpy(str,dirt->d_name);
return str;
}
}
perror(".");
exit(-1);
}
- 测试截图

参考资料
pwd (Unix命令)
Linux下用C语言实现pwd命令显示当前工作目录(再次修正)
20155228 2017-11-19 实现mypwd(选做,加分)的更多相关文章
- 20155239 2017-11-19 实现mypwd(选做,加分)
20155239 2017-11-19 实现mypwd(选做,加分) 题目和要求 学习pwd命令 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 实现mypwd 测试mypwd ...
- 实现mypwd(选做)
实现mypwd(选做) 任务清单 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd 4 测试mypwd (一)pwd命令的学习 1.pw ...
- 团队作业4——第一次项目冲刺(Alpha版本)2017.11.19
第三次会议:2017-11-16 第二次会议讨论的还没有完全实现,于是在第三次会议上对此进行了一些对我们工作上的看法,得出结论:多花时间啊!!!! 又没照照片图: 会议主要内容: 1.登录注册完善 2 ...
- 2017.11.19 C语言基础及流水灯实现
/* 从右往左*/ #include <reg52.h> sbit ADDR0 = P1^0; sbit ADDR1 = P1^1; sbit ADDR2 = P1^2; sbit ADD ...
- 第一次项目冲刺(Alpha版本)2017/11/19
一.当天站立式会议 会议内容 1.对数据库的设计的进一步讨论 2.讨论SSH一些配置细节 3.分配今天的任务 二.任务分解图 三.燃尽图 四.心得 1.零散的时间要利用起来,追上计划的进度. 2.在小 ...
- 2017.11.9 如何利用JS做登陆验证界面
()案例----JavaScript实现输入验证 需要验证的表单输入域和要求 用户名不能为空,是否符合规定的格式 密码长度是否超过6,两次密码输入一致 邮箱地址:邮箱地址必须符合邮箱形式 ~~~注意提 ...
- 第31次Scrum会议(11/19)【欢迎来怼】
一.小组信息 队名:欢迎来怼 小组成员 队长:田继平 成员:李圆圆,葛美义,王伟东,姜珊,邵朔,阚博文 小组照片 二.开会信息 时间:2017/11/19 17:05~17:34,总计29min. 地 ...
- 课下选做作业实现mypwd
2019-2020-1 20175227 <信息安全系统设计基础> 课下选做作业实现mypwd 要求 学习pwd命令 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 ...
- (选做)实现mypwd
选做 实现mypwd 实验内容: 1.学习pwd命令. 2.研究pwd实现需要的系统调用(man -k; grep),写出伪代码. 3.实现mypwd. 4.测试mypwd. 实验步骤: 学习pwd命 ...
随机推荐
- Linux系统解析域名的先后顺序【转帖】
Linux系统解析域名的先后顺序 gd_WWW已经在本地(/etc/hosts)进行指向,但是竟然还能解析到外网,让我百思不得其解.经过不断查找发现域名解析与以下四个文件有关: /etc/hosts ...
- TIJ -- CountDownLatch
1. 2. Class : CountDownLatchDemo package lime.thinkingInJava._021._007._001; import java.sql.Time; i ...
- android控件RecyclerView中,如何显示自定义分割线以及最后一项去除分割线
在控件RecyclerView中,分割线DividerItemDecoration类的使用经常见,如果是使用自带的分割线,只需要这样写即可 RecyclerView mRecyclerView; mR ...
- Qt编写自定义控件4-旋转仪表盘
前言 旋转仪表盘,一般用在需要触摸调节设置值的场景中,其实Qt本身就提供了QDial控件具有类似的功能,本控件最大的难点不在于绘制刻度和指针等,而在于自动计算当前用户按下处的坐标转换为当前值,这个功能 ...
- XML5个转义符:<,>,&,”,©;的转义字符分别如下: < >& " '
XML5个转义符:<,>,&,”,©;的转义字符分别如下: < >& " ' $search = array ...
- div左右居中css
l_btn{ font-size: 1.2rem; width: 190px; height: 50px; border: 1px solid #fff; border-radius: 25px; c ...
- 【微信小程序——开发步骤1】
知识点: view,image,text编写文本框架 使用弹性盒子动态布局 使用rpx调试分辨率 在wxml中查看默认样式属性 步骤: 1.以如图页面实例说明如何写出微信文本内容 先对页面写出整体 ...
- java学习之路--StringBuffer常见的功能和实例
---恢复内容开始--- 储存 StringBuffer append();将指定数据作为参数添加到已有数据尾处 StringBuffer insert(index,数据):可以将数据插到指定的ind ...
- hadoop伪分布环境快速搭建
1.首先下载一个完成已经进行简单配置好的镜像文件(hadoop,HBASE,eclipse,jdk环境已经搭建好,tomcat为7.0版本,建议更改为tomcat8.5版本,运行比较稳定). 2安装V ...
- tcpdf开发文档(中文翻译版)
2017年5月3日15:06:15 这个是英文翻译版,我看过作者的文档其实不太友善或者不方便阅读,不如wiki方便 后面补充一些,结构性文档翻译 这是一部官方网站文档,剩余大部分都是开发的时候和网络总 ...