电脑技术002pc网认为此文章对《w10电脑系统怎么关机Unix文件系统和pwd命令实现详解》说的很在理,电脑技术002pc网为你提供最佳的电脑基础教程,操作系统。
函数原型int res=rename (const char* from,const char *to);
原理:复制链接到新的名字/位置再删除原来的链接
if(link("x","z")!=-1)
unlink("x");
命令 cd 对进程有影响,对目录本身没有影响
实现 头文件 #include <unistd.h>
函数原型 int res=chdir (const char *path);
14. pwd 命令的实现
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
ino_t get_inode(char *);//get the inode number
void printpathto(ino_t);
void inum_to_name(ino_t,char *,int);//get the node name by its inode number
int main()
{
printpathto(get_inode("."));
putchar('\n');
return 0;
}
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(".");
printpathto(my_inode); //itorater
printf("/%s",its_name);
}
}
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(".");
return;
}
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",(int)inode_to_find);
return;
}
ino_t get_inode(char *fname)
{
struct stat info;
if(stat(fname,&info)==-1)
{
fprintf(stderr,"Can not stat");
perror(fname);
return 1;
}
return info.st_ino;
}
运行结果:
caoli@caoli-laptop:~/workspace/test$ ./pwd1
/home/caoli/workspace/test
caoli@caoli-laptop:~/workspace/test$
更多:w10电脑系统怎么关机Unix文件系统和pwd命令实现详解
https://www.002pc.com/xitongchangshi/51.html
你可能感兴趣的pwd,Unix,详解,文件系统,命令,实现
