C语言chmod()什么作用

发布网友

我来回答

3个回答

热心网友

chmod u+s :设置文件的SUID. 只对可执行文件有效,对目录无效.如果一个文件 的拥有者(一般是root)将一个可执行文件设为SUID,则这个文件将变成所有人都可以执行!!!
chmod g+s :将一个目录设置成为SGID,则这个目录下不管预设的群组是什么,都将属于原组,这样可以确保一个组的成员能够完全的享用文件.
chmod o+t :设置文件或目录的粘贴位(sticky),除了root和文件的创建者,其他任何人都不能对文件和目录下的文件进行删除和更名

热心网友

chmod - change mode of a fileSYNOPSIS
#include <sys/stat.h>

int chmod(const char *path, mode_tmode);

DESCRIPTION

The chmod() function shall change S_ISUID, S_ISGID, [XSI] S_ISVTX, and the file permission bits of the file named by the pathname pointed to by the path argument to the corresponding bits in the mode argument. The application shall ensure that the effective user ID of the process matches the owner of the file or the process has appropriate privileges in order to do this.
S_ISUID, S_ISGID, [XSI] S_ISVTX, and the file permission bits are described in <sys/stat.h>.
If the calling process does not have appropriate privileges, and if the group ID of the file does not match the effective group ID or one of the supplementary group IDs and if the file is a regular file, bit S_ISGID (set-group-ID on execution) in the file's mode shall be cleared upon successful return from chmod().
Additional implementation-defined restrictions may cause the S_ISUID and S_ISGID bits in mode to be ignored.
The effect on file descriptors for files open at the time of a call to chmod() is implementation-defined.
Upon successful completion, chmod() shall mark for update the st_ctime field of the file.
RETURN VALUE

Upon successful completion, 0 shall be returned; otherwise, -1 shall be returned and errno set to indicate the error. If -1 is returned, no change to the file mode occurs.
ERRORS

The chmod() function shall fail if:
[EACCES]

Search permission is denied on a component of the path prefix.
[ELOOP]
A loop exists in symbolic links encountered ring resolution of the path argument.
[ENAMETOOLONG]
The length of the path argument exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}.
[ENOTDIR]
A component of the path prefix is not a directory.
[ENOENT]
A component of path does not name an existing file or path is an empty string.
[EPERM]
The effective user ID does not match the owner of the file and the process does not have appropriate privileges.
[EROFS]
The named file resides on a read-only file system.
The chmod() function may fail if:
[EINTR]

A signal was caught ring execution of the function.
[EINVAL]
The value of the mode argument is invalid.
[ELOOP]
More than {SYMLOOP_MAX} symbolic links were encountered ring resolution of the path argument.
[ENAMETOOLONG]
As a result of encountering a symbolic link in resolution of the path argument, the length of the substituted pathname strings exceeded {PATH_MAX}.
The following sections are informative.
EXAMPLESSetting Read Permissions for User, Group, and Others

The following example sets read permissions for the owner, group, and others.
#include <sys/stat.h>

const char *path;
...
chmod(path, S_IRUSR|S_IRGRP|S_IROTH);
Setting Read, Write, and Execute Permissions for the Owner Only
The following example sets read, write, and execute permissions for the owner, and no permissions for group and others.
#include <sys/stat.h>

const char *path;
...
chmod(path, S_IRWXU);

参考资料:API\susv3\index.html

热心网友

函数名称:chmod
函数原型:int chmod( const char *filename, int pmode );
所属库:io.h
函数功能:改变文件的读写许可设置,如果改变成功返回0,否则返回-1

这个例子中实现了把文件sample.txt设置为只读文件:
  #include <conio.h>
  #include <io.h>
  #include <sys/stat.h>
  int main(void)
  {
  if( chmod("D:\\sample.txt",S_IREAD)==-1) {
  cprintf("文件sample.txt不存在\n");
  }
  else {
  cprintf("文件sample.txt变为只读文件\n");
  }

  return 0;
  }

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com