How do we set, reset and check the "sticky bit" from within a C program?
Thanks
To read the stick bit you use stat()
check the .st_mode
for S_ISVTX
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
struct stat file_stats;
stat("my_file", &file_stats);
if (file_stats.st_mode & S_ISVTX)
printf("sticky\n");
to reset it, you do it via chmod
struct stat file_stats;
stat("my_file", &file_stats);
mode_t new_mode = file_stats.st_mode & ~S_ISVTX;
chmod("my_file", new_mode);
to set it, chmod
it is
struct stat file_stats;
stat("my_file", &file_stats);
mode_t new_mode = file_stats.st_mode | S_ISVTX;
chmod("my_file", new_mode);
this code is untested.
man pages: stat(2) chmod(2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With