#include <sys/types.h> #include <sys/stat.h> int chmod(const char *pathname, mode_t mode);
chmod
changes the mode bits for the directory or file specified
by pathname
.
The following symbols are defined in the <sys/stat.h>
include file:
S_ISUID
exec
function, the user id of the file owner becomes
the effective user ID of the process.
S_ISGID
exec
function, the group ID that owns the file becomes the
effective group ID of the process.
S_ISVTX
S_IRUSR
S_IWUSR
S_IXUSR
S_IRWXU
S_IRGRP
S_IWGRP
S_IXGRP
S_IRWXG
S_IROTH
S_IWOTH
S_IXOTH
S_IRWXO
A process can set mode bits if it has superuser authority, or if the user ID is the same as that of the file's owner. The S_ISGID bit in the file's mode bits is cleared if
chmod
returns 0 if it is successful and - 1 if it is not successful.
#include <sys/types.h> #include <sys/stat.h> int chexec(const char *name) { struct stat stat_data; mode_t newmode; int rc; rc = stat(name, &stat_data); if (rc != 0) { perror("stat failure"); return -1; } newmode = stat_data.st_mode; if (newmode & S_IRUSR) newmode |= S_IXUSR; if (newmode & S_IRGRP) newmode |= S_IXGRP; if (newmode & S_IROTH) newmode |= S_IXOTH; /* If the mode bits changed, make them effective. */ if (newmode != stat_data.st_mode) { rc = chmod(name, newmode); if (rc != 0) perror("chmod failure"); return rc; } return(0); /* No change was necessary. */ }
chown
, fchmod
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.