chmod、_wchmod
Io.h:インデックス への移動
ヘッダー ファイル
io.h
カテゴリ
入出力ルーチン
プロトタイプ
int chmod(const char *path, int amode);
int _wchmod(const wchar_t *path, int amode);
説明
ファイル アクセス モードを変更します。
chmod は、amode で渡されたマスクにしたがって、path によって指定されたファイルのファイル アクセス権限を設定します。path は、文字列を指しています。
amode には、シンボル定数 S_IWRITE および S_IREAD(sys\stat.h で定義)の一方または両方を入れることができます。
S_IWRITE |
書き込み許可 |
S_IREAD |
読み取り許可 |
S_IREAD | S_IWRITE |
読み書き許可(書き込みアクセス許可は、暗黙的に読み取り許可を含む) |
戻り値
ファイル アクセス モードの変更に成功した場合、chmod は 0 を返します。そうでなければ、chmod は、値 -1 を返します。
エラーのイベントでは、グローバル変数 errno に以下のいずれかの値が設定されます:
EACCES |
アクセスが拒否されました |
ENOENT |
パスまたはファイル名が見つからない |
例
#include <errno.h>
#include <stdio.h>
#include <io.h>
#include <process.h>
#include <sys\stat.h>
void main(void)
{
char filename[64];
struct stat stbuf;
int amode;
printf("Enter name of file: ");
scanf("%s", filename);
if (stat(filename, &stbuf) != 0)
{
perror("Unable to get file information");
exit(1);
}
if (stbuf.st_mode & S_IWRITE)
{
printf("Changing to read-only\n");
amode = S_IREAD;
}
else
{
printf("Changing to read-write\n");
amode = S_IREAD|S_IWRITE;
}
if (chmod(filename, amode) != 0)
{
perror("Unable to change file mode");
exit(1);
}
exit(0);
}
移植性
POSIX | Win32 | ANSI C | ANSI C++ | |
---|---|---|---|---|
chmod |
+ |
+ |
||
_wchmod |
NT のみ |