chdir
Modify the current process path;
- SYNOPSIS:
#include <unistd.h> int chdir(const char *path);
- DESCRIPTION:
- path: the working directory to be changed, similar to the cd command;
- RETURN VALUE:
0 is returned for success and - 1 is returned for failure;
getcwd
Obtain the working directory of the current process;
- SYNOPSIS:
#include <unistd.h> char *getcwd(char *buf, size_t size);
- DESCRIPTION:
- buf: save the obtained directory;
- Size: the size of a given buf;
- RETURN VALUE:
0 is returned for success and - 1 is returned for failure;
rmdir
- SYNOPSIS:
Delete an empty directory;#include <unistd.h> int rmdir(const char *pathname);
- DESCRIPTION:
- pathname: Specifies the directory to delete;
- RETURN VALUE:
0 is returned for success and - 1 is returned for failure;
opendir
Open a directory;
-
SYNOPSIS:
#include <sys/types.h> #include <dirent.h> DIR *opendir(const char *name);
-
DESCRIPTION:
- name: specify the directory to open;
-
RETURN VALUE:
If it is opened successfully, a DIR pointer will be returned, which is an internal structure, similar to the FILE structure; NULL is returned when opening fails;
readdir
readdir can be used to traverse all files in the specified directory path. However, for sub files that do not contain subdirectories, if you want to traverse recursively, you can use depth traversal or breadth traversal algorithms;
- SYNOPSIS:
#include <dirent.h> struct dirent *readdir(DIR *dirp);
- DESCRIPTION:
- `dirp: DIR pointer returned when opening directory;
- RETURN VALUE:
If the reading is successful, the struct dirty pointer will be returned to judge the file type. If the reading is completed, NULL will be returned; - dirent structure:
struct dirent { ino_t d_ino; /* Directory entry point */ off_t d_off; /* The displacement from the beginning of the directory file to the entry point of the directory */ unsigned short d_reclen; /* d_name Length of */ unsigned char d_type; /* d_name File type */ char d_name[256]; /* file name */ }; /* d_type: DT_BLK This is a block device. DT_CHR This is a character device. DT_DIR This is a directory. DT_FIFO This is a named pipe (FIFO). DT_LNK This is a symbolic link. DT_REG This is a regular file. DT_SOCK This is a UNIX domain socket. DT_UNKNOWN The file type is unknown. */
- Ergodic basic model
int getFile(char *baseDir) { DIR *dirp; struct dirent* dp; // Open Directory if ((dirp = opendir(base_dir)) == NULL) { perror("opendir error"); return -1; } // Start traversal while ((dp = readdir(dirp)) != NULL) { // Ignore current directory '.' And the upper level directory ".." if (0 == strcmp(dp->d_name, ".") || 0 == strcmp(dp->d_name, "..")) continue; // Read file type char type[50]; switch (dp->d_type) { case DT_DIR: //If you deeply traverse the subdirectories under the directory, you need to use a recursive algorithm here break; case DT_REG: breeak; case DT_BLK: break; case DT_CHR: break; case DT_FIFO: break; case DT_LNK: break; case DT_SOCK: break; default: break; } } // Close directory closedir(dirp); return 0; }
closedir
Close a directory;
-
SYNOPSIS:
#include <sys/types.h> #include <dirent.h> int closedir(DIR *dirp);
-
DESCRIPTION:
- dirp: specify the directory to close;
-
RETURN VALUE:
0 is returned for success and - 1 is returned for failure;
dup/dup2
Copy of file descriptors (two file descriptors correspond to the same file);
- SYNOPSIS:
#include <unistd.h> int dup(int oldfd); int dup2(int oldfd, int newfd);
- DESCRIPTION:
- oldfd: old file descriptor;
- Newfd: oldfd and newfd point to different files. Close newfd before copying, and then oldfd copies to newfd and returns newfd. If oldfd and newfd are the same file descriptor, newfd will not be turned off and will be returned directly.
- RETURN VALUE:
dup returns the smallest file descriptor that is not occupied in the file descriptor table. dup2 will return newfd;
fcntl
You can change the properties of the open file;
- SYNOPSIS:
- Copy an existing descriptor (F_DUPFD). Like the function of the dup function, copy the file descriptor pointed by fd. After the call is successful, a new file descriptor is returned and points to the same file together with the old file descriptor.
- Gets / sets the tag of the file descriptor
- F_GETFD
- F_SETFD
- Get / set file status flag:
- F_GETFL: when using the GET command, the third parameter is set to 0, and the variable is used to receive the return value of the function
- O_RDONLY: open read-only;
- O_WRONLY: open write only;
- O_RDWR: read / write on;
- O_EXEC: execute open;
- O_SEARCH: search open directory;
- O_APPEND: append write
- O_NONBLOCK: non blocking mode;
- F_ Setfl (the following identifiers can be changed): usually F first_ Getfl gets the flag, and then (| =) the new flag as the third parameter
- O_APPEND
- O_NONBLOCK
- F_GETFL: when using the GET command, the third parameter is set to 0, and the variable is used to receive the return value of the function
- Get / set asynchronous I/O ownership
- F_GETOWN
- F_SETOWN
- Obtain / set record lock
- F_GETLK
- F_SETLK
- F_SETLKW
#include<unistd.h> #include<fcntl.h> int fcntl(int fd, int cmd); int fcntl(int fd, int cmd, long arg); int fcntl(int fd, int cmd ,struct flock* lock);