tags:

views:

70

answers:

5

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.

+3  A: 

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

codaddict
+4  A: 

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.

Ignacio Vazquez-Abrams
Why make 2 calls!?
Matt Joiner
Also what's wrong with the POSIX manpages for all the functions listed? There's no OS specific stuff required here.
Matt Joiner
If you set the mode explicitly within the `mkdir()` call then you can skip the `stat()` call, but then you end up ignoring the user's umask setting. Creating the directory read-only in the first place makes it so that you cannot later populate it. And yes, you can find each of those calls in section 3p as well.
Ignacio Vazquez-Abrams
+1  A: 

umask should work

#include <stat.h>
BuggerMe
+1  A: 

You can use this one:

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

int mkdir(const char *pathname, mode_t mode);
falagar
+1  A: 

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

klez