One 、chdir function

1、 effect : Modify the path of the current process

2、 The function prototype :

   #include <unistd.h> int chdir(const char *path);

Two 、getcwd function

1、 effect : Get the current process working directory

2、 The function prototype :

   #include <unistd.h>
 
           char *getcwd(char *buf, size_t size);
 
           char *getwd(char *buf);

notes : In general chdir function and getcwd Functions are used together

chdir Functions and getcwd The use of functions :

#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<stdlib.h>#include<stdio.h>  int main(int argc, char *argv[] ){
 
if( argc<2 ){perror("./a.out filepath");exit(1);}
 
printf(" agrv[1] = %s\n",argv[1]);//  Modify the current path int ret =chdir(argv[1]);if( ret == -1 ){perror("chdir");exit(1);}
 
//  Here, by creating a new file in the changed Directory , To prove that the catalog has changed int fd = open("chdir.txt",O_CREAT|O_RDWR,0644);if( fd == -1 ){perror("open");exit(1);}
 
close(fd);
 
//  Get the name of the changed directory char buf[100]={0};
 
getcwd(buf,sizeof(buf));printf("current dir: %s\n",buf);
 
return 0;}

3、 ... and 、rmdir function

1、 effect : Delete a directory

2、 The function prototype :

#include <unistd.h> int rmdir(const char *pathname);

Four 、mkdir function

1、 effect : Create a directory
2、 The function prototype :

#include <sys/stat.h>#include <sys/types.h>   int mkdir(const char *pathname, mode_t mode);

5、 ... and 、opendir function

1、 effect : Open a directory

2、 The function prototype :

 #include <sys/types.h>
   #include <dirent.h>
 
       DIR *opendir(const char *name);   DIR *fdopendir(int fd);

3、 Return value :

(1)、DIR Structure pointer , The structure is an internal structure , Save information about the open directory , Acting on something similar to FILE structure .

(2)、 Function error , return NULL

6、 ... and 、readdir function

1、 effect : Read the table of contents

2、 The function prototype :

 #include <dirent.h>
 
       struct dirent *readdir(DIR *dirp);

3、 Return value :

Returns a record entry

 struct dirent {ino_t          d_ino;       /* inode number */                  //  The entry point of the directory is  inode off_t          d_off;       /* not an offset; see NOTES */      //  The shift of the directory entry point from the beginning of the directory file header unsigned short d_reclen;    /* length of this record */         // d_name  length unsigned char  d_type;      /* type of file; not supported      // d_name  The folder in question  
                                              by all filesystem types */char           d_name[256]; /* filename */                      //  file name  };

d_tyep Yes 8 Types :

        (1)、 DT_BLK      This is a block device.            Block device (2)、 DT_CHR      This is a character device.        Character device (3)、 DT_DIR       This is a directory.               Catalog  (4)、 DT_FIFO     This is a named pipe (FIFO).       The Conduit (5)、 DT_LNK      This is a symbolic link.           Soft link (6)、 DT_REG      This is a regular file.            Ordinary documents (7)、 DT_SOCK     This is a UNIX domain socket.      Socket (8)、 DT_UNKNOWN     The file type is unknown.          Unknown type 

7、 ... and 、closedir function

1、 effect : Close a directory

2、 The function prototype :

#include <sys/types.h>

#include <dirent.h>

int closedir(DIR *dirp);

3、 Return value :

If the function executes successfully , return 0; If it fails , return -1.

opendir、readdir、closedir Three functions Comprehensive use of :

#include<unistd.h>#include<dirent.h>#include<sys/types.h>#include<sys/stat.h>#include<stdlib.h>#include<stdio.h>#include<string.h>
 
 //  obtain  root  The number of files in the directory int get_file_count(char *root){// open dir
DIR * dir = NULL;
dir = opendir(root);if( NULL == dir ){perror("opendir");exit(1);}
 
//  Traverse the currently open directory struct dirent* ptr = NULL;char path[1024]={0};int total = 0;while( (ptr = readdir(dir) )!= NULL){//  To filter out  .  and  ..if( strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0 ){continue;}//  If it's a catalog , Read the directory recursively if(ptr->d_type == DT_DIR){sprintf(path,"%s/%s",root,ptr->d_name);
total += get_file_count(path);}
 
//  If it's a regular document if( ptr->d_type == DT_REG ){
total++;}}//  Close directory closedir(dir);return total;}
 int main(int argc,char *argv[]){if( argc<2 ){perror("./a.out dir\n");exit(1);}
 
//  Get the number of files int count =get_file_count(argv[1]);
 
printf("%s has file numbers : %d\n",argv[1],count);return 0;}

