Linux PWD instruction C implementation

Source: Internet
Author: User
Tags parent directory

Linux pwd command C realizes the whole idea
    • To implement the PWD command:
    • Enter PWD at the command line: Displays the path that is now located.

    • Study the system calls required to implement PWD

    • We can get the system call function information we need through the man command and the grep command.

    • Mans ChDir

    • The directory is a kind of file, this file is special, it is stored in a corresponding table, that is, the file name and the corresponding relationship table of I node, and I node is the structure of the record details of this file, such as file size, attributes, permissions, the existence of the hard disk block and so on. To create a file in a directory is to add a corresponding relationship in this table, using a file is based on the I node to determine the actual storage location of the hard disk.

    1. can be done by

      readdir(".")

      To get the current directory.

    2. cd ..

Cd

command to return to the parent directory, and then

readdir(".");

To read the current directory and execute it sequentially.

    1. So by implementing

      cd

      command, you can resolve

      pwd

      Command

    2. Pass

      man -k chage | grep dir

      = = Available ChDir implementation = =

    3. Run the view in the root directory

      "."
".."

Node number of the I nodes. = = can be used as the terminating condition of the loop = =.

    1. ls -i -a .
      To view the I node value.
To summarize, it is:

1. Find the I-node in this directory

2. Go to the parent directory and find the file name of the I-node

3. Cycle the above process until it reaches the root

Pseudo code
     定义一个char数组用来保存当前目录的绝对路径;     递归:    调用函数chdir()来改变当前程序的工作路径;(返回上一级)    if(返回的指针==NULL)        调用函数中存在错误,输出错误警告;    else        直接打印结果    
Code implementation

Detailed code:
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <dirent.h>ino_t get_  Inode (char*), void Printpathto (ino_t), void Inum_to_name (ino_t,char*,int), int main () {Printpathto (Get_inode ("."));    Print path to here Putchar (' \ n '); return 0;}    void Printpathto (ino_t this_inode) {ino_t my_inode;    Char Its_name[bufsiz]; /* If the I-node in this directory is different from the parent directory, that is, this directory is not the root directory */if (Get_inode ("..")!         =this_inode) {chdir ("..");        Enter the parent directory Inum_to_name (THIS_INODE,ITS_NAME,BUFSIZ);        My_inode = Get_inode (".");        Printpathto (My_inode);    printf ("/%s", its_name);    }}void inum_to_name (ino_t inode_to_find,char* namebuf,int buflen)//Find the file name of the I-node and place it in the character array {dir* dir_ptr;    struct dirent* direntp;    Dir_ptr = Opendir (".");        if (dir_ptr = = NULL) {perror (".");    Exit (1);  } while ((DIRENTP = Readdir (dir_ptr)) = NULL) {if (Direntp->d_ino = = Inode_to_find) {          strncpy (Namebuf,direntp->d_name,buflen);            Namebuf[buflen-1] = ' + ';            Closedir (DIR_PTR);        Return    }} fprintf (stderr, "error looking for inum% d\n", inode_to_find); Exit (1);}    ino_t Get_inode (char* fname)//based on the file name, returns the-I node {struct STAT info;        if (stat (fname, &info) = =-1) {fprintf (stderr, "cannot stat");        Perror (fname);    Exit (1); } return Info.st_ino;}

Linux pwd instruction C implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.