我怎样才能复制从一个已经存在的文件的权限? [英] How can I copy permissions from a file that already exists?

查看:146
本文介绍了我怎样才能复制从一个已经存在的文件的权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须写在C程序(一个类Unix系统上),这是我的问题:

I have to write a program in C (on a Unix-like system) and this is my problem:

我有一个文件(FILE1),我想创建另一个文件(文件2),其中有文件1的相同的权限。然后,我要创建另一个文件(FILE3),其中有文件1的相同的权限,但只有它的主人。

I have a file (FILE1) and I want to create another file (FILE2) which has the same permissions of FILE1. Then I have to create another file (FILE3) which has the same permissions of FILE1 but only for the owner.

我会用chmod()来更改权限,但我不明白如何获得FILE1的权限。

I would use chmod() to change permissions but I don't understand how to obtain the permissions of FILE1.

您可以帮帮我吗?

推荐答案

STAT() FSTAT()功能检索 struct stat中,其中包括一名成员 ST_MODE 指示文件模式,在权限存储。

The stat() and fstat() functions retrieve a struct stat, which includes a member st_mode indicating the file mode, where the permissions are stored.

您可以将该值传递给 CHMOD() fchmod()掩盖了非后的文件 - 权限位:

You can pass this value to chmod() or fchmod() after masking out the non-file-permission bits:

struct stat st;

if (stat(file1, &st))
{
    perror("stat");
} 
else
{
    if (chmod(file2, st.st_mode & 07777))
    {
        perror("chmod");
    }
}

这篇关于我怎样才能复制从一个已经存在的文件的权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