Loose-Info.com
Last Update 2012/07/05
TOP - C言語 - sys/stat.h - chmod()

ファイルまたはディレクトリのアクセス・パーミッションを変更します。

 戻り値1  = chmod(  引数1  ,  引数2  )

戻り値1 :
int
実行が成功した場合 : 0
成功以外の場合 : -1
引数1 :
char *
変更するファイル(ディレクトリ)のパス
引数2 :
mode_t
アクセス・パーミッション
パーミッションの指定には下記のファイルモードを使用
sys/stat.h(Mac OS X 10.6 - [GCC 4.2])の中でのファイルモードの記述
/* File mode */ /* Read, write, execute/search by owner */ #define S_IRWXU 0000700 /* [XSI] RWX mask for owner */ #define S_IRUSR 0000400 /* [XSI] R for owner */ #define S_IWUSR 0000200 /* [XSI] W for owner */ #define S_IXUSR 0000100 /* [XSI] X for owner */ /* Read, write, execute/search by group */ #define S_IRWXG 0000070 /* [XSI] RWX mask for group */ #define S_IRGRP 0000040 /* [XSI] R for group */ #define S_IWGRP 0000020 /* [XSI] W for group */ #define S_IXGRP 0000010 /* [XSI] X for group */ /* Read, write, execute/search by others */ #define S_IRWXO 0000007 /* [XSI] RWX mask for other */ #define S_IROTH 0000004 /* [XSI] R for other */ #define S_IWOTH 0000002 /* [XSI] W for other */ #define S_IXOTH 0000001 /* [XSI] X for other */ #define S_ISUID 0004000 /* [XSI] set user id on execution */ #define S_ISGID 0002000 /* [XSI] set group id on execution */ #define S_ISVTX 0001000 /* [XSI] directory restrcted delete */ #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE) #define S_ISTXT S_ISVTX /* sticky bit: not supported */ #define S_IREAD S_IRUSR /* backward compatability */ #define S_IWRITE S_IWUSR /* backward compatability */ #define S_IEXEC S_IXUSR /* backward compatability */ #endif #endif /* !S_IFMT */

(例)
#include <stdio.h> #include <sys/stat.h> #include <dirent.h> int main() { FILE *fp; struct dirent *dirst; struct stat buf; printf("mkdir()実行 : %d\n", mkdir("test_dir_01", 0755)); stat("test_dir_01", &buf); printf("mkdir()実行後stat()実行[st_mode] : %#o\n", buf.st_mode); chmod("test_dir_01", 0777); stat("test_dir_01", &buf); printf("chmod()実行後stat()実行[st_mode] : %#o\n\n", buf.st_mode); fp = fopen("test_dir_01/test_file_01.txt", "w"); fclose(fp); DIR *dp = opendir("test_dir_01"); while((dirst = readdir(dp)) != NULL) { printf("ディレクトリ名 : [%s]\n", dirst->d_name); } closedir(dp); remove("test_dir_01/test_file_01.txt"); remove("test_dir_01"); return 0; }

実行結果
mkdir()実行 : 0 mkdir()実行後stat()実行[st_mode] : 040755 chmod()実行後stat()実行[st_mode] : 040777 ディレクトリ名 : [.] ディレクトリ名 : [..] ディレクトリ名 : [test_file_01.txt]