日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pwd 命令编写

發布時間:2024/4/17 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pwd 命令编写 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

該程序模擬系統的 pwd命令,當然功能也沒有它的強大

程序主要是使用 chdir(const char* path)函數,該函數使當前的目錄跳轉到指定的目錄中

程序使用遞歸的方式一步一步的返回目錄的路徑

相關函數介紹 成功返回0,失敗返回-1

mkdir(char *pathname,mode_t mode)

rmdir(const char* path)

unlink(const char *path)

link(const char *old,const char *new)

rename(const char* from,const char *to) 重命名函數實際是先建立與新文件的連接,然后刪除以前的連接來實現文件重命名的,使用link,unlink函數

?

/**
*?pwd.c
*/
#include
<stdio.h>
#include
<unistd.h>
#include
<sys/types.h>
#include
<sys/stat.h>
#include
<dirent.h>
void?print_cur_path();
void?get_path_name(int?ino,char?*path_name);
int?get_ino(char?*dir_name);
int?main(int?ac,char?*av[])
{
print_cur_path(get_ino(
"."));
printf(
"\n");
return?0;
}
void?print_cur_path(int?ino)
{
char?path_name[BUFSIZ];
????????
if(get_ino("..")!=ino)
????????{
????????????????chdir(
"..");
????????????????get_path_name(ino,path_name);
????????????????print_cur_path(get_ino(
"."));
????????????????printf(
"/%s",path_name);
????????}
}
void?get_path_name(int?ino,char?*path_name)
{
????????DIR?
*dir;
????????
struct?dirent?*dir_name=NULL;

????????
if((dir=opendir("."))==NULL)
????????{
????????????????perror(
"can'n?open?current?directory.\n");
????????????????exit(
1);
????????}
????????
while((dir_name=readdir(dir))!=NULL)
????????{
????????????????
if(dir_name->d_ino==ino)
????????????????{
????????????????strncpy(path_name,dir_name
->d_name,BUFSIZ);
????????????????path_name[BUFSIZ
-1]='\0';
????????????????closedir(dir);
????????????????
return;
????????????????}
????????}
????????perror(
"read?directory?name?error.\n");
????????error(
1);
????????closedir(dir);

}
int?get_ino(char?*dir_name)
{
struct?stat?st_file;
if(stat(dir_name,&st_file)==-1)
????????{
????????perror(dir_name);
????????exit(
1);
????????}
return?st_file.st_ino;
}

?

轉載于:https://www.cnblogs.com/ringwang/archive/2009/04/05/1429853.html

總結

以上是生活随笔為你收集整理的pwd 命令编写的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。