The chmod () function and Fchmod () function _c language in C language are compared in detail.

Source: Internet
Author: User
Tags chmod error code file permissions

C language chmod () function: Modifying file permissions
header file:

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

To define a function:

int chmod (const char * path, mode_t mode);

Function Description: chmod () Changes the permissions of the parameter path to specify the file, depending on the parameter mode permission.

There are several combinations of parameter mode:
1, s_isuid 04000 file (set User-id on execution) bit
2, S_isgid 02000 file (set Group-id on execution) bit
3, s_isvtx 01000 files of the sticky bit
4, S_IRUSR (s_iread) 00400 file owner with Read permission
5, S_IWUSR (s_iwrite) 00200 file owner with writable permission
6, S_IXUSR (s_iexec) 00100 file owner with executable permissions
7, s_irgrp 00040 user group with Read permission
8, s_iwgrp 00020 user group with writable access
9, s_ixgrp 00010 user group with executable permissions
10, S_iroth 00004 other users have Read permission
11, S_iwoth 00002 Other user with writable permission
12, S_ixoth 00001 other users have executable permissions

Note: You can modify the file permissions only if the file owner or valid user ID is 0.

Based on system security, if you want to write data to an execution file that has S_isuid or S_isgid permissions, the two bits are cleared. If a directory has s_isuid bit permissions, it means that only the owner of the file or root can delete the file under this directory.

Return value: The permission change succeeds returns 0, the failure returns 1, the error reason is in errno.

Error code:
1, the eperm process of the effective user identification code and to modify the permissions of the file owner is different, and also does not have root permissions.
2. The eaccess parameter path specifies a file that cannot be accessed.
3, Erofs to write permission files exist in the read-only file system.
4, efault parameter The path pointer exceeds the accessible memory space.
5, einval parameter mode is incorrect
6, Enametoolong parameter path is too long
7, enoent specified file does not exist
8, enotdir parameter path path is not a directory
9, Enomem core memory is not enough
10, eloop parameter path has too many symbolic connection problems.
11. Eio I/O access errors

Example

/* Set/etc/passwd file permissions to s_irusr| s_iwusr| s_irgrp| S_iroth *
/#include <sys/types.h>
#include <sys/stat.h>
main ()
{
  chmod ("/etc/ passwd ", s_irusr| s_iwusr| s_irgrp| S_iroth);
}

C language Fchmod () function: permissions to modify files
header file:

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

To define a function:

int fchmod (int fildes, mode_t mode);

Function Description: Fchmod () Changes the permissions of the Fildes file under Parameter mode permission. Parameter Fildes is the file descriptor for the open file. Parameter mode please refer to chmod ().

Return value: The permission change succeeds then returns 0, the failure returns 1, the error reason exists in errno.

Error code:
1, the EBADF parameter fildes is invalid file descriptive word.
2. The valid user identification code for the eperm process is different from the file owner who wants to modify the permissions, and does not have root permissions.
3, Erofs to write permission files exist in the read-only file system.
4, Eio I/O access errors.

Example

#include <sys/stat.h>
#include <fcntl.h>
main ()
{
  int fd;
  FD = open ("/etc/passwd", o_rdonly);
  Fchmod (FD, s_irusr| s_iwusr| s_irgrp| S_iroth);
  Close (FD);
}

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.