Linux system programming: Write a pwd command, linuxlinuxpwd

Source: Internet
Author: User

Linux system programming: Write a pwd command, linuxlinuxpwd

Pwd command: print the current working directory

We all know that each directory has two special directories (... and...), the current directory, and the upper directory. Each directory has an I node associated with it.

ghostwu@ubuntu:~$ ls -i3677860 bak        3670018 examples.desktop     1507 python3678042 core       3670033 Music                1506 shell_script   1505 c_program  3672560 note              3670169 software3672551 data       3675147 php               3678095 tags3670028 Desktop     150874 php_study         3670030 Templates3670032 Documents  3670034 Pictures          3677997 unix3670029 Downloads  3670031 Public            3670035 Videos

The inode values of each file and directory can be displayed through ls-I. For example, I will use ls-ia to display inode values of all files.

1. When working in the basic directory, the inode value of basic (that is,.) in the current directory is 1573909,...: 1507.

2. When you switch the path to python,.: 1507 is exactly the same as that of basic. And so on

Through inode association, we can find out the hierarchical relationship of directories. The next question is: How do you know that you have reached the root directory?

ghostwu@ubuntu:~/python/basic$ ls -ia1573909 .            8913 person2.class.py       8405 test1.py   1507 ..           3427 person3.class.py       8897 test2.py   8910 func2.py     8916 person4.class.py       4537 test3.py   8911 func3.py     8912 person.class.py        8908 test4.py   8909 func.py      8915 superlist.class.pyghostwu@ubuntu:~/python/basic$ cd ..ghostwu@ubuntu:~/python$ ls -ia   1507 .  3670017 ..  1573911 advance  1573909 basic   151172 djangoghostwu@ubuntu:~/python$ cd ..ghostwu@ubuntu:~$ ls -ia3670017 .                 3672499 .mysql_history      2 ..                3677054 .navicat64   3695 .adobe            3672560 note1050432 .atom             3675147 php...

Under the root directory. and .., their inode nodes have the same characteristics, so as long as the inode in the current directory is determined to be equal to the inode in the upper directory, it can be determined that the inode will arrive at the root directory.

ghostwu@ubuntu:/$ ls -1ia      2 .      2 .. 915713 bin      2 boot 130818 cdrom      3 dev 523265 etc      2 home     ...

1. The first problem to be solved: if the corresponding inode value is obtained through the path/file name, the struct stat structure of the file/directory is obtained through the stat function, file information is saved here, including inode

1/* ============================================== =========================================== 2 * Copyright (C) 2018. all rights reserved. 3*4 * file name: pwd. c 5 * Creator: ghostwu (Wu Hua) 6 * creation Date: July 7 * description: compile the pwd command in 8*9 ============================================== =============================== */10 11 # include <stdio. h> 12 # include <sys/types. h> 13 # include <sys/stat. h> 14 # include <unistd. h> 15 # include <stdlib. h> 16 17 18 // read the current file's I Node 19 ino_t get_inode (char * name); 20 21 int main (int argc, char * argv []) 22 {23 printf ("Current directory. inode = % ld \ n ", get_inode (". "); 24 printf (" upper directory .. inode = % ld \ n ", get_inode (".. "); 25 return 0; 26} 27 28 29 ino_t get_inode (char * name) {30 struct stat statinfo; 31 if (-1 = stat (name, & statinfo) {32 printf ("failed to open file % s \ n", name); 33 exit (-1); 34} 35 return statinfo. st_ino; 36}View Code

2. Complete pwd source code

1/* ============================================== =========================================== 2 * Copyright (C) 2018. all rights reserved. 3*4 * file name: pwd. c 5 * Creator: ghostwu (Wu Hua) 6 * creation Date: July 7 * description: compile the pwd command in 8*9 ============================================== =============================== */10 11 # include <stdio. h> 12 # include <sys/types. h> 13 # include <sys/stat. h> 14 # include <unistd. h> 15 # include <stdlib. h> 16 # include <dirent. H> 17 # include <string. h> 18 19 # ifndef BUFSIZE20 # define BUFSIZE 10021 # endif22 23 // read the current file I node 25 ino_t get_inode (char * name); 26 void printpathto (ino_t cur_node ); 27 // based on the current inode node, find its corresponding path name 28 void inode_to_name (ino_t cur_node, char * str, int bufsize); 29 30 int main (int argc, char * argv []) 31 {32 // printf ("Current directory. inode = % ld \ n ", get_inode (". "); 33 // printf (" upper directory .. inode = % ld \ n ", get_inode (".. "); 34 printpathto (get_inode (". "); 35 putchar ('\ n'); 36 return 0; 37} 38 39 void printpathto (ino_t cur_node) {40 41 char dir_name [BUFSIZE]; 42 ino_t my_node; 43 // if the current node is not equal .., it indicates that the root directory 44 if (cur_node! = Get_inode (".. ") {45 // switch to the upper directory, the current directory (.) in the upper-level directory (..) 46 // before finding the name, switch to the upper-level directory 47 chdir (".. "); 48 inode_to_name (cur_node, dir_name, BUFSIZE); 49 // chdir (".. "); // It cannot be placed here. The directory name is not found here. 50 my_node = get_inode (". "); 51 printpathto (my_node); 52 printf ("/% s ", dir_name); 53} 54} 55 56 void inode_to_name (ino_t cur_node, char * str, int bufsize) {57 DIR * dir_entry; 58 struct dirent * pCurDir; 59 if (di R_entry = opendir (". ") = NULL) {60 printf (" open cur directory error \ n "); 61 exit (-1 ); 62} 63 // printf ("cur inode = % ld \ n", cur_node); 64 while (pCurDir = readdir (dir_entry ))! = NULL) {65 if (cur_node = pCurDir-> d_ino) {66 // printf ("% s \ n", pCurDir-> d_name); 67 strncpy (str, pCurDir-> d_name, bufsize); 68 str [bufsize-1] = '\ 0'; 69 closedir (dir_entry); 70 return; 71} 72} 73} 74 75 76 ino_t get_inode (char * name) {77 struct stat statinfo; 78 if (-1 = stat (name, & statinfo )) {79 printf ("failed to open file % s \ n", name); 80 exit (-1); 81} 82 return statinfo. st_ino; 83}View Code

Effect after running:

ghostwu@ubuntu:~/c_program/linux_unix/chapter4$ ./pwd/ghostwu/c_program/linux_unix/chapter4

A layer of home is missing, and the layer of home is stopped.

ghostwu@ubuntu:/home$ ls -ia      2 .        2 ..  3670017 ghostwu       11 lost+found

The home layer is indeed equal? Why is this happening? Because/home is a partition, in linux, each partition has an independent root directory structure,/home is the root node of the partition, but is mounted to the root partition (/) below

Summary:

1) linux file partition and Structure

2) directory and file cascade through inode

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.