Achieve mypwd - 20175204 Zhang Zhen rushing water

Achieve mypwd

Mission requirements:

1 learning pwd command
2 study pwd achieve the required system calls (man -k; grep), write pseudo code
3 to achieve mypwd
4 test mypwd
submission process blog's link


Task One: Learning pwd command

Task steps:

1.pwd Detailed command:
pwd command function is to display the full path to the current working directory where you can see the absolute path to the current directory by pwd.
2.pwd command parameters:
-L: - Logical, displays the current path, when the connection file, the file path connecting directly to the display, (in this default mode when no parameters).
-p: - physical, displays the current path, when the connection file, without using a connection path is connected directly to the display file points to the file. When a multilayer connection file, display file points to the final connection file.
3. Learn pwd command in Linux:
Use man pwd

the use of man -k pwdlearning

4. Use the pwd command


Task Two: Research pwd achieve the required system calls (man -k; grep), write pseudo code

Task steps:

1.pwd The principle: pwd Print the current working directory to the absolute path, you can find step by step from the current directory to the root directory, and find the root directory, you can get the full path. The system is managed by inode files, each file has inode number (the directory is a special file, there are two special filenames in each directory. (Current directory) and .. (parent directory)). Continue to move forward to find the same directory has no parent directory, two special filename. .. and still present in the root directory, they said in the same inode number to reach the root directory. When the inode number inode number and the initial catalog of constantly looking for the end of the same path.

2.利用man -k directory | grep 3查找与路径相关的库函数,并利用命令学习getcwd,getwd函数。

3.伪代码

1.获得当前文件的inode号
2.不断向前寻找直到从根目录中找到inode相同的值,找到相应的文件名
3.输出路径

任务三:实现mypwd

任务步骤:

1.实现代码

#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);  //不断寻找找到与ionde相同的值,并获得文件名
int getinode(char *);  //获得inode号
int main()  
{  
printpath();  
putchar('\n');  
return ;  
}  

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);  
}
void printpath()  
{  
int inode,up_inode;  
char *str;  
inode = getinode(".");  
up_inode = getinode("..");  
chdir("..");  
str = inode_to_name(inode);  
if(inode == up_inode) {    
    return;  
}  
printpath();  
printf("/%s",str);  
}  

任务四:测试mypwd

任务步骤:

1.输入命令进行编译运行。

Guess you like

Origin www.cnblogs.com/zyzgl/p/12023658.html