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. Python中 if __name__ == '__main__': 详解

    一个python文件就可以看作是一个python的模块,这个python模块(.py文件)有两种使用方式:直接运行和作为模块被其他模块调用. __name__:每一个模块都有一个内置属性__name_ ...

  2. webstorm快捷方式

    刚开始在使用webstrom的时候,不知道快捷方式,感觉自己把webstorm当做记事本使用,真的挺傻的,在朋友的指导下,原来webstorm有快捷方式 一.界面操作 快捷键 说明 ctrl+shif ...

  3. 『奇葩问题集锦』npm install 报错 node-pre-gyp ERR! node-pre-gyp -v v0.6.25

    gyp ERR! configure error gyp ERR! stack Error: Can't find Python executable "python", you ...

  4. Tasklist 命令的使用

    1,根据PID查找进程 tasklist /fi "pid eq 2245" 2,根据名称查找进程 tasklist /fi "imagename eq notepad. ...

  5. SortedList的用法

    1.SortedList定义 System.Collections.SortedList类表示键/值对的集合,这些键值对按键排序并可按照键和索引访问.SortedList 在内部维护两个数组以存储列表 ...

  6. Java2_Java泛型

    一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: 1 public class GenericTest { 2 3 public static void main(Stri ...

  7. Mac 配置java版本 ---- MySql数据库权限设置 --- openfire

    java -version 显示java 版本 sudo su - root 切换身份 cd /usr/local/openfire 进入openfire目录 cd bin/ 进入 bin vim o ...

  8. C语言面试题(嵌入式开发方向,附答案及点评)

    整理自C语言面试题(嵌入式开发方向,附答案及点评) 预处理器(Preprocessor) 1. 用预处理指令#define 声明一个常数,用以表明1年中有多少秒(忽略闰年问题) #define SEC ...

  9. 【HDU 1828】 Picture (矩阵周长并,线段树,扫描法)

    [题目] Picture Problem Description A number of rectangular posters, photographs and other pictures of ...

  10. 【BZOJ 2154】Crash的数字表格 (莫比乌斯+分块)

    2154: Crash的数字表格 Description 今天的数学课上,Crash小朋友学习了最小公倍数(Least Common Multiple).对于两个正整数a和b,LCM(a, b)表示能 ...