The umask of function and truncate of function in C language are briefly introduced

  • 2020-04-02 03:21:54
  • OfStack

C language umask() function: sets the permission mask when creating a new file
The header file:


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

Definition function:


mode_t umask(mode_t mask);

Function description: umask() sets the system umask value to the value after the parameter mask&0777, and then returns the previous umask value. When creating a new file with open(), this parameter mode is not the permission to actually create the file, but the permission value of (mode&~umask).

Such as:
When the file is created, the file permission is 0666, and the umask value is usually 022 by default, so the file's true permission is 0666& ~ 022 = 0644, that is, rw-r-r --r-- returns no error value on this call. The return value is the umask value of the original system.

C truncate() function: changes file size
The header file:


#include <unistd.h>

Definition function:


int truncate(const char * path, off_t length);

Truncate () changes the file size specified by the parameter path to the size specified by the parameter length.

Return value: 0 on success, -1 on failure, error in errno.

Error code:
1. The file specified by the EACCESS parameter path cannot be accessed.
2. The file to be written by EROFS exists in the read-only file system.
3. The EFAULT parameter path pointer exceeds the accessible memory space.
4. The EINVAL parameter path contains illegal characters.
5. The ENAMETOOLONG parameter path is too long.
The path to ENOTDIR is not a directory.
7. The EISDIR parameter path points to a directory.
8. The file to which the ETXTBUSY parameter path refers is a Shared program and is being executed.
9. The ELOOP parameter path has too many symbolic connections.
EIO I/O access error.


Related articles: