点击(此处)折叠或打开
- #include <apue.h>
- #include <dirent.h>
- #include <sys/types.h>
- //BUFSIZ is defined in stdio.h: it is the maximum string size
- #define MAXPATH BUFSIZ
- //print_pwd prints the pwd to the string abs_pathname and NULL_terminates it
- void print_pwd(char *abs_pathname);
- //print_path prints to str the path from / to the file specified by cur_inum
- void print_path(char *str,ino_t cur_inum);
- //inum_to_name puts the filename of the ifle with i-node inum into string buf,
- //at most len chars long and 0 terminates it
- void inum_to_name(ino_t inum,char *buf,int len);
- //get_ino gets the i-node number of file fname,if that fnmae is the name of a file
- //in the current working directory ,Returns 0 if successful,-1 if not.
- int get_ino(char *fnmae,ino_t *inum);
- //****************************************
- int main(int argc, char* argv[])
- {
- char path[MAXPATH]="\0"; //string to store pwd
- print_pwd(path); //print pwd to string path
- printf("%s\n",path); //print path to stdout
- return 0;
- }
- //***************************************
- void print_pwd(char *pathname)
- {
- ino_t inum;
- get_ino(".",&inum);
- print_path(pathname,inum);
- }
- //********************************************
- void print_path(char *abs_pathname,ino_t this_inode)
- {
- ino_t parent_inode;
- char its_name[BUFSIZ];
- //get inumber of parent
- get_ino("..",&parent_inode);
- //at root if parent inum==cur inum
- if(parent_inode != this_inode){
- chdir(".."); //cd up to parent
- //get filename of current file
- inum_to_name(this_inode,its_name,BUFSIZ);
- //recursively get path to parent directory
- print_path(abs_pathname,parent_inode);
- strcat(abs_pathname,"/");
- strcat(abs_pathname,its_name);
- }
- else
- strcat(abs_pathname,"/");
- }
- //*****************************************************
- void inum_to_name(ino_t inode_to_find,char *namebuf,int buflen)
- {
- DIR *dir_ptr;
- struct dirent *direntp;
- dir_ptr=opendir(".");
- if(dir_ptr==NULL){
- perror(".");
- exit(1);
- }
- //search directory for a file with specified inum
- 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,"\nError looking for i-node number %d\n",inode_to_find);
- exit(1);
- }
- //**************************************************************
- int get_ino(char *fname,ino_t *inum)
- {
- struct stat info;
- if(stat(fname,&info)==-1){
- fprintf(stderr,"Can not stat ");
- perror(fname);
- return -1;
- }
- *inum=info.st_ino;
- return 0;
- }
点击(此处)折叠或打开
- #include <apue.h>
- #include <dirent.h>
- #include <sys/types.h>
- //BUFSIZ is defined in stdio.h: it is the maximum string size
- #define MAXPATH BUFSIZ
- //print_pwd prints the pwd to the string abs_pathname and NULL_terminates it
- void print_pwd(char *abs_pathname);
- //print_path prints to str the path from / to the file specified by cur_inum on the
- //device with device id dev_num
- void print_path(char *str,ino_t cur_inum,dev_t dev_num);
- //inum_to_name puts the filename of the file with i-node inum on device
- //with device number dev_num into string buf,at most len chars long and 0
- //terminates it
- void inum_to_name(ino_t inum,dev_t dev_num,char *buf,int len);
- //get_dev_ino gets the device id and the i-node number of file fname,if that fname is the name of a file
- //in the current working directory ,Returns 0 if successful,-1 if not.
- int get_deviceid_inode(const char *fname,dev_t *dev_id, ino_t *inum);
- //****************************************
- int main(int argc, char* argv[])
- {
- char path[MAXPATH]="\0"; //string to store pwd
- print_pwd(path); //print pwd to string path
- printf("%s\n",path); //print path to stdout
- return 0;
- }
- //***************************************
- void print_pwd(char *abs_pathname)
- {
- ino_t inum;
- dev_t devnum;
- get_deviceid_inode(".",&devnum,&inum);
- print_path(abs_pathname,inum,devnum);
- }
- //********************************************
- void print_path(char *abs_pathname,ino_t this_inode,dev_t this_dev)
- //recusively prints path leading down to file with this inode on this_dev
- //use static int height to dtermine which resursive level it is in
- {
- ino_t parent_inode;
- char its_name[BUFSIZ];
- dev_t dev_of_node,dev_of_parent;
- static int height=0;
- //get device id and inumber of parent
- get_deviceid_inode("..",&dev_of_parent,&parent_inode);
- //at root if parent inum==cur inum & device ids are same
- if((parent_inode != this_inode)||(dev_of_parent!=this_dev)){
- chdir(".."); //cd up to parent
- //get filename of current file
- inum_to_name(this_inode,this_dev,its_name,BUFSIZ);
- height++; //about to make resursive call
- //recursively get path to parent directory
- print_path(abs_pathname,parent_inode,dev_of_parent);
- strcat(abs_pathname,its_name);
- if (1 < height)
- /* since height is decremented whenever we leave call it can only be > 1
- if we have not popped all calls from the stack
- */
- strcat(abs_pathname,"/");
- height--;
- }
- else //must be at root
- strcat(abs_pathname,"/");
- }
- //*****************************************************
- void inum_to_name(ino_t inode_to_find,dev_t devnum,char *name,int buflen)
- {
- DIR *dir_ptr;
- struct dirent *direntp;
- struct stat statbuf;
- dir_ptr=opendir(".");
- if(dir_ptr==NULL){
- perror(".");
- exit(1);
- }
- //search directory for a file with specified inum
- while((direntp=readdir(dir_ptr))!=NULL){
- if(-1==(stat(direntp->d_name,&statbuf))){
- fprintf(stderr,"could not stat");
- perror(direntp->d_name);
- exit(1);
- }
- if((statbuf.st_ino==inode_to_find)&&(statbuf.st_dev==devnum)){
- strncpy(name,direntp->d_name,buflen);
- name[buflen-1]='\0';
- closedir(dir_ptr);
- return;
- }
- }
- fprintf(stderr,"\nError looking for i-node number %d\n",inode_to_find);
- exit(1);
- }
- //**************************************************************
- int get_deviceid_inode(const char *fname,dev_t *dev_id,ino_t *inum)
- {
- struct stat info;
- if(stat(fname,&info)==-1){
- fprintf(stderr,"Can not stat ");
- perror(fname);
- return -1;
- }
- *inum=info.st_ino;
- *dev_id=info.st_dev;
- return 0;
- }