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命 ...
随机推荐
- ScheduledThreadPoolExecutor实现原理
ScheduledThreadPoolExecutor实现原理 博客分类: concurrency java 自jdk1.5开始,Java开始提供ScheduledThreadPoolExecut ...
- 通过java递归思想实现以树形方式展现出该目录中的所有子目录和文件
当初在开始接触Java时 学习File部分的一个小练习 挺有意思 一开始是通过看 北京圣思园 张龙老师的视频开始学校java的,必须强烈推荐,真很棒. 功能实现:主要实现以树形方式展现出该目录中的 ...
- 使用cnpm 安装vue.js
前提已经安装了node.js 一.临时使用 1.npm install -g cnpm --registry=https://registry.npm.taobao.org 2.cnpm instal ...
- JS -- serializeJSON
http://www.cnblogs.com/linzenews/p/7065050.html
- Android 7.0 适配
extends:http://www.jianshu.com/p/56b9fb319310http://blog.csdn.net/chay_chan/article/details/57083383
- 跨平台桌面程序框架Electron
https://www.npmjs.com/ js库
- Skip the Class
BestCoder Round #92 Skip the Class Accepts: 678 Submissions: 1285 Time Limit: 2000/1000 MS (Java/ ...
- plsql中文乱码
一.关于PLSQL无法正确显示中文 刚才下载安装了PLSQL Developer 9.0.0.1601 汉化绿色版,执行SQL查询语句,发现显示的数据中只要有中文都会以?表示. 原因:客户端跟服务器的 ...
- IkAnalyzer2012FF_u1.jar免费下载
链接:https://pan.baidu.com/s/1P_0cdRLKJO4VIUTokvTS0g 提取码:qt3w
- vue前后分离---数据模拟
最近为在做CRM的前期工作,忙里偷闲写了个关于数据模拟方面的东西 主要是现在博客中满天都再说前后分离,但是还没有几个实际操作的---让许多新手{-_-} 方法一: 启动一个express静态服务器-- ...