在C中复制文件后保留文件所有者和权限 [英] Keeping fileowner and permissions after copying file in C

查看:12
本文介绍了在C中复制文件后保留文件所有者和权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:在 C 中,我创建了一个文件的副本(有一些更改) 这是通过 fopen()、getchar 和 putchar 轻松完成的.复制文件很好,输出文件本身就是我想要的.

here is my problem: In C, I create the copy of a file (with some changes) This is done trivially via fopen(), getchar and putchar. Copying the file is fine and the outputfile itself is what I want it to be.

我的问题是:我假设我会经常使用这个程序作为 sudo,然后生成的文件既有另一个所有者(root)又有不同的权限(执行权限消失了).

My problem is: I assume that I will use this program often as sudo and then the resulting file has both another owner (root) as well as different permissions (execute rights are gone).

我的问题是:如何复制原文件的所有者和权限,然后写入新文件?

My question is: How can I copy the owner and permissions of the original file and then write them into the new one?

推荐答案

使用 fstat(2) 系统调用获取有关所有者和权限的详细信息,以及 fchmod(2) 和 fchown(2) 系统调用来设置它们.参见*BSD cp(1) 源代码.

Use the fstat(2) system call to obtain the details about the owner and the permissions, and the fchmod(2) and fchown(2) system calls to set them. See an example in the setfile function of the *BSD cp(1) source code.

这篇关于在C中复制文件后保留文件所有者和权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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