Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2275835
  • 博文数量: 668
  • 博客积分: 10016
  • 博客等级: 上将
  • 技术积分: 8588
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-29 19:22
文章分类

全部博文(668)

文章存档

2011年(1)

2010年(2)

2009年(273)

2008年(392)

分类:

2008-07-26 12:25:24

#include
#include     //exit()
#include     //strerror()
#include      //errno
#include //stat()
#include

int main(int argc, char * argv[])
{
    char *filename;
    struct stat buf;

    if (argc != 2) {
        printf("Please input filename\n");
        exit(-1);
    }
    filename = argv[1];
    if (stat(filename, &buf) < 0) {
        printf("Mesg: %s\n", strerror(errno));
        exit(-1);
    }
    printf("%s's size      is %-4d bytes\n", filename, buf.st_size);
    printf("%s's t_blksize is %-4d bytes\n", filename, buf.st_blksize);
    printf("%s's blocks    is %-4d blocks\n", filename, buf.st_blocks);
}

----------------------------------------------------

$ gcc test.c
$ ./a.out zengxiaolong
zengxiaolong's size      is 13   bytes
zengxiaolong's t_blksize is 4096 bytes
zengxiaolong's blocks    is 8    blocks
注: 文件zengxiaolong的实际内容为13个字节,占用4KB大小的磁盘空间(1 blocks -- 一块为4KB)




stat, fstat, lstat - get file status
----------------------------------------------------
    #include
    #include
    #include

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

    lstat函数与stat类似,但是当文件是一个符号连接时,lstat返回该符号连接的有关信息,而不是由该符号连接引用的文件的信息。


struct--stat
----------------------------------------------------
struct stat {
    mode_t    st_mode;    // file type & mode(permissions)
    ino_t     st_ino;     // i-node number(serial number)
    dev_t     st_dev;     // device number(filesystem)
    dev_t     st_rdev;    // device number for specials files
    nlink_t   st_nlink;   // number of links
    uid_t     st_uid;     // user ID of owner
    gid_t     st_gid;     // group ID of owner
    off_t     st_size;    // size in bytes, for regular files
    time_t    st_atime;   // time of last access
    time_t    st_mtime;   // time of last modification
    time_t    st_ctime;   // time of last file status change
    long      st_blksize; // best I/O block size
    long      st_blocks; -// number of 512-byte blocks allocated
};



struct--inode
----------------------------------------------------
/usr/src/linux/include/linux/fs.h
struct inode {
    struct hlist_node   
-----------i_hash;
    struct list_head   
------------i_list;
    struct list_head   
------------i_sb_list;
    struct list_head     
----------i_dentry;
    unsigned long        
----------i_ino;
    atomic_t             
----------i_count;
    umode_t          
--------------i_mode;
    unsigned int                    i_nlink;
    uid_t            
--------------i_uid;
    gid_t            
--------------i_gid;
    dev_t           
------------ --i_rdev;
    loff_t          
-------------- i_size;
    struct timespec                 i_atime;
    struct timespec      
----------i_mtime;
    struct timespec      
----------i_ctime;
    unsigned int         
----------i_blkbits;
    unsigned long      
------------i_version;
    blkcnt_t         
--------------i_blocks;
    unsigned short   
--------------i_bytes;
    spinlock_t           
----------i_lock;
    struct mutex                    i_mutex;
    struct rw_semaphore             i_alloc_sem;
    struct inode_operations         *i_op;
    const struct file_operations    *i_fop;
    struct super_block              *i_sb;
    struct file_lock                *i_flock;
    struct address_space            *i_mapping;
    struct address_space            i_data;
#ifdef CONFIG_QUOTA
    struct dquot                    *i_dquot[MAXQUOTAS];
#endif
    struct list_head                i_devices;
    union {
        struct pipe_inode_info      *i_pipe;
        struct block_device         *i_bdev;
        struct cdev                 *i_cdev;
    };
    int                             i_cindex;   
    __u32                           i_generation;   
#ifdef CONFIG_DNOTIFY
    unsigned long                   i_dnotify_mask;
    struct dnotify_struct           *i_dnotify;
#endif
   
#ifdef CONFIG_INOTIFY
    struct list_head                inotify_watches;
    struct mutex                    inotify_mutex;
#endif
    unsigned long                   i_state;
    unsigned long                   dirtied_when;
    unsigned int                    i_flags;
    atomic_t                        i_writecount;
#ifdef CONFIG_SECURITY
    void                            *i_security;
#endif
    void                            *i_private;
#ifdef __NEED_I_SIZE_ORDERED
    seqcount_t                      i_size_seqcount;
#endif
};



struct--stat
----------------------------------------------------
/usr/src/linux/include/asm-i386/stat.h
struct stat {
    unsigned long
--st_dev;
    unsigned long
--st_ino;
    unsigned short
-st_mode;
    unsigned short
-st_nlink;
    unsigned short
-st_uid;
    unsigned short
-st_gid;
    unsigned long
--st_rdev;
    unsigned long
--st_size;
    unsigned long
--st_blksize;
    unsigned long
--st_blocks;
    unsigned long
--st_atime;
    unsigned long
--st_atime_nsec;
    unsigned long
--st_mtime;
    unsigned long
--st_mtime_nsec;
    unsigned long
--st_ctime;
    unsigned long
--st_ctime_nsec;
    unsigned long
--__unused4;
    unsigned long
--__unused5;
};
阅读(6522) | 评论(0) | 转发(0) |
0

上一篇:hharm-2410核心板

下一篇:linux下的ed 编辑器

给主人留下些什么吧!~~