使用stat/lstat获取文件属性

头文件:#include <sys/types.h>

    #include <sys/stat.h>

int stat(const char  *path,struct  stat *struct_stat);
int lstat(const char *path,struct stat *struct_stat);

stat与lstat的区别:这两个方法区别在于stat没有处理字符链接(软链接)的能力,如果一个文件是符号链接,stat会直接返回它所
指向的文件的属性;而lstat返回的就是这个符号链接的内容。

两个函数的第一个参数都是文件的路径,第二个参数是struct stat的指针。返回值为0,表示成功执行。

error:
执行失败是,error被自动设置为下面的值:

EBADF:   文件描述词无效
EFAULT:   地址空间不可访问
ELOOP:   遍历路径时遇到太多的符号连接
ENAMETOOLONG:文件路径名太长
ENOENT:  路径名的部分组件不存在,或路径名是空字串
ENOMEM:  内存不足
ENOTDIR:  路径名的部分组件不是目录

当然除了以上方法,也可以通过文件描述符来获取文件的属性;

int fstat(int fdp, struct stat *struct_stat); //通过文件描述符获取文件对应的属性。fdp为文件描述符

struct stat:

一个关于文件属性重要的结构体

struct stat {
mode_t st_mode; //文件对应的模式,文件,目录等
ino_t st_ino; //inode节点号
dev_t st_dev; //设备号码
dev_t st_rdev; //特殊设备号码
nlink_t st_nlink; //文件的硬链接数连接数
uid_t st_uid; //文件所有者
gid_t st_gid; //文件所有者对应的组
off_t st_size; //普通文件,对应的文件字节数
time_t st_atime; //文件最后被访问的时间
time_t st_mtime; //文件内容最后被修改的时间
time_t st_ctime; //文件属性inode改变时间
blksize_t st_blksize; //文件内容对应的块大小
blkcnt_t st_blocks; //文件内容对应的块数量
};

stat结构体中的 st_mode 则定义了下列数种情况:

S_IFMT     文件类型的位遮罩
S_IFLNK   符号连接
S_IFREG   一般文件
S_IFBLK 0060000   区块装置
S_IFDIR   目录
S_IFCHR   字符装置
S_IFIFO 先进先出
S_ISUID   文件的(set user‐id on execution)位
S_ISGID   文件的(set group‐id on execution)位
S_ISVTX   文件的sticky位
S_IRUSR   文件所有者具可读取权限
S_IWUSR   文件所有者具可写入权限
S_IXUSR   文件所有者具可执行权限
S_IRGRP   用户组具可读取权限
S_IWGRP   用户组具可写入权限
S_IXGRP   用户组具可执行权限
S_IROTH   其他用户具可读取权限
S_IWOTH   其他用户具可写入权限
S_IXOTH   其他用户具可执行权限

上述的文件类型在POSIX中定义了检查这些类型的宏定义
例如:
S_ISLNK (st_mode) 判断是否为符号连接
S_ISREG (st_mode) 是否为一般文件
S_ISDIR (st_mode) 是否为目录
S_ISCHR (st_mode) 是否为字符设备文件
S_ISSOCK (st_mode) 是否为socket

文件属性获取

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h> int main(int argc,char **argv)
{
struct stat statbuff;
if(argc<)
{
printf("usage: %s [filename]",argv[]);
return -;
}
lstat(argv[],&statbuff);
if(S_ISLNK(statbuff.st_mode))
printf("%s is a link file\n",argv[]);
/*
* //也可以这么写
if(S_IFLNK == statbuff.st_mode & S_IFMT)
printf("%s is link file\n",argv[1]);
*/
if(S_ISREG(statbuff.st_mode))
printf("%s is regular file\n",argv[]);
if(S_ISDIR(statbuff.st_mode))
printf("%s is directory file\n",argv[]);
return ;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char** argv)
{
  struct stat buf;
  if(argc < )
  {
  printf("Usage: stat [filename]\n");
  return ‐;
  }
  if(stat(argv[], &buf) == ‐)
  {
    perror("stat");
    return ‐;
  }
  printf("access time %s\n", ctime(&buf.st_atime));
  printf("modify time %s\n", ctime(&buf.st_mtime));
  printf("change inode time %s\n", ctime(&buf.st_ctime));
  return ;
}
 
