ページ 11

ファイルの状態を取得するシステムコールについて

Posted: 2010年12月23日(木) 20:48
by nanami
c言語で ls -al と同じ結果を表示するプログラムを作りたいのですが rwx などのアクセス権限をどうしたら表示させることが出来るのか
わかりません。どうか教えていただけないでしょうか。

Re: ファイルの状態を取得するシステムコールについて

Posted: 2010年12月23日(木) 21:34
by Poco
stat/fstatシステムコールが使えませんか?

Re: ファイルの状態を取得するシステムコールについて

Posted: 2010年12月23日(木) 21:40
by nanami
すみません,,,,
まるなげにしていました。
lsのプログラムなら作成できたのですが、statのmodeからの持ってきかたがわからないんです

1: #include <stdio.h>
2: #include <stdlib.h>
3: #include <dirent.h>
4: #include <sys/types.h>
5: #include <sys/stat.h>
6:
7: int main( int argc, char *argv[] )
8: {
9: char *dir;
10: DIR *dp;
11: struct dirent *entry;
12: struct stat statbuf;
13:
14: if(argc<2){
15: dir = getenv("PWD");
16: }else{
17: dir = argv[1];
18: }
19:
20: if(( dp = opendir(dir) ) == NULL ){
21: perror("opendir");
22: exit( EXIT_FAILURE );
23: }
24:
25: while((entry = readdir(dp)) != NULL){
26: stat(entry->d_name, &statbuf);
27: if(S_ISDIR(statbuf.st_mode)){
28: fprintf(stdout, "%s/\n", entry->d_name);
29: }else{
30: fprintf(stdout, "%s\n", entry->d_name);
31: }
32: }
33:
34: closedir(dp);
35:
36: return(0);
37: }

Re: ファイルの状態を取得するシステムコールについて

Posted: 2010年12月23日(木) 21:51
by Poco
以下のURLにあるst_mode フィールドの説明が参考になりませんか?
http://kazmax.zpp.jp/cmd/s/stat.2.html

以下のような感じのソースになるんですかねえ。。

stat(entry->d_name, &statbuf);
if ( statbuf.st_mode & S_IRGRP ) {
// グループの読み込み権があれば、、、
}

Re: ファイルの状態を取得するシステムコールについて

Posted: 2010年12月23日(木) 22:13
by nanami
ありがとうごじます^^
がんばってみます。