Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change/show permissions in C

Tags:

c

linux

chmod

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.

like image 663
speed17 Avatar asked Mar 29 '10 16:03

speed17


1 Answers

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.

like image 193
Cascabel Avatar answered Sep 23 '22 18:09

Cascabel