chown(), fchown(), lchown() - change owner and group of a file
#include <unistd.h>
int chown (const char *path, uid_t owner, gid_t group)
int fchown (int fildes, uid_t owner, gid_t group)
int lchown (const char *path, uid_t owner, gid_t group)
These calls change the owner identifier (ID) and group ID of a file. A process must have the appropriate permissions on a file to change ownership.
The owner ID and group ID of the file named by path or referenced by fildes is changed as specified by the arguments owner and group. The owner of a file may change the group to a group of which he or she is a member.
The chown(2) and lchown(2) utilities behave nearly identically, except if the file path is a symbolic link: in that case, chown(2) will change the ownership on the file referenced by the symbolic link, and lchown(2) will change the ownership on the symbolic link file itself.
All three calls clear the set-user-id and set-group-id bits on the file to prevent accidental or mischievous creation of set-user-id and set-group-id programs.
One of the owner or group IDs may be left unchanged by specifying it as -1.
Note that with Windows, it is not usually possible to give away ownership of a file. It is possible, however, to take ownership of a file, if the effective user ID of the process has the Windows Take Ownership permission for the file. Any user with the Windows privileges SE_BACKUP and SE_RESTORE may give away ownership of a file. (Normally this is only users with Administrator or Backup Operator privileges.)
The chown(2), lchown(2), and fchown(2) functions return zero if the operation was successful; they return -1 if an error occurs, placing a more specific error code in the global variable errno.
The chown(2) and lchown(2) will fail and the file will be unchanged if:
The fchown(2) function will fail and the file will be unchanged if:
The lchown(2) call can also fail for these reasons:
chgrp(1)
chmod(2)