Parsing Linux file and directory operations related functions _c language

Source: Internet
Author: User

struct stat
{
mode_t    st_mode;    file type. File Permissions
ino_t     st_ino;        I node number
dev_t     st_dev;       
dev_t    st_rdev;     Device file serial number
nlink_t    st_nlink;    link
uid_t    st_uid;
gid_t     st_gid;        User ID
off_t    st_size;    file size, this field is only meaningful for normal files, directory files, and symbolic connections.
time_t    st_atime;    Last access time
time_t    st_mtime;     The last modification time of the content of the file
time_t    st_ctime;    file status last modified
long    st_blksize;   
long     st_blocks
};

1,stat function to obtain 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 a named file is a symbolic connection, LSTAT returns information about the symbolic connection rather than the file referenced by the symbol connection

2,access function to determine file permissions
#include <unistd.h>
int access (const char *name, int mode);
Return: If the success is 0, if the error is-1
The mode constant of the access function, taken from the <unistd.h>
Mode description
R_OK Test Read License right
W_OK Test Write permission right
X_OK Test Execution License right
F_OK whether the test file exists

3,umask function settings file Create screen Word
#include <sys/types.h>
#include <sys/stat.h>
mode_t umask (mode_t Task);
Back: Previous file method Create mask Word

4,chmod function to modify the permissions of a 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 function returns: if the success is 0, if the error is-1


The 5,chown function can be used to change the user ID and group ID of a 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 function returns: if the success is 0, if the error is-1

6, the truncated file at the end of the file can call function truncate and ftruncate. Shortening the length of a file to 0 is a special case, with the O_TRUNC flag to do this.
#include <sys/types.h>
#include <unistd.h>
int truncate (const char *pathname, off_t
length);
int ftruncate (int filedes, off_t length);
two function returns; 0 for success; 1 for error

7, the way to create a connection to an existing file is to use the link function, which would like to be hard connected to Ln. only the Superuser process can create a new connection to a directory. The reason for this is that this may form a loop in the file system, and most utilities that handle the file system cannot handle this situation
#include <unistd.h>
int link (const char*oldpath, const char *newpath);
Return: If the success is 0, if the error is-1

To delete an existing directory entry, you can call the unlink function.
#include <unistd.h>
int unlink (const char *pathname);
Return: If the success is 0, if the error is-1. This function deletes the directory entry and drops the connection count of the file referenced by pathname by 1.

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

The Symlink function creates a symbolic connection. Equivalent to soft connection, ln-s
#include <unistd.h>
int symlink (const char *oldpath, const char *sympath);
Return: If the success is 0, if the error is-1

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);
Return: If the success is the number of bytes read, if the error is-1
This function combines all the actions of open, read, and close.

8, create the directory with the MkDir function and delete the directory with the RmDir function.
#include <sys/types.h>
#include <sys/stat.h>
int mkdir (const char *pathname, mode_t mode);
Return: If the success is 0, if the error is-1
#include <unistd.h>
int rmdir (const char *pathname);
Return: If the success is 0, if the error is-1

The 9,remove function unlocks a connection to a file or directory. For files, remove has the same function as unlink. For catalogs, remove has the same function as rmdir.
#include <stdio.h>
int remove (const char *pathname);
Return: If the success is 0, if the error is-1

The file or directory is renamed with the Rename function.
#include <stdio.h>
int rename (const char *oldname, const char *newwname);
Return: If the success is 0, if the error is-1

10, the access and modification time of a file can be changed with the Utime function.
#include <sys/types.h>
#include <utime.h>
int utime (const char *name, const struct UTIMEBUF *t);
Return: If the success is 0, if the error is-1
If the Times is a null pointer, both the access time and the modification time are set to the current time;
If the Times is a non-empty pointer, the access time and the modification time are set to the values in the structure to which the Times refers. At this point, the active user ID of the process must equal the owner ID of the file, or the process must be a superuser process. It's not enough to have write permission on a file

The structure used by this function is:
struct UTIMBUF {
time_t Actime; /*access time*/
time_t Modtime; /*modification time*/
}

11, the Operation function of the file directory, Opendir Readdir Rewinddir
#include <sys/types.h>
#include <dirent.h>
DIR *opendir (const char *pathname);
Return: If the success is a pointer, if the error is null

struct Dirent *readdir (DIR *dr);
Return: If the success is a pointer, if the end of the directory or error is null

void Rewinddir (DIR *dr);
Resets the location of the read directory to the beginning

int close (DIR *dr); Return: If the success is 0, if the error is-1

The dirent structure defined in the header file <dirent.h> is related to the implementation. This structure contains at least 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 the failure returns NULL, BUF is the character array where the path is stored and the size is length

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.