tags:

views:

130

answers:

2

Hi! I am new to C programming and I'd like to implement chmod command on files of a dir and subdir. How can I change/show permissions with a C code? Could someone help with a example? I would appreciate if anyone can provide me a code.

A: 

with the GNU C library you should be able to do it directly with

int chmod (const char *filename, mode_t mode)
int chown (const char *filename, uid_t owner, gid_t group)

check it out here.. all these functions are in sys/stat.h

Jack
+3  A: 

There's a chmod function. From man 3p chmod:

SYNOPSIS
   #include <sys/stat.h>

   int chmod(const char *path, mode_t mode);

...

If you want to read the permissions, you'd use stat. From man 3p stat:

SYNOPSIS
   #include <sys/stat.h>

   int stat(const char *restrict path, struct stat *restrict buf);

...

If you want to do it recursively like you mentioned, you'll have to do the looping over results of readdir yourself.

Jefromi
Thank you for the quick answer, but can I find somwhere a full code about it?
@user304414: Did you try the man page?
Ignacio Vazquez-Abrams
I did, but I found just pieces.
@user304414: Did you try the versions of the man pages I linked? They have real examples below them.
Jefromi
Yes, I did, but those are just examples, not a full program. I can learn just from a functioning program code.
@user304414: Those are full examples of the calls to these functions. If you want a full program, wrap it in a main, declare the necessary variables...
Jefromi