Search code examples
c++linuxfile-locking

Lock / Prevent edit of source files on Linux using C++


How can I programmatically lock/unlock, or otherwise prevent/enable editing, a source file on Linux using C++.

I want to be able to lock source file so that if I open it in an editor it will not allow me to save back to the same source file.

I am thinking of maybe changing the permissions to read-only (and change it back to read-write later): how do I do that from C++?


Solution

  • Try man fchmod:

    NAME
           chmod, fchmod - change permissions of a file
    
    SYNOPSIS
           #include <sys/types.h>
           #include <sys/stat.h>
    
           int chmod(const char *path, mode_t mode);
           int fchmod(int fildes, mode_t mode);