提问人:Maruko 提问时间:10/17/2023 最后编辑:Maruko 更新时间:10/17/2023 访问量:81
如何在 C 语言中编写 root 拥有的文件?
How to write a file owned by root in C?
问:
我怎样才能写进root拥有的文件,例如在/etc中? 特别是,我需要让普通用户拥有可执行文件,因此我不能以 root 身份运行它,也不能使用 sudo 调用它。
我知道这不是一个特定的 C 问题,但是我想知道 C 中可能的解决方案。 另一种语言的相同解决方案也可能有所帮助。
使用 C 编程语言的示例
/*
How can I write into a file owned by root user?
-rwxrwxr-x 1 user user 8480 ott 17 11:48 test
-rw-rw-r-- 1 user user 432 ott 17 11:48 test.c
-rw-rw-r-- 1 root root 20 ott 17 11:48 textfile.txt
$ ./test
Test how to write a file owned by root
File can't be opened
Segmentation fault (core dumped)
*/
#include <stdio.h>
int main(int argc, char **argv)
{
printf("Test how to write a file owned by root\n");
FILE *fp;
fp = fopen("textfile.txt", "w");
if (fp != NULL) {
fprintf(fp, "Wite success!\n");
printf("\n Details successfully written to the file\n\n");
}
else
printf("File can't be opened\n");
fclose(fp);
return 0;
}
谢谢
答: 暂无答案
评论
fprintf(fp, "Wite success!\r\n");
'\r'
"w"
fwrite()
"\r\n"
"\n"
"...success!\r\r\n"
"\r\n"
"wb"