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. height为100%的问题

    问题描述 很多同学,对于设置div 的高度为100%时,有疑惑. 设置div 的高度为100%,意思是此 div 的高度 铺满父元素. 那么 怎么使 div 铺满浏览器屏幕? 代码 <!DOCT ...

  2. URL重写 UrlRewrite

    为什么要URL重写? 1.有利于SEO,带参数的URL权重较低: 2.地址看起来更正规,推广uid 伪静态:看起来像普通页面,而非动态生成的页面. 原理:在Global.asax的Applicatio ...

  3. 通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(一)

    项目组用air来开发手游, 但有些在原生应用里很容易实现的功能没有办法在air中直接调用,比如说震动,服务等等.但Adobe 提供了一种方法让air间接调用本地代码(java,object-c...) ...

  4. 【原创】Linux 增加系统调用

    Linux 增加系统调用大致步骤: 1.下载好内核文件,在内核源文件中添加好自己的调用函数. 2.编译内核 3.验证. 一.在内核源文件中增加自己的函数   首先将内核文件移至/usr/src/下并解 ...

  5. 身份证js验证

    <script type="text/javascript"> //--身份证号码验证-支持新的带x身份证 function isIdCardNo(num) { var ...

  6. Oracle数据库小知识,改数据库数据

    在一张表上面右键-->查询数据,会生成sql语句,表后面带有t,表示模糊查询, 后面跟上for update之后,执行语句-->小锁(编辑数据)就可以修改数据里面的数据了,修改之后--&g ...

  7. PL/SQL学习(五)异常处理

    原文参考:http://plsql-tutorial.com/ 组成: 1) 异常类型 2) 错误码 3) 错误信息   代码结构: DECLARE Declaration section BEGIN ...

  8. DOS命令中出现空格问题

    1.DOS命令中路径出现空格时如何处理? 在DOS命令中,如果路径中出现空格,可能为报错:如参数错误 如:  xcopy C:\ABC CD\txt.txt C:\ ,   由于路径中包含空格,执行后 ...

  9. KVO与KVC初步了解

    参考: http://magicalboy.com/kvc_and_kvo/ http://www.mamicode.com/info-detail-515516.html KVC,即是指 NSKey ...

  10. ubuntu16.04添加开机启动任务

    比如要把run-nexus.sh这个脚本制作成开机启动项. 系统工具->首选项->启动应用程序.添加该文件,即可.