Change the user ID and group ID of a file
#include <sys/types.h>
#include <unistd.h>
int chown( const char * path,
uid_t owner,
gid_t group );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The chown() function changes the user ID and group ID of the file specified by path to be the numeric values contained in owner and group, respectively.
If the named file is a symbolic link, chown() changes the ownership of the file or directory that the symbolic link refers to; lchown() changes the ownership of the symbolic link file itself.
Only processes with an effective user ID equal to the file's user ID or with the IOFUNC_ABILITYID_CHOWN ability (see iofunc_ability_check()) may change the ownership of a file.
If the _POSIX_CHOWN_RESTRICTED flag (tested via the _PC_CHOWN_RESTRICTED limit in pathconf()), is set for path, then:
If chown() is successful, and:
then the set-user-ID (S_ISUID) and set-group-ID (S_ISGID) bits of the file mode are cleared.
If chown() succeeds, the st_ctime field of the file is marked for update.
/*
* Change the ownership of a list of files
* to the current user/group
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main( int argc, char** argv )
{
int i;
int ecode = 0;
for( i = 1; i < argc; i++ ) {
if( chown( argv[i], getuid(), getgid() ) == -1 ) {
perror( argv[i] );
ecode++;
}
}
exit( ecode );
}
| Safety: | |
|---|---|
| Cancellation point | No |
| Interrupt handler | No |
| Signal handler | Yes |
| Thread | Yes |