Hi,
I am using C language and Linux OS as my programming platform. 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?
Thanks.
Hi,
I am using C language and Linux OS as my programming platform. 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?
Thanks.
The way to do it is to use mkdir(2)
to create the folder, populate it with the files you want it to have, use stat(2)
to get the current permissions, mask out the write bits, then use chmod(2)
to set the permissions.
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
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:
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...