stat, fstat, and lstat Functions 

本部分讨论的内容主要围绕3个stat函数及其返回值。

#include <sys/stat.h>
int stat(const char *restrict pathname, struct stat *restrict buf);
int fstat(int filedes, struct stat *buf);
int lstat(const char *restrict pathname, struct stat *restrict buf);

stat函数返回pathname的stat结构体。

fstat返回fd代表的文件的stat结构体。

lstat与stat很相似,但是当pathname是一个软链接时,lstat返回的是软链接的信息,而不是软链接指向的文件。


File Types 

文件类型

regular file, directory file, block special file, character special file, FIFO(pipe), socket, symbolic link.

根据stat结构体中的st_mode判断文件类型

  • S_ISREG():regular file
  • S_ISDIR():directory file
  • S_ISCHR():character special file
  • S_ISBLK():block special file
  • S_ISFIFO():pipe or FIFO
  • S_ISLNK():symbolic link
  • S_ISSOCK():socket
  • S_TYPEISMQ():message queue
  • S_TYPEISSEM():semaphore
  • S_TYPEISSHM():shared memory object

access函数

测试当前用户拥有的文件权限

#include <unistd.h>
int access(const char *pathname, int mode);

mode取值:man 2 access查看帮助文档

umask函数

设置文件被创建时的权限

#include <sys/stat.h>
mode_t umask(mode_t cmask);

cmask取值:man 2 umask查看帮助文档

chmod/fchmod函数

更改文件的权限

#include <sys/stat.h>
int chmod(const char *pathname, mode_t mode);
int fchmod(int filedes, mode_t mode);

mode取值:man 2 chmod查看帮助文档

chown/fchown/lchown函数

更改文件的uid&gid

#include
int chown(const char *pathname, uid_t owner, gid_t group);
int fchown(int filedes, uid_t owner, gid_t group);
int lchown(const char *pathname, uid_t owner, gid_t group);

[Unix.C]Files and Directories的更多相关文章

  1. 10 Useful du (Disk Usage) Commands to Find Disk Usage of Files and Directories

    The Linux “du” (Disk Usage) is a standard Unix/Linux command, used to check the information of disk ...

  2. Files and Directories

    Files and Directories Introduction     In the previous chapter we coveredthe basic functions that pe ...

  3. Notes for Apue —— chapter 4 Files and Directories(文件和目录)

    4.1 Introduction 4.2 stat, fstat, fstatat, and lstat Functions The lstat function is similar to stat ...

  4. 【原】The Linux Command Line - Manipulation Files And Directories

    cp - Copy Files and directories mv - move/rename files and directories mkdir - create directories rm ...

  5. How to ignore files and directories in subversion?

    Step 1 Copy the files and directories to other place. Step 2 Delete the files and directories. Step ...

  6. Getting svn to ignore files and directories

    August 27, 2013Software Developmentresources, subversion, svn, tutorial, version control Who knew it ...

  7. UNIX高级环境编程(4)Files And Directories - umask、chmod、文件系统组织结构和链接

    本篇主要介绍文件和文件系统中常用的一些函数,文件系统的组织结构和硬链接.符号链接. 通过对这些知识的了解,可以对Linux文件系统有更为全面的了解.   1 umask函数 之前我们已经了解了每个文件 ...

  8. UNIX高级环境编程(5)Files And Directories - 文件相关时间,目录文件相关操作

     1 File Times 每个文件会维护三个时间字段,每个字段代表的时间都不同.如下表所示: 字段说明: st_mtim(the modification time)记录了文件内容最后一次被修改的时 ...

  9. UNIX高级环境编程(3)Files And Directories - stat函数,文件类型,和各种ID

    在前面的两篇,我们了解了IO操作的一些基本操作函数,包括open.read和write. 在本篇我们来学习一下文件系统的其他特性和一个文件的属性,涉及的函数功能包括: 查看文件的所有属性: 改变文件所 ...

随机推荐

  1. 【Android】 Sqlite3 not found

    调试机没有sqlite3命令文件 导入即可 sqlite3 http://pan.baidu.com/s/1bohTMiz //(使用老版sqlite3需要导入libncurses.so文件至/sys ...

  2. 移动端开发(使用webuploader上传图片,客户端交互,修改alert弹窗等)

    之前实习做的一个移动端的页面 需要的功能有图片上传 点击客户端的返回按钮 有提示(即与客户端有交互) 遇到不少的坑 总结一下问题 1.图片上传功能  使用工具 百度的webuploader 暂时遇到的 ...

  3. Mac苹果电脑加密视频播放器使用教程

    1.   下载文件 https://pan.baidu.com/s/1slhFYuL 2.    操作流程 温馨提示 播放时,请务必保证播放设备联网(原因:用户名权限验证需要网络,播放后10秒即可关闭 ...

  4. hbuider 中点击就显示出一个单选的列表 ,然后后台跨域向里面动态添加数据,注意里面的格式是json object

    jsp页面: <li class="mui-table-view-cell" onclick="showActionSheet()"> <di ...

  5. HTML5格式化

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. xml技术DTD约束定义

    XML约束 在XML技术中,可以编写一个文档来约束一个xml文档的书写规范,这称之为XML约束为什么需要XML约束? class.xml <stu><面积>?人怎么会有面积元素 ...

  7. C# MySql 操作类

    /* MySql 类 */ using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  8. 使用 windows 计划任务播放音乐文件

    这个问题网上可以搜到很多答案,但都有一些小细节没有交代,而我平时又很少使用计划任务,所以配置中出了点问题,特此备注. 1.播放器 检查当前系统下目标文件的默认播放器是什么,并且确保可以运行. 比如首次 ...

  9. maven的安装,maven库配置和Eclipse插件的安装

    maven的安装,maven库配置和Eclipse插件的安装 1.下载并解压maven 2.配置环境变量 3.配置maven配置文件 1.下载链接 Downloading Apache Maven 2 ...

  10. (00)Java编程思想开篇立言。

    从今天开始,在相当长的时间中我在看Java编程思想.也把这个博客作为开始.这就是一个读书的笔记.