8、 ... and 、dup and dup2 function

1、 effect : Copy the existing file descriptor

2、 The function prototype :

#include <unistd.h>

int dup(int oldfd);
int dup2(int oldfd, int newfd);

3、 Return value :

(1)、dup Returns the unoccupied... In the file descriptor

(2)、dup2 In two cases :

(a)、oldfd----->newfd If newfd Is an open file descriptor , Turn it off before copying newfd

(b)、oldfd------>newfd It's the same file descriptor , It won't turn off newfd , Go straight back to oldfd

  #include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<stdio.h>#include<stdlib.h>#include<string.h> int main(void){int fd =open("a.txt",O_RDWR);if( fd == -1 ){perror("open");exit(1);} 
     printf("file open fd = %d\n",fd); 
     //  Find the process file descriptor table    =======  first ==========  Available file descriptors //  Copy the file specified by the parameter to the description            Return this descriptor  
     int ret = dup(fd);if( fd == -1 ){perror("dup");exit(1);} 
     printf(" dup fd = %d\n",ret);char *buf = " Are you the rescuer the monkey asked ??\n";char *buf1 = " damn you , I'm a procedural ape !!!\n"; 
     write(fd,buf,strlen(buf));write(ret,buf1,strlen(buf1)); 
     close(fd);
     return 0;}

 #include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<stdio.h>#include<stdlib.h>#include<string.h> int main(void){int fd =open("english.txt",O_RDWR);if( fd == -1 ){perror("open");exit(1);} 
     int fd1 =open("a.txt",O_RDWR);if( fd1 == -1 ){perror("open");exit(1);} 
     printf("fd = %d\n",fd);printf("fd1 = %d\n",fd1); 
     int ret = dup2(fd1, fd);if( ret == -1 ){perror("dup2");exit(1);} 
     printf(" current fd = %d\n",ret); 
     char *buf = " It is your style that matters  !!!!!!!!!!!!!!!!!\n"; 
     write(fd,buf,strlen(buf));write(fd1,"hello world!",12); 
     close(fd);close(fd1); 
     return 0;}

Nine 、fcntl function

1、 effect : Change the properties of an open file

2、 The function prototype :

 #include <unistd.h>
 #include <fcntl.h>
 
   int fcntl(int fd, int cmd, ... /* arg */ );              This is a function with variable length parameters 

3、 function :

(1)、 Copy an existing descriptor --------cmd F_DUPFD

(2)、 get / Set file status --------cmd( The parameters are set as follows )

(a)、F_GETFD

(b)、F_STFD

(3)、 get / Set file tag status -------- cmd

(a)、

  O_RDONLY                       Read only open
    O_WRONLY                      Just write open
    O_RDWR                          Read write on
    O_EXEC                            Execute open
    O_SEARCH                       Search opens
    O_APPEND                       Append open
    O_NONBLOCK                  Non-blocking mode 

(b)、F_SETFL

 O_APPEND                     
 O_NONBLOCK

(4)、 get / Set asynchronous I / O ownership -------- cmd

(a)、F_GETOWN
(b)、F_SETOWN

(5)、 get / Set record lock -------- cmd
(a)、F_GETLK
(b)、F_SETLK
、SETLKW

   #include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<stdio.h>#include<stdlib.h>#include<string.h> int main(void){int flag;int fd; 
     //  Test string  
     char *p = " We are a socialist country with special envoys from China !!!!!";char *q =" ha-ha , Socialism is good "; 
     //  Open the file in write only mode  
     fd = open("test.txt",O_WRONLY);if( fd == -1 ){perror("open");exit(1);
     } 
     //  Enter new content , The content will cover the original content if( write(fd,p,strlen(p)) == -1 ){perror("write");exit(1);} 
     // Use  F_GETFL  Command to get the file status flag int flags = fcntl(fd,F_GETFL,0); 
     if( flags == -1 ){perror("fcntl");exit(1);} 
     // Add the file status flag to  “ Additional writing ”  Options  
     flag |= O_APPEND; 
     //  Change the file status to append write  
     if( fcntl(fd,F_SETFL,flag) == -1 ){perror("fcntl");exit(1);} 
     //  Enter new content again , The content is appended to the old content pair  
     if( write(fd,q,strlen(q)) == -1 ){perror("write again");exit(1);}
     return 0;}