stat

"stat"は指定されたファイルの情報を返す関数です.
"stat, fstat, lstat"は"stat"構造体を返します.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);

struct stat {
   dev_t     st_dev;     /* ID of device containing file */
   ino_t     st_ino;     /* inode number */
   mode_t    st_mode;    /* protection */
   nlink_t   st_nlink;   /* number of hard links */
   uid_t     st_uid;     /* user ID of owner */
   gid_t     st_gid;     /* group ID of owner */
   dev_t     st_rdev;    /* device ID (if special file) */
   off_t     st_size;    /* total size, in bytes */
   blksize_t st_blksize; /* blocksize for file system I/O */
   blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
   time_t    st_atime;   /* time of last access */
   time_t    st_mtime;   /* time of last modification */
   time_t    st_ctime;   /* time of last status change */
};


/* マクロ */
S_ISREG(m)  is it a regular file?
S_ISDIR(m)  directory?
S_ISCHR(m)  character device?
S_ISBLK(m)  block device?
S_ISFIFO(m) FIFO (named pipe)?
S_ISLNK(m)  symbolic link?  (Not in POSIX.1-1996.)
S_ISSOCK(m) socket?  (Not in POSIX.1-1996.)


stat example

/* stat.c */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>

void print_stat(const char *name);
void *return_type(const dev_t mode);

int main(int argc, char **argv)
{
    int i;

    if(argc  < 2) {
        fprintf(stderr, "usage: %s Filename...\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    for(i = 1; i  < argc; i++)
        print_stat(argv[i]);

    return 0;
}

void print_stat(const char *name) {
    struct stat file;

    if(stat(name, &file) == EOF) {
        perror("stat");
        exit(EXIT_FAILURE);
    }

    else {
        printf(" Name: %s\n", name);
        printf(" Type: %s\n", return_type(file.st_mode));
        printf(" Size: %u Byte\n", (unsigned)file.st_size);
        printf("Links: %d\n", file.st_nlink);
        printf("  uid: %d\n", (int)file.st_uid);
        printf("  gid: %d\n", (int)file.st_gid);
        printf(" mode: %o\n\n", file.st_mode & ~S_IFMT);
        printf("      Last Access: %s", ctime(&(file.st_atime)));
        printf("Last Modification: %s", ctime(&(file.st_mtime)));
        printf("      Last Change: %s\n", ctime(&(file.st_ctime)));
    }
}

void *return_type(const dev_t mode) {
    if(S_ISREG(mode))       return "通常のファイル";
    else if(S_ISDIR(mode))  return "ディレクトリ";
    else if(S_ISCHR(mode))  return "キャラクターデバイス";
    else if(S_ISBLK(mode))  return "ブロックデバイス";
    else if(S_ISFIFO(mode)) return "名前つきパイプ";
    else if(S_ISLNK(mode))  return "シンボリックリンク";
    else if(S_ISSOCK(mode)) return "ソケット";
}
プログラムのメインはこの場所です.
if(stat(name, &file) == EOF) {
perror("stat");
exit(EXIT_FAILURE);
}
要点はこの部分だけなのでここを説明します.
"stat"は失敗した場合"EOF"(環境によって値が変わる場合がありますが, -1で定義されています.)を返します.
成功したらポインタで指定した構造体にファイルの情報を格納します.


実行例

$ ./stat stat
Name: stat
Type: 通常のファイル
Size: 6730 Byte
Links: 1
uid: 1000
gid: 100
mode: 755
Last Access: Sat Feb 16 15:22:09 2013
Last Modification: Sat Feb 16 15:22:00 2013
Last Change: Sat Feb 16 15:22:00 2013