20155222 C language Implementation pwd command

Source: Internet
Author: User
Tags define function

20155222 C language Implementation linux PWD command
    • 1. Learn the PWD command in the Linux hierarchy, users can create a new directory with the mkdir command in any directory that is authorized, or you can use the CD command to convert from one directory to another. However, there is no prompt to tell the user which directory is currently in. To know the current directory, you can use the PWD command, which displays the entire path name.

    • 2. Study the system calls required to implement PWD

Opendir (Open Directory)
Correlation function Open,readdir,closedir,rewinddir,seekdir,telldir,scandir
Table header File

#include<sys/types.h>#include<dirent.h>

Defining functions

DIR * opendir(const char * name);

The function Description Opendir () is used to open the directory specified by the parameter name, and returns the Dir- pattern of the directory stream, similar to open (), followed by the use of this return value for reading and searching the directory.
The return value succeeds
returns the Dir-type directory stream, and the open failure returns NULL.
Error code eaccess insufficient permissions
Emfile has reached the maximum number of files that the process can open simultaneously.
Enfile has reached the maximum number of files that the system can open simultaneously.
Enotdir parameter name is not a real directory
The ENOENT parameter name specifies a directory that does not exist, or the parameter name is a null character
String.
Enomem core memory is low.

Readdir (read directory)
Correlation function Open,opendir,closedir,rewinddir,seekdir,telldir,scandir
Table header File

#include <sys/types.h>#include <dirent.h>

define function struct Dirent * readdir (dir * dir);
The function Description Readdir () returns the next directory entry point for the DIR directory stream parameter.
The structure dirent is defined as follows

struct dirent{ino_t d_ino;ff_t d_off;signed short int d_reclen;unsigned char d_type;har d_name[256;};

D_ino the inode for this directory entry point
D_off directory file at the beginning of this directory entry point displacement
D_reclen _name length, contains no null characters
D_type D_name refers to the file type
D_name file name
The return value succeeds and returns to the next directory entry point. An error occurred or was read to the end of the directory file to return NULL.
Additional Instructions EBADF parameter dir is an invalid directory stream.
 
Example:

#include<sys/types.h>#include<dirent.h>#include<unistd.h>main(){DIR * dir;struct dirent * ptr;int i;dir =opendir(“/etc/rc.d”);while((ptr = readdir(dir))!=NULL){printf(“d_name: %s\n”,ptr->d_name);}closedir(dir);}执行d_name:.d_name:..d_name:init.dd_name:rc0.dd_name:rc1.dd_name:rc2.dd_name:rc3.dd_name:rc4.dd_name:rc5.dd_name:rc6.dd_name:rcd_name:rc.locald_name:rc.sysinit

Closedir (Close directory)
Correlation function Opendir
Table header File

#include<sys/types.h>#include<dirent.h>

Defines the function int closedir (DIR *dir);
The function Description Closedir () closes the directory stream that the parameter dir refers to.
The return value closed successfully returns 0, the failure returns 1, and the reason for the error is stored in errno.
Error code EBADF parameter dir is not an invalid directory stream

    • 3. Implementation steps:
      Step one: View the current directory "." Inode-number of the file
      Step two: View the current directory "..." Inode-number of the file
      Step three: Compare two inode-number, if equal, to the root directory, enter step four, otherwise enter "..", according to the Inode-number obtained in step one, find the appropriate file name in the directory and put it into the stack, back to step one.
      Step four: Output path name of the stack in turn

    • 4. Implementing the Code

#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <dirent.h> #include < sys/stat.h> #include <fcntl.h> #include <string.h>int main () {char this_name[20]= ".", Fath        Er_name[20]= ".";        Char name[16][32];        struct Dirent *this_dir=null,*father_dir=null,*temp=null;        DIR *dir=null;        int i=0;        unsigned long int this_ino;                while (1) {dir = Opendir (this_name);                if (dir = = NULL) printf ("Error open");                        while (1) {this_dir = Readdir (dir); if (strcmp (This_dir->d_name, ".")                ==0) break;                } this_ino=this_dir->d_ino;                Closedir (dir);                dir = Opendir (father_name);                        while (1) {father_dir = Readdir (dir); if (Father_dir->d_name[0]== '. '                        &&father_dir->d_name[1]== '. ')                Break                } if (This_dir->d_ino==father_dir->d_ino) break;                Closedir (dir);                Dir=opendir (Father_name);                        while ((Temp=readdir (dir))!=null) {if (Temp->d_ino==this_ino)                    {bzero (name[i],32);                                strcat (Name[i++],temp->d_name);                        Break                }} closedir (dir);                strcat (This_name, "/..");        strcat (Father_name, "/..");}    for (i--;i>=0;i--) printf ("/%s", Name[i]); printf ("\ n");}
    • 5. Test code

20155222 C language Implementation pwd command

Related Article

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.