char *ctime(const time_t *timep); //将时间转换成字符串
 

Linux 获取文件属性的更多相关文章

  1. Atitit linux获取项目运行环境版本

    Atitit linux获取项目运行环境版本 1.1. Nginx版本1 1.2. Php版本1 1.3. Mysql版本2 1.4. Redis版本2 1.1. Nginx版本 [root@iZ25 ...

  2. Linux 获取设备树源文件(DTS)里描述的资源

    Linux 获取设备树源文件(DTS)里的资源 韩大卫@吉林师范大学 在linux使用platform_driver_register() 注册 platform_driver 时, 需要在 plat ...

  3. Linux获取线程tid线程名

    Linux获取线程tid线程名 1 2 3 4 5 6 //thread name char cThreadName[32] = {0}; prctl(PR_GET_NAME, (unsigned l ...

  4. 获取文件属性信息之stat、fstat和lstat

    UNIX文件系统是目录和文件组成的一种层次结构.目录(directory)是一个包含许多目录项的文件,在逻辑上,可以认为每个目录项都包含一个文件名,同时还包含说明该文件属性的信息.文件属性是指文件类型 ...

  5. linux系统文件属性及企业精典故障案例

    linux系统文件属性: [root@nginx_back ~]# stat keepalived-1.2.7.tar.gz 查看文件属性 File: "keepalived-1.2.7.t ...

  6. [记]WIndow/Linux 获取本机(全部)IPv4、IPv6、MAC地址方法 (C/C++)

    Linux 获取本机IP.MAC地址用法大全 //#include <sys/types.h> #include <ifaddrs.h> #include <sys/io ...

  7. Linux获取网络接口信息

    linux获取网络接口信息需要用到的函数为ioctl(),结构体struct ifreq,struct ifconf 1.ioctl()函数原型及作用 #include <sys/ioctl.h ...

  8. Windows/Linux获取当前运行程序的绝对路径

    windows 获取当前运行程序的绝对路径(.exe) GetModuleFileNameA()函数获取绝对路径,不过文件路径中的反斜杠需要进行替换. ]; GetModuleFileNameA(NU ...

  9. Go语言之进阶篇获取文件属性

    1.获取文件属性 示例: get_file_attribute.go package main import ( "fmt" "os" ) func main( ...

随机推荐

  1. nodejs-runoob

    是否安装成功nodejs - node -v 是否安装成功npm - npm -v

  2. linux下修改主机名hostname方法(转载)

    查看主机名: 在终端输入hostname 永久修改 第一步:#hostname oratest 第二步:修改/etc/sysconfig/network中的hostname 第三步:修改/etc/ho ...

  3. Memcached服务端自动启动(转载)

    Memcached服务端自动启动 原文链接:http://www.cnblogs.com/technet/archive/2011/09/11/2173485.html  经测试,要使得Memcach ...

  4. spring2实现定时任务的一种方式

    1. 在项目中放入Spring的jar包 2. applicationContext.xml的<beans xmlns>部分,添加context相关内容: <beans xmlns= ...

  5. java基础知识查漏 四

    1.JAVA多线程实现方式 (1)继承Thread类,并重写run()方法 (2)实现Runnable接口,,实现run()方法 (3)使用ExecutorService.Callable.Futur ...

  6. 5 Ways to Send Email From Linux Command Line

    https://tecadmin.net/ways-to-send-email-from-linux-command-line/ We all know the importance of email ...

  7. access 连接数据库

    前提条件 如果没有安装office的话,需要安装引擎 安装了office就不用安装引擎 连接数据库 Dim plMydb As Microsoft.Office.Interop.Access.Dao. ...

  8. Hibernate总结(转)

    原文:http://blog.csdn.net/yuebinghaoyuan/article/details/7300599 那我们看一下hibernate中整体的内容: 我们一一介绍其中的内容. H ...

  9. wifi 协议栈的历史的总结

    google 了一下找到下面的网页关于wifi 协议栈的说明 https://www.lifewire.com/wireless-standards-802-11a-802-11b-g-n-and-8 ...

  10. Impala 安装笔记1一Cloudera CDH4.3.0安装

    Impala是Cloudera在受到Google的Dremel启发下开发的实时交互SQL大数据查询工具,Impala没有再使用缓慢的Hive+MapReduce批处理,而是通过使用与商用并行关系数据库 ...