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. c++动态绑定与静态绑定

    C++为了支持多态性,采用了动态绑定和静态绑定 相关概念: 对象的静态类型:对象在声明时采用的类型,编译时确定 对象的动态类型:目前所指对象的类型,在运行时确定 class B { } class C ...

  2. 九度OJ 1082 代理服务器 -- 贪心算法

    题目地址:http://ac.jobdu.com/problem.php?pid=1082 题目描述: 使用代理服务器能够在一定程度上隐藏客户端信息,从而保护用户在互联网上的隐私.我们知道n个代理服务 ...

  3. windows phone 扫描二维码

    在网上找了找扫描二维码的例子,利用ZXing库实现(下载),提供的Silverlight版本的下载,在网上搜了一下已经有wp的dll可用了,不过网上实现的条码扫描的例子还都是用的Silverlight ...

  4. Ubuntu下VIM(GVIM)环境配置

    GVIM安装( Ubuntu自带VIM ): 通过应用商店安装或者通过以下安装. sudo apt-get install vim-gnome GVIM配置: 在 家目录 ( ~/ ) 下建立 .vi ...

  5. 字符串长度计算、截取、url参数获取、计算百分比、时间戳格式化

    1.中英混合文字字符截取 //中文长度截取计算,可取中英混合,个数向上取整,精确度1个英文字符误差,一个英文算一个字符,一个汉字算一个字符. //sub("中文zlsd",1) - ...

  6. node.js操作mongoDB数据库

    链接数据库: var mongo=require("mongodb"); var host="localhost"; var port=mongo.Connec ...

  7. 在ECSHOP首页今日特价(促销商品)增加倒计时效果

    看到不少朋友在找首页特价商品倒计时的修改方法,写了这篇文章希望能帮到有此需要的朋友们 1.首先修改程序部分 打开includes/lib_goods.php 找到get_promote_goods() ...

  8. Android入门教程之我见

    真正的从安卓入门学习到实际工作也差不多一年时间了,也做了几个项目.在这期间经历了一开始学习Android的基本知识后仍旧无从下手,不知道如何开始开发一个app,到现在也开始学会注意Android架构的 ...

  9. Android 连接tomcat模拟登陆账号

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  10. 【BZOJ 3529】 [Sdoi2014]数表 (莫比乌斯+分块+离线+树状数组)

    3529: [Sdoi2014]数表 Description 有一张N×m的数表,其第i行第j列(1 < =i < =礼,1 < =j < =m)的数值为能同时整除i和j的所有 ...