C语言chmod()什么作用
答案:3 悬赏:10 手机版
解决时间 2021-12-04 07:15
- 提问者网友:謫仙
- 2021-12-03 20:08
C语言chmod()什么作用
最佳答案
- 五星知识达人网友:未来江山和你
- 2021-12-03 21:38
函数名称:chmod
函数原型:int chmod( const char *filename, int pmode );
所属库:io.h
函数功能:改变文件的读写许可设置,如果改变成功返回0,否则返回-1
这个例子中实现了把文件sample.txt设置为只读文件:
#include
#include
#include
int main(void)
{
if( chmod("D:\\sample.txt",S_IREAD)==-1) {
cprintf("文件sample.txt不存在\n");
}
else {
cprintf("文件sample.txt变为只读文件\n");
}
return 0;
}
函数原型:int chmod( const char *filename, int pmode );
所属库:io.h
函数功能:改变文件的读写许可设置,如果改变成功返回0,否则返回-1
这个例子中实现了把文件sample.txt设置为只读文件:
#include
#include
#include
int main(void)
{
if( chmod("D:\\sample.txt",S_IREAD)==-1) {
cprintf("文件sample.txt不存在\n");
}
else {
cprintf("文件sample.txt变为只读文件\n");
}
return 0;
}
全部回答
- 1楼网友:刀戟声无边
- 2021-12-04 00:25
chmod - change mode of a fileSYNOPSIS
#include
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.
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 during 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 during execution of the function.
[EINVAL]
The value of the mode argument is invalid.
[ELOOP]
More than {SYMLOOP_MAX} symbolic links were encountered during 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
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
const char *path;
...
chmod(path, S_IRWXU);
#include
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
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 during 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 during execution of the function.
[EINVAL]
The value of the mode argument is invalid.
[ELOOP]
More than {SYMLOOP_MAX} symbolic links were encountered during 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
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
const char *path;
...
chmod(path, S_IRWXU);
参考资料:API\susv3\index.html
- 2楼网友:我住北渡口
- 2021-12-03 22:54
chmod u+s :设置文件的SUID. 只对可执行文件有效,对目录无效.如果一个文件 的拥有者(一般是root)将一个可执行文件设为SUID,则这个文件将变成所有人都可以执行!!!
chmod g+s :将一个目录设置成为SGID,则这个目录下不管预设的群组是什么,都将属于原组,这样可以确保一个组的成员能够完全的享用文件.
chmod o+t :设置文件或目录的粘贴位(sticky),除了root和文件的创建者,其他任何人都不能对文件和目录下的文件进行删除和更名
chmod g+s :将一个目录设置成为SGID,则这个目录下不管预设的群组是什么,都将属于原组,这样可以确保一个组的成员能够完全的享用文件.
chmod o+t :设置文件或目录的粘贴位(sticky),除了root和文件的创建者,其他任何人都不能对文件和目录下的文件进行删除和更名
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