標準/拡張機能 | C/C++ | 依存項目 |
---|---|---|
POSIX.1 |
両方 |
#define _POSIX_SOURCE
#include <unistd.h>
int chown(const char *pathname, uid_t owner, gid_t group);
ファイルの所有者またはグループ (あるいはその両方) を変更します。pathname は、その所有者またはグループを 変更したいファイルの名前です。owner は、ファイルの新しい所有者のユーザー ID (UID) です。group は、ファイルの新規グループの グループ ID (GID) です。
_POSIX_CHOWN_RESTRICTED が unistd.h ヘッダー・ファイルに定義して ある場合、以下の条件のいずれかに該当するときにのみ、プロセスは、ファイルの グループを変更できます。
pathname が正規ファイルであるときに、ファイル・モードの S_IXUSR、 S_IXGRP、または S_IXOTH ビットの 1 つ以上が設定 されている場合、chown() は、ファイル・モード の set-user-ID (S_ISUID) ビットと set-group-ID (S_ISGID) ビットを クリアし、正常に戻ります。
pathname が正規ファイルでないときに、ファイル・モードの S_IXUSR、 S_IXGRP、または S_IXOTH ビットの 1 つ以上が設定 されている場合、chown() は、ファイルの set-user-ID (S_ISUID) ビット と set-group-ID (S_ISGID) ビットをクリアします。
chown() は、正常終了の場合には、ファイル の st_ctime フィールドに更新済みのマークを付けます。
XPG4.2 の特殊な動作: owner または group を それぞれ (uid_t)-1 または (gid_t)-1 と指定した 場合、ファイルの対応する ID は変更されません。
chown() は、正常終了の場合には、ファイルの所有者、グループ、および変更時刻を更新して 0 を戻します。
⁄* CELEBC12
This example changes the owner and group of a file.
*⁄
#define _POSIX_SOURCE
#include <unistd.h>
#include <sys⁄stat.h>
#include <sys⁄types.h>
#undef _POSIX_SOURCE
#include <stdio.h>
main() {
char fn[]=".⁄temp.file";
FILE *stream;
struct stat info;
if ((stream = fopen(fn, "w")) == NULL)
perror("fopen() error");
else {
fclose(stream);
stat(fn, &info);
printf("original owner was %d and group was %d¥n", info.st_uid,
info.st_gid);
if (chown(fn, 25, 0) != 0)
perror("chown() error");
else {
stat(fn, &info);
printf("after chown(), owner is %d and group is %d¥n",
info.st_uid, info.st_gid);
}
unlink(fn);
}
}
original owner was 0 and group was 0
after chown(), owner is 25 and group is 0