Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1782988
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: LINUX

2015-10-30 16:57:42

原始pwd的实现请参照Coreutils.

点击(此处)折叠或打开

  1. #include <apue.h>
  2. #include <dirent.h>
  3. #include <sys/types.h>

  4. //BUFSIZ is defined in stdio.h: it is the maximum string size
  5. #define MAXPATH BUFSIZ


  6. //print_pwd prints the pwd to the string abs_pathname and NULL_terminates it
  7. void print_pwd(char *abs_pathname);

  8. //print_path prints to str the path from / to the file specified by cur_inum
  9. void print_path(char *str,ino_t cur_inum);

  10. //inum_to_name puts the filename of the ifle with i-node inum into string buf,
  11. //at most len chars long and 0 terminates it
  12. void inum_to_name(ino_t inum,char *buf,int len);

  13. //get_ino gets the i-node number of file fname,if that fnmae is the name of a file
  14. //in the current working directory ,Returns 0 if successful,-1 if not.
  15. int get_ino(char *fnmae,ino_t *inum);

  16. //****************************************
  17. int main(int argc, char* argv[])
  18. {
  19.     char path[MAXPATH]="\0"; //string to store pwd
  20.     print_pwd(path); //print pwd to string path
  21.     printf("%s\n",path); //print path to stdout
  22.     return 0;
  23.     }


  24. //***************************************
  25. void print_pwd(char *pathname)
  26. {
  27.     ino_t inum;
  28.     get_ino(".",&inum);
  29.     print_path(pathname,inum);
  30.     }


  31. //********************************************
  32. void print_path(char *abs_pathname,ino_t this_inode)
  33. {
  34.     ino_t parent_inode;
  35.     char its_name[BUFSIZ];

  36.     //get inumber of parent
  37.     get_ino("..",&parent_inode);


  38.     //at root if parent inum==cur inum
  39.     if(parent_inode != this_inode){
  40.         chdir(".."); //cd up to parent
  41.         //get filename of current file
  42.         inum_to_name(this_inode,its_name,BUFSIZ);
  43.         //recursively get path to parent directory
  44.         print_path(abs_pathname,parent_inode);
  45.         strcat(abs_pathname,"/");
  46.         strcat(abs_pathname,its_name);
  47.         }
  48.     else
  49.         strcat(abs_pathname,"/");
  50.         }

  51. //*****************************************************
  52. void inum_to_name(ino_t inode_to_find,char *namebuf,int buflen)
  53. {
  54.     DIR *dir_ptr;
  55.     struct dirent *direntp;
  56.     dir_ptr=opendir(".");
  57.     if(dir_ptr==NULL){
  58.         perror(".");
  59.         exit(1);
  60.         }

  61.     //search directory for a file with specified inum
  62.     while((direntp=readdir(dir_ptr))!=NULL)
  63.         if(direntp->d_ino==inode_to_find){
  64.             strncpy(namebuf,direntp->d_name,buflen);
  65.             namebuf[buflen-1]='\0';
  66.             closedir(dir_ptr);
  67.             return;
  68.             }
  69.     fprintf(stderr,"\nError looking for i-node number %d\n",inode_to_find);
  70.     exit(1);
  71.     }

  72. //**************************************************************
  73. int get_ino(char *fname,ino_t *inum)
  74. {
  75.     struct stat info;
  76.     if(stat(fname,&info)==-1){
  77.         fprintf(stderr,"Can not stat ");
  78.         perror(fname);
  79.         return -1;
  80.         }
  81.     *inum=info.st_ino;
  82.     return 0;
  83.     }

