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.
+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
2010-03-29 16:56:19
Thank you for the quick answer, but can I find somwhere a full code about it?
2010-03-29 17:07:20
@user304414: Did you try the man page?
Ignacio Vazquez-Abrams
2010-03-29 17:09:17
I did, but I found just pieces.
2010-03-29 17:11:56
@user304414: Did you try the versions of the man pages I linked? They have real examples below them.
Jefromi
2010-03-29 17:25:37
Yes, I did, but those are just examples, not a full program. I can learn just from a functioning program code.
2010-03-29 17:28:29
@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
2010-03-29 17:33:38