Functions that parse Linux files and directory operations

  • 2020-04-02 01:15:42
  • OfStack

Struct stat
{
mode_t       St_mode;       File type, file permissions
ino_t         St_ino;               I node number
dev_t       The st_dev;              
dev_t       St_rdev;       Device file number
nlink_t       St_nlink;       link
uid_t       St_uid;
gid_t         St_gid;               The user ID
off_t       St_size;       File size, this field only makes sense for plain files, directory files, and symbolic joins.
time_t       St_atime;       Last access time
time_t       St_mtime;       The last time the file contents were modified
time_t       St_ctime;       Last modified time of file state
long       St_blksize;      
long         St_blocks;
};

1. The stat function gets the file information.
# include < Sys/types. H >
# include < Sys/stat. H >
Int stat(const char *pathname, struct stat *buf);
Int fstat (int fd,struct stat *buf);
Int lstat(const char *pathname, struct stat *buf);

The lstat function is similar to stat, but when the named file is a symbolic connection,lstat returns information about the symbolic connection, not about the file referenced by the symbolic connection

2. The access function determines the file permission
# include < Unistd. H >
Int access (const char *name, int mode);
Returns: 0 for success or -1 for error
The mode constant of the access function, taken from < Unistd. H >
mode                                 said     Ming
R_OK                                   Test read permissions
W_OK                                 Test write permission
X_OK                               Test execution permission
F_OK                               Test whether the file exists

3, the umask function sets the file to create a mask word
# include < Sys/types. H >
# include < Sys/stat. H >
Mode_t umask (mode_t task);
Return: previous file mode to create a masked word

4. The chmod function is used to modify the permissions of the file
# include < Sys/types. H >
# include < Sys/stat. H >
Int chmod(const char *pathname, mode_t mode);
Int fchmod(int fd, mode_t mode);
Two functions return 0 for success and -1 for error


5, the chown function can be used to change the user ID and group ID of the file.
# include < Sys/types. H >
# include < Unistd. H >
Int chown(const char *pathname,uid_t owner,gid_t group);
Int fchown(int fd, uid_t owner, gid_t group);
Int lchown(const char *pathname, uid_t owner, gid_t group);
Three functions return 0 on success and -1 on error

6, truncate the file at the end of the file can call the functions truncate and ftruncate. Truncating a file to 0 is a special case, which can be done with the O_TRUNC flag.
# include < Sys/types. H >
# include < Unistd. H >
Int truncate(const char *pathname, off_t
Length);                                                                              
Int ftruncate(int filedes, off_t length);
Two functions return; Zero for success and minus one for error

7. The way to create a connection to an existing file is to use the link function, as in the case of hard connection ln. Only the superuser process can create a new connection to a directory. The reason is that doing so can create loops in the file system, which most utilities dealing with file systems cannot handle
# include < Unistd. H >
Int link(const char * oldpath, const char *newpath);
Returns: 0 for success or -1 for error

To delete an existing directory entry, call the unlink function.
# include < Unistd. H >
Int unlink(const char *pathname);
Returns: 0 for success or -1 for error. This function deletes the directory entry and subtracts 1 from the connection count of the file referenced by pathname.

Some limitations of hard connections: (a) hard connections generally require that the connection and the file be on the same file system, and (b) that only the superuser can create a hard connection to a directory.

The symlink function creates a symbolic connection. That's the same thing as a soft connection, ln minus s
# include < Unistd. H >
Int symlink (const char * oldpath, const char * sympath);
Returns: 0 for success or -1 for error

Because the open function follows a symbolic connection, you need a way to open the connection itself and read the name in the connection.
The readlink function provides this functionality.
# include < Unistd. H >
Int readlink(const char * pathname, char * buf, int bufsize);
Returns: number of bytes read on success or -1 on error
This function combines all the operations of open, read, and close.

8, use mkdir to create directories and rmdir to delete directories.
# include < Sys/types. H >
# include < Sys/stat. H >
Int mkdir(const char *pathname, mode_t mode);
Returns: 0 for success or -1 for error
# include < Unistd. H >
Int rmdir(const char *pathname);
Returns: 0 for success or -1 for error

The remove function disconnects a file or directory. For files, remove has the same function as unlink. For directories, remove has the same function as rmdir.
# include < stdio.h >
Int remove(const char *pathname);
Returns: 0 for success or -1 for error

Rename a file or directory with the rename function.
# include < stdio.h >
Int rename(const char *oldname, const char *newwname);
Returns: 0 for success or -1 for error

The access and modification times of a file can be changed using the utime function.
# include < Sys/types. H >
# include < Utime. H >
Int utime (const char *name, const struct utimebuf *t);
Returns: 0 for success or -1 for error
If times is a null pointer, both the access time and the modification time are set to the current time.
If times is a non-null pointer, the access time and modification time are set to the values in the structure that times points to. At this point, the valid user ID of the process must be equal to the owner ID of the file, or the process must be a superuser process. It is not enough to have write permissions on a file

The structure used by this function is:
Struct utimbuf {
Time_t actime;                              
Time_t modtime;                            
}

11. Opendir readdir rewinddir
# include < Sys/types. H >
# include < Dirent. H >
DIR *opendir(const char *pathname);
Returns: pointer on success or NULL on error

Struct dirent * readdir (DIR * Dr);
Returns: pointer if successful, NULL if at the end of the directory or error

Void rewinddir (DIR * Dr);
Reset the location of the read directory to begin

Int close (DIR * Dr); Returns: 0 for success or -1 for error

Defined in the header file < Dirent. H > The dirent structure in is implementation-related. This structure contains at least the following two members:
Struct dirent {
      Ino_t d_ino;
      Char d_name [NAME_MAX + 1];
}

12, chdir, change the current directory
# include < Unistd. H >
Int chdir (const char * pathname);
Int pchdir (int fd);

Getcwd, get the full path to the current directory.
# include < Unistd. H >
Char *getcwd(char *buf, size_t size);
If NULL is returned, buf is the array of characters for the stored path and size is the length


Related articles: