NAME

stat 获取文件属性

这个函数位于头文件里

函数原型:

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

參数:

path  文件路径+文件名称

buf     指向buffer的指针

返回值:

-1   遇到错误

0    成功返回

函数作用:

把path文件的信息拷贝到指针buf所指的结构体中。

描写叙述

stat结构体:

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 filesystem 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 */

};

dev_t包括设备文件的ID

st_inoi结点

st_mode文件类型和许可权限

st_nline文件链接数

st_uid用户全部者的ID

st_gid所属组的ID

st_rdev设备ID(假设指定文件)

st_size 所占的字节数

st_blksize文件系统I/O块大小

st_block分配的512B大小的

st_atime最后訪问时间

st_mtime最后改动时间

st_ctime状态最后改变时间

以下的POSIX宏定义用于核对st_mode域的文件类型

S_ISREG(m) 是常规文件?

S_ISDIR(m) 文件夹?

S_ISCHR(m) 字符设备?

S_ISBLK(m) 块设备?

S_ISFIFO(m) FIFO?

S_ISLNK(m) 符号链接? (Not in POSIX.1-1996.)

S_ISSOCK(m) 套接字? (Not in POSIX.1-1996.)

以下的标志用于st_mode域:

S_IFMT 0170000 bit mask for the file type bit fields

S_IFSOCK 0140000 socket

S_IFLNK 0120000 symbolic link

S_IFREG 0100000 regular file

S_IFBLK 0060000 block device

S_IFDIR 0040000 directory

S_IFCHR 0020000 character device

S_IFIFO 0010000 FIFO

S_ISUID 0004000 set-user-ID bit

S_ISGID 0002000 set-group-ID bit (see below)

S_ISVTX 0001000 sticky bit (see below)

S_IRWXU 00700 mask for file owner permissions

S_IRUSR 00400 owner has read permission

S_IWUSR 00200 owner has write permission

S_IXUSR 00100 owner has execute permission

S_IRWXG 00070 mask for group permissions

S_IRGRP 00040 group has read permission

S_IWGRP 00020 group has write permission

S_IXGRP 00010 group has execute permission

S_IRWXO 00007 mask for permissions for others (not in group)

S_IROTH 00004 others have read permission

S_IWOTH 00002 others have write permission

S_IXOTH 00001 others have execute permission

实例:

#include

#include

#include

#include

#include

int main(int argc, char *argv[])

{

struct stat info;

if (argc != 2)

{

fprintf(stderr, "Usage: %s

", argv[0]);

exit(EXIT_FAILURE);

}

if (stat(argv[1], &info) == -1)

{

perror("stat");

exit(EXIT_FAILURE);

}

printf("File type; ");

switch(info.st_mode & S_IFMT)

{

case S_IFBLK: printf("block device

"); break;

case S_IFCHR: printf("character device

"); break;

case S_IFDIR: printf("directory

"); break;

case S_IFIFO: printf("FIFO pipe

"); break;

case S_IFLNK: printf("symlink

"); break;

case S_IFREG: printf("regular file

"); break;

case S_IFSOCK: printf("socket

"); break;

default: printf("unknown

"); break;

}

printf("I-node number: %ld

", (long)info.st_ino);

printf("Mode: %lo(octal)

",

(unsigned long)info.st_mode);

printf("Link count: %ld

", (long)info.st_nlink);

printf("Ownership: UID=%ld GID=%ld

",

(long)info.st_uid, (long)info.st_gid);

printf("Preferred I/O block size: %ld bytes

",

(long) info.st_blksize);

printf("File size: %lld bytes

",

(long long) info.st_size);

printf("Blocks allocated: %lld

",

(long long) info.st_blocks);

printf("Last status change: %s", ctime(&info.st_ctime));

printf("Last file access: %s", ctime(&info.st_atime));

printf("Last file modification: %s", ctime(&info.st_mtime));

exit(EXIT_SUCCESS);

}

执行结果:

liujl@liujl-ThinkPad-Edge-E431:~/linux_program/list$ ./stat stat.c

File type; regular file

I-node number: 679622

Mode: 100644(octal)

Link count: 1

Ownership: UID=1000 GID=1000

Preferred I/O block size: 4096 bytes

File size: 2102 bytes

Blocks allocated: 8

Last status change: Wed Jul 16 23:26:20 2014

Last file access: Wed Jul 16 23:26:35 2014

Last file modification: Wed Jul 16 23:26:20 2014

附录为/usr/include/sys/stat.h源代码:

/*-

* Copyright (c) 1982, 1986, 1989 The Regents of the University of California.

* All rights reserved.

*

* Redistribution and use in source and binary forms, with or without

* modification, are permitted provided that the following conditions

* are met:

* 1. Redistributions of source code must retain the above copyright

* notice, this list of conditions and the following disclaimer.

* 2. Redistributions in binary form must reproduce the above copyright

* notice, this list of conditions and the following disclaimer in the

* documentation and/or other materials provided with the distribution.

* 3. All advertising materials mentioning features or use of this software

* must display the following acknowledgement:

*This product includes software developed by the University of

*California, Berkeley and its contributors.

* 4. Neither the name of the University nor the names of its contributors

* may be used to endorse or promote products derived from this software

* without specific prior written permission.

*

* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND

* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE

* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE

* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE

* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL

* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS

* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)

* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT

* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY

* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF

* SUCH DAMAGE.

*

*@(#)stat.h7.11 (Berkeley) 3/3/91

*/

struct stat

{

dev_tst_dev;/* inode's device */

ino_tst_ino;/* inode's number */

mode_tst_mode;/* inode protection mode */

nlink_tst_nlink;/* number of hard links */

uid_tst_uid;/* user ID of the file's owner */

gid_tst_gid;/* group ID of the file's group */

dev_tst_rdev;/* device type */

off_tst_size;/* file size, in bytes */

time_tst_atime;/* time of last access */

longst_spare1;

time_tst_mtime;/* time of last data modification */

longst_spare2;

time_tst_ctime;/* time of last file status change */

longst_spare3;

longst_blksize;/* optimal blocksize for I/O */

longst_blocks;/* blocks allocated for file */

u_longst_flags;/* user defined flags for file */

u_longst_gen;/* file generation number */

};

#defineS_ISUID0004000/* set user id on execution */

#defineS_ISGID0002000/* set group id on execution */

#ifndef _POSIX_SOURCE

#defineS_ISTXT0001000/* sticky bit */

#endif

#defineS_IRWXU0000700/* RWX mask for owner */

#defineS_IRUSR0000400/* R for owner */

#defineS_IWUSR0000200/* W for owner */

#defineS_IXUSR0000100/* X for owner */

#ifndef _POSIX_SOURCE

#defineS_IREADS_IRUSR

#defineS_IWRITES_IWUSR

#defineS_IEXECS_IXUSR

#endif

#defineS_IRWXG0000070/* RWX mask for group */

#defineS_IRGRP0000040/* R for group */

#defineS_IWGRP0000020/* W for group */

#defineS_IXGRP0000010/* X for group */

#defineS_IRWXO0000007/* RWX mask for other */

#defineS_IROTH0000004/* R for other */

#defineS_IWOTH0000002/* W for other */

#defineS_IXOTH0000001/* X for other */

#ifndef _POSIX_SOURCE

#defineS_IFMT 0170000/* type of file */

#defineS_IFIFO 0010000/* named pipe (fifo) */

#defineS_IFCHR 0020000/* character special */

#defineS_IFDIR 0040000/* directory */

#defineS_IFBLK 0060000/* block special */

#defineS_IFREG 0100000/* regular */

#defineS_IFLNK 0120000/* symbolic link */

#defineS_IFSOCK 0140000/* socket */

#defineS_ISVTX 0001000/* save swapped text even after use */

#define S_BLKSIZE512/* block size used in the stat struct */

/* 0666 */

#defineDEFFILEMODE(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)

#endif

#defineS_ISDIR(m)((m & 0170000) == 0040000)/* directory */

#defineS_ISCHR(m)((m & 0170000) == 0020000)/* char special */

#defineS_ISBLK(m)((m & 0170000) == 0060000)/* block special */

#defineS_ISREG(m)((m & 0170000) == 0100000)/* regular file */

#defineS_ISFIFO(m)((m & 0170000) == 0010000)/* fifo */

#ifndef _POSIX_SOURCE

#defineS_ISLNK(m)((m & 0170000) == 0120000)/* symbolic link */

#defineS_ISSOCK(m)((m & 0170000) == 0140000)/* socket */

#endif

#ifndef KERNEL

#include

__BEGIN_DECLS

mode_tumask __P((mode_t));

intchmod __P((const char *, mode_t));

intfstat __P((int, struct stat *));

intmkdir __P((const char *, mode_t));

intmkfifo __P((const char *, mode_t));

intstat __P((const char *, struct stat *));

#ifndef _POSIX_SOURCE

intfchmod __P((int, mode_t));

intlstat __P((const char *, struct stat *));

#endif /* not POSIX */

__END_DECLS

#endif

linux c文件属性,【linux c learn 之stat】获取文件的属性相关推荐

  1. linux 默认文件属性,linux系统下文件的默认权限以及隐藏属性的作用

    [摘要] 操作系统(Operating System,简称OS)是管理计算机硬件与软件资源的计算机程序.操作系统需要处理如管理与配置内存.决定系统资源供需的优先次序.控制输入设备与输出设备.操作网络与 ...

  2. linux 默认文件属性,linux - 文件夹、文件默认属性: umask使用

    一 权限掩码umask umask是chmod配套的.总共为4位(gid/uid,属主.组权,其他用户的权限),只是通经常使用到的是后3个.比如你用chmod 755 file(此时这文件的权限是属主 ...

  3. linux smb 所有者,python-3.x - 在Linux上使用python从smb共享中获取文件的所有者。 - SO中文参考 - www.soinside.com...

    这简直不是一件小事,可惜答案并不像我希望的那样简单. 如果以后有人会被这个同样的问题困扰,我就把这个答案贴出来,但希望也许有人能早点贴出更好的解决方案来 为了找到主人,我用 本库及其实例:from s ...

  4. linux getfattr中文乱码,在bash shell中使用getfattr查看文件扩展属性

    getfattr用法 用于获取文件扩展属性,返回一系列键值对,参考Linux Man Page. 常用OPTIONS -n name, --name=name Dump the value of th ...

  5. linux把目录下的文件设置属性为rx,LINUX的文件属性与目录配置

    LINUX的文件属性与目录配置 发布时间:2008-03-20 10:28:46来源:红联作者:cnbtoo -rwxrwxrwx 1 root root 293 Oct 19 21:24 test ...

  6. linux的文件属性和权限学习——分析ls命令结果

    转自: http://blog.csdn.net/daheiantian/article/details/5974962 最近阅读<鸟哥的linux私房菜>,确实是一本好书,使自己在文件属 ...

  7. Linux—目录文件属性和权限管理详解

    关注微信公众号:CodingTechWork,一起学习进步. 引言   Linux中了解用户和用户组概念后,我们就需要对文件目录进行赋权操作,以便于文件访问权限的管控,这就涉及到文件的属性,那用户和用 ...

  8. Linux系统文件属性

    在 Linux 中第一个字符代表这个文件是目录.文件或链接文件等等. 当为 d 则是目录 当为 - 则是文件: 若是 l 则表示为链接文档(link file): 若是 b 则表示为装置文件里面的可供 ...

  9. chmod 755和chmod +x 区别 | Linux修改文件属性 | 小白笔记

    Linux chmod命令 修改文件属性 参考链接:菜鸟教程 Linux修改文件属性 | 小白笔记 认识 chmod 命令 chmod 755和chmod +x 区别 认识 chmod 命令 理解 数 ...

最新文章

  1. 小程序获取用户所在城市完整代码
  2. C++继承中构造函数、析构函数调用顺序及虚析构函数
  3. 零售行业O2O盛行 或成黑客窃取数据目标
  4. OVS 各功能调用过程(三十一)
  5. Matlab矩阵函数
  6. 2、JDBC连接数据库
  7. CSDN Markdown 博客如何设置插入代码背景颜色(设置成黑色)?
  8. php nginx配置404页面,Nginx实现404页面的几种方法
  9. ubuntu更新python的指令_ubuntu下python模块的库更新(转载)
  10. html 苹果桌面浮窗,苹果手机钉钉悬浮窗设置打开的方法
  11. Linux命令详解-mkdir
  12. Element-UI-的布局和容器---Element-UI工作笔记003
  13. 局部变量与全局变量同名时如何在局部变量的作用范围内访问全局变量?
  14. 《Processing SPARQL queries over distributed RDF graphs》——读书笔记
  15. (附源码)SSM学科竞赛赛场安排系统JAVA计算机毕业设计项目
  16. 华为开发者大会直播间鸿蒙,聊一聊华为开发者大会上的鸿蒙OS
  17. 【论文笔记】Learning from Multiple Cities: A Meta-Learning Approach for Spatial-Temporal Prediction
  18. 北极熊秀舞步神似美国明星
  19. MySQL UUID函数的详解
  20. 《图解密码技术》笔记13:PGP-密码技术的完美组合

热门文章

  1. SX1301吞吐量是SX1276/8的多少倍?
  2. 奇异值分解(SVD)原理与在降维中的应用
  3. python编程语言优缺点_原创001 第一次接触这个神奇而又无所不能的编程语言:Python...
  4. dht11温湿度传感器_Arduino不调用库实现DHT11数据读取
  5. Apache-DBUtils实现CRUD操作,已封装的API实现jdbc对数据库进行操作
  6. idea使用c3p0数据库连接池无法加载配置文件xml,配置文件放置的位置
  7. java哈希_Java如何采用哈希码实现分类(以员工分配为例)
  8. 语音识别插件_2D动画唇动合成,根据语音自动生成动画人物口型
  9. 开源RefreshListView下拉刷新效果
  10. 仪征市第二中学计算机老师,静心倾听花自开 ——仪征市第二中学徐丞老师