点击(此处)折叠或打开

  1. #include <apue.h>
  2. #include <dirent.h>
  3. #include <sys/types.h>

  4. //BUFSIZ is defined in stdio.h: it is the maximum string size
  5. #define MAXPATH BUFSIZ


  6. //print_pwd prints the pwd to the string abs_pathname and NULL_terminates it
  7. void print_pwd(char *abs_pathname);

  8. //print_path prints to str the path from / to the file specified by cur_inum on the
  9. //device with device id dev_num
  10. void print_path(char *str,ino_t cur_inum,dev_t dev_num);

  11. //inum_to_name puts the filename of the file with i-node inum on device
  12. //with device number dev_num into string buf,at most len chars long and 0
  13. //terminates it
  14. void inum_to_name(ino_t inum,dev_t dev_num,char *buf,int len);

  15. //get_dev_ino gets the device id and the i-node number of file fname,if that fname is the name of a file
  16. //in the current working directory ,Returns 0 if successful,-1 if not.
  17. int get_deviceid_inode(const char *fname,dev_t *dev_id, ino_t *inum);

  18. //****************************************
  19. int main(int argc, char* argv[])
  20. {
  21.     char path[MAXPATH]="\0"; //string to store pwd
  22.     print_pwd(path); //print pwd to string path
  23.     printf("%s\n",path); //print path to stdout
  24.     return 0;
  25.     }


  26. //***************************************
  27. void print_pwd(char *abs_pathname)
  28. {
  29.     ino_t inum;
  30.     dev_t devnum;

  31.     get_deviceid_inode(".",&devnum,&inum);
  32.     print_path(abs_pathname,inum,devnum);
  33.     }


  34. //********************************************
  35. void print_path(char *abs_pathname,ino_t this_inode,dev_t this_dev)
  36. //recusively prints path leading down to file with this inode on this_dev
  37. //use static int height to dtermine which resursive level it is in
  38. {
  39.     ino_t parent_inode;
  40.     char its_name[BUFSIZ];
  41.     dev_t dev_of_node,dev_of_parent;
  42.     static int height=0;

  43.     //get device id and inumber of parent
  44.     get_deviceid_inode("..",&dev_of_parent,&parent_inode);

  45.     //at root if parent inum==cur inum & device ids are same
  46.     if((parent_inode != this_inode)||(dev_of_parent!=this_dev)){
  47.         chdir(".."); //cd up to parent
  48.         //get filename of current file
  49.         inum_to_name(this_inode,this_dev,its_name,BUFSIZ);
  50.         height++; //about to make resursive call
  51.         //recursively get path to parent directory
  52.         print_path(abs_pathname,parent_inode,dev_of_parent);
  53.         strcat(abs_pathname,its_name);
  54.         if (1 < height)
  55.             /* since height is decremented whenever we leave call it can only be > 1
  56.             if we have not popped all calls from the stack
  57.             */
  58.             strcat(abs_pathname,"/");
  59.         height--;
  60.         }
  61.     else //must be at root
  62.         strcat(abs_pathname,"/");
  63.         }

  64. //*****************************************************
  65. void inum_to_name(ino_t inode_to_find,dev_t devnum,char *name,int buflen)
  66. {
  67.     DIR *dir_ptr;
  68.     struct dirent *direntp;
  69.     struct stat statbuf;

  70.     dir_ptr=opendir(".");
  71.     if(dir_ptr==NULL){
  72.         perror(".");
  73.         exit(1);
  74.         }

  75.     //search directory for a file with specified inum
  76.     while((direntp=readdir(dir_ptr))!=NULL){
  77.         if(-1==(stat(direntp->d_name,&statbuf))){
  78.             fprintf(stderr,"could not stat");
  79.             perror(direntp->d_name);
  80.             exit(1);
  81.             }
  82.         if((statbuf.st_ino==inode_to_find)&&(statbuf.st_dev==devnum)){
  83.             strncpy(name,direntp->d_name,buflen);
  84.             name[buflen-1]='\0';
  85.             closedir(dir_ptr);
  86.             return;
  87.             }
  88.         }
  89.     fprintf(stderr,"\nError looking for i-node number %d\n",inode_to_find);
  90.     exit(1);
  91.     }

  92. //**************************************************************
  93. int get_deviceid_inode(const char *fname,dev_t *dev_id,ino_t *inum)
  94. {
  95.     struct stat info;
  96.     if(stat(fname,&info)==-1){
  97.         fprintf(stderr,"Can not stat ");
  98.         perror(fname);
  99.         return -1;
  100.         }
  101.     *inum=info.st_ino;
  102.     *dev_id=info.st_dev;
  103.     return 0;
  104.     }


阅读(1681) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~