[英]Create read only folder in C language
I am using C language and Linux OS as my programming platform. 我正在使用C语言和Linux OS作为我的编程平台。 And I want to know how can I make a read-only folder programmatically? 我想知道如何以编程方式制作一个只读文件夹? Is there any mkdir command in C language for Linux or Unix-like system? 对于Linux或类Unix系统,是否有C语言的mkdir命令?
Thanks. 谢谢。
做到这一点的方法是使用mkdir(2)
创建文件夹,用所需的文件填充该文件夹,使用stat(2)
获取当前权限,屏蔽掉写入位,然后使用chmod(2)
设置权限。
You can use this one: 您可以使用以下一种:
#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);
You can use the mkdir()
function 您可以使用mkdir()
函数
Synopsis: 概要:
#include <sys/stat.h>
int mkdir(const char *path, mode_t mode);
For example to create a folder named 'hello' that is accessible only by the current user: 例如,创建一个名为“ hello”的文件夹,该文件夹只能由当前用户访问:
mkdir("hello", 0700); /*the second argument is the permission mask*/
For further info type on the terminal 有关更多信息,请在终端上输入
man 2 mkdir
If you feel creative you can do this in a more naive way 如果您有创造力,可以以更幼稚的方式进行
system("mkdir hello");
system("chmod 700 hello");
but there's no reason to do that... 但是没有理由这样做
umask应该工作
#include <stat.h>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.