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. Dynamic Programming: From novice to advanced

    作者:Dumitru 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg An impo ...

  2. Github进行项目管理的常用命令总结

    最近项目中使用github来管理代码,将git的常用命令整理了一下,方便以后查询 查看git的本地配置git config --list 克隆到本地git clone (网页上面的那个http地址 g ...

  3. LAMP 环境 快速安装

    (一)安装Apache 1.下载安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 yum install zlib-devel -y wget http://m ...

  4. C#中的Collections命名空间

    System.Collections命名空间包含可使用的集合类和相关的接口. 该命名空间下的.NET非泛型集合类如下所示: — System.Collections.ArrayList:数组集合类,使 ...

  5. Fedora 21 安装QQ国际版

    首先安装依赖包 sudo yum install freetype.i686 libpng.i686 libgcc.i686 libXau.i686 点击下载wine-2012qq国际版 unzip ...

  6. 【原创】Linux 内核模块编程

    sudo gedit hello.c #include <linux/module.h> #include <linux/kernel.h> #include <linu ...

  7. jQuery选择器(一)

    1.#id根据给定的ID匹配一个元素. <div id="notMe"><p>id="notMe"</p></div& ...

  8. demo_06Canvas

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. HTML css面试题

    1.对WEB标准以及W3C的理解与认识 标签闭合.标签小写.不乱嵌套.提高搜索机器人搜索几率.使用外链css和js脚本.结构行为表现的分离.文件下载与页面速度更快.内容能被更多的用户所访问.内容能被更 ...

  10. php操作memcache的用法、详解和方法介绍

    1.简介 memcache模块是一个高效的守护进程,提供用于内存缓存的过程式程序和面向对象的方便的接口,特别是对于设计动态web程序时减少对数据库的访问. memcache也提供用于通信对话(sess ...