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 を返します。そうでない場合は,値 -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 のみ |