domlao
domlao

Reputation: 16029

Create read only folder in C language

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.

Upvotes: 1

Views: 2175

Answers (5)

Federico klez Culloca
Federico klez Culloca

Reputation: 27149

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...

Upvotes: 1

falagar
falagar

Reputation: 336

You can use this one:

#include <sys/stat.h>
#include <sys/types.h>

int mkdir(const char *pathname, mode_t mode);

Upvotes: 1

BuggerMe
BuggerMe

Reputation: 77

umask should work

#include <stat.h>

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799450

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.

Upvotes: 5

codaddict
codaddict

Reputation: 455430

You can make use of the mkdir system call:

int mkdir (const char *filename, mode_t mode);

To make the newly created folder RO (no write and no execute permission) you can make use of the mode parameter as described here

Upvotes: 3

Related Questions