How to change / show permissions in C

I am new to C programming and I would like to implement the chmod command for directory and subdir files. How can I change / show permissions using C code? Can anyone help with an example? I would appreciate it if anyone could provide me with the code.

+4
source share
3 answers

There is a chmod function here. From man 3p chmod :

SYNOPSIS #include <sys/stat.h> int chmod(const char *path, mode_t mode); ... 

If you want to read permissions, 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 this recursively, as you mentioned, you have to loop through the readdir results yourself.

+9
source

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

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

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

+1
source

Example: (show / test permissions)

 struct stat st; int ret = stat(filename, &st); if(ret != 0) { return false; } if((st.st_mode & S_IWOTH) == S_IWOTH) { } else { } 
0
source

Source: https://habr.com/ru/post/1305471/


All Articles