[a] stat / lstat / fstat

#include <sys/stat.h>
int stat(const char *restrict pathname, struct stat *restrict buf)
int lstat(const char *restrict pathname, struct stat *restrict buf)
int fstat(int fd, struct stat *buf)
struct stat {
mode_t st_mode;
ino_t st_ino;
dev_t st_dev;
dev_t st_rdev;
nlink_t st_nlink;
uid_t st_uid;
gid_t st_gid;
off_t st_size;
struct timespec st_atime;
struct timespec st_mtime;
struct timesepc st_ctime;
blksize_t st_blksize;
blkcnt_t st_blocks;
}
/* st_mode 二进制位对应关系, 显示的数字为 8 进制*/
#define S_IFMT 0170000 /* type of file mask */
#define S_IFIFO 0010000 /* named pipe (fifo) */
#define S_IFCHR 0020000 /* character special */
#define S_IFDIR 0040000 /* directory */
#define S_IFBLK 0060000 /* block special */
#define S_IFREG 0100000 /* regular */
#define S_IFLNK 0120000 /* symbolic link */
#define S_IFSOCK 0140000 /* socket */
#define S_IFWHT 0160000 /* whiteout */
#define S_ISUID 0004000 /* set user id on execution */
#define S_ISGID 0002000 /* set group id on execution */
#define S_ISVTX 0001000 /* save swapped text even after use */
#define S_IRWXU 0000700 /* RWX mask for owner */
#define S_IRUSR 0000400 /* read permission, owner */
#define S_IWUSR 0000200 /* write permission, owner */
#define S_IXUSR 0000100 /* execute/search permission, owner */
#define S_IRWXG 0000070 /* RWX mask for group */
#define S_IRGRP 0000040 /* read permission, group */
#define S_IWGRP 0000020 /* write permission, group */
#define S_IXGRP 0000010 /* execute/search permission, group */
#define S_IRWXO 0000007 /* RWX mask for other */
#define S_IROTH 0000004 /* read permission, other */
#define S_IWOTH 0000002 /* write permission, other */
#define S_IXOTH 0000001 /* execute/search permission, other */
struct timespec {
time_t tv_sec;
long tv_nsec;
}
/* 判断文件类型的預定义宏, 类型匹配返回非 0, 否则返回 0 */
S_ISREG(buf.st_mode)
S_ISDIR(buf.st_mode)
S_ISCHR(buf.st_mode)
S_ISBLK(buf.st_mode)
S_ISFIFO(buf.st_mode)
S_ISLNK(buf.st_mode)
S_ISSOCK(buf.st_mode)
  • 成功返回 0, 出錯返回 -1
  • lstat 不追踪软链接目标文件, 可获得软链接本身的状态信息, 软链接的文件大小等于其所指向的目标文件名称的字符数量
  • 文件的状态信息将写入事先定义的 stat 結构体中
  • 文件类型的判断, 由于每种文件类型并不独占相应的二进制位, 故需使用預定义的 S_ISREG ... 等宏, 或者取 buf.st_mode & S_IFMT 的結果与对应的文件类型(S_IFDIR ...)进行相等性测试
  • 文件的每类权限均独占相应的二进制位, 故可直接使用 buf.st_mode 与各权限宏逐一按位 '&'

[b] umask

#include <sys/stat.h>
mode_t umask(mode_t cmask)
  • 总是成功, 返回之前的屏蔽字, 没有出錯返回值
  • 仅作用于当前进程环境, 不更改 shell 的 umask 值

[c] chmod / lchmod / fchmod

#include <sys/stat.h>
int chmod(const char *path, mode_t mode)
int lchmod(const char *path, mode_t mode)
int fchmod(int fd, mode_t mode)
  • 成功返回 0, 出錯返回 -1
  • lchmod 不追踪软链接
  • mode 可使用八进制数字表示, 如 0644 等

[d] chown / lchown / fchown

#include <unistd.h>
int chown(const char *path, uid_t owner, gid_t group)
int lchown(const char *path, uid_t owner, gid_t group)
int fchown(int fd, uid_t owner, gid_t group)
  • 成功返回 0, 出錯返回 -1

[e] truncate / ftruncate

#include <unistd.h>
int truncate(const char *path, off_t length)
int ftruncate(int fd, off_t length)
  • 成功返回 0, 出錯返回 -1
  • 将指定文件截断为 length 字节, 其值若大于原文件大小, 将在文件末尾创建空洞

[f] link / unlink / mkdir / rmdir / remove

#include <unistd.h>
int link(const char *path, const char *path2)
int unlink(const char *path)
int mkdir(const char *path, mode_t mode)
int rmdir(const char *path)
#include <stdio.h>
int remove(const char *path)
  • 成功返回 0, 出錯返回 -1
  • link 创建硬链接, unlink 删除硬链接, mkdir 创建目录, rmdir 删除空目录
  • 标准库函数 remove 可对文件或目录执行 "删除"

[g] rename

#include <stdio.h>
int rename(const char *path, const char *path2)
  • 成功返回 0, 出錯返回 -1

[h] symlink

#include <unistd.h>
int symlink(const char *path, const char *path2)
  • 成功返回 0, 出錯返回 -1

[i] readlink

#include <unistd.h>
ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize)
  • 成功返回读取到的字节数, 出錯返回 -1
  • 用于读取软链接指向的目标文件或目录名称

[j] futimens

#include <sys/stat.h>
int futimens(int fd, const struct timespec times[])
  • 成功返回 0, 出錯返回 -1
  • 用于更改文件或目录的 atime 或 mtime, 不能更改 ctime, 結构体数組的第一个元素指 atime
  • 若 times 参数指定为 NULL, 则更改为当前时间
  • 任意数組元素的 tv_nsec 字段为 UTIME_NOW, 则对应时间设置为当前时间; 若为 UTIME_OMIT, 则对应时间保持不变

[k] opendir / fdopendir / readdir / rewinddir / closedir / telldir / seekdir

#include <dirent.h>
DIR *opendir(const char *path)
DIR *fdopendir(int fd)
struct dirent *readdir(DIR *dp)
void rewinddir(DIR *dp)
int closedir(DIR *dp)
long telldir(DIR *dp)
void seekdir(DIR *dp, long loc) 
struct dirent {
ino_t d_ino;
char d_name[];

[l] chdir / fchdir / getcwd

#include <unistd.h>
int chdir(const char *path)
int fchdir(int fd)
char *getcwd(char *buf, size_t size) 

...

[04]APUE:文件与目录的更多相关文章

  1. [APUE]文件和目录(中)

    一.link.unlink.remove和rename 一个文件可以有多个目录项指向其i节点.使用link函数可以创建一个指向现存文件连接 #include <unistd.h> int ...

  2. [APUE]文件和目录(上)

    一.文件权限 1. 各种ID 我在读这一章时遇到了各种ID,根据名字完全不清楚什么意思,幸好看到了这篇文章,http://blog.csdn.net/ccjjnn19890720/article/de ...

  3. APUE 文件和目录

    文件和目录 Unix 所有的文件都对应一个 struct stat,包含了一个文件所有的信息. #include <sys/stat.h> struct stat { mode_t st_ ...

  4. [APUE]文件和目录(下)

    一.mkdir和rmdir函数 #include <sys/types.h> #include <sys/stat.h> int mkdir(const char *pathn ...

  5. APUE ☞ 文件和目录

    粘着位(Sticky Bit) S_ISVTX位被称为粘着位.如果一个可执行程序文件的这一位被设置了,程序第一次运行完之后,程序的正文部分的一个副本仍被保存在交换区(程序的正文部分是机器指令).这使得 ...

  6. ubuntu 16.04查询文件安装目录

    dpkg -L filename dpkg -l | grep filename whereis filename find / -name filename

  7. (三) 一起学 Unix 环境高级编程 (APUE) 之 文件和目录

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  8. apue chapter 4 文件和目录

    1.文件信息结构体 struct stat{ mode_t st_mode; //file type and permissions ino_t st_ino; //i-node number (se ...

  9. 64位ubuntu14.04配置adb后提示没有那个文件或目录

    1.配置完adb环境变量后在终端输入adb: ameyume@ameyume-HP-450-Notebook-PC:~$ adb /home/ameyume/adt-bundle-linux-x86_ ...

随机推荐

  1. curd 里url传输汉字验证错误问题解决方法

    在url汉字转换的部分用base64_encode转化 base64_encode 将字符串以 BASE64 编码. 语法: string base64_encode(string data); 返回 ...

  2. .Net“/”应用程序中的服务器错误 超过了最大请求长度 错误解决办法

    错误如下: 错误提示: 说明: 执行当前 Web 请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详细信息: System.Web.HttpE ...

  3. Ubuntu 16.10下的 jdk 1.8.0_111

    下载好对应版本的jdk copy到此目录下,并解压: 呼出终端,输入指令: gedit ~/.bashrc 会出现文本编辑界 export JAVA_HOME=/usr/lib/jvm/jdk1.8. ...

  4. iOS(视图控制器转场)

    转场需要提供转场代理,不使用默认的代理则需要自己实现代理方式,有UINavigationController.UITabBarController.UIViewController三种代理,实现以下三 ...

  5. IE6 7下常见CSS兼容性处理

    以下是一些比较常见的IE6 7下的兼容性问题. 在当下这个时代,其实我们几乎可以不用再去针对IE6做兼容性的处理,除非你的公司还是诡异的要求你兼容到IE6.但是了解一些常见的兼容性问题还是可以帮助我们 ...

  6. git 记住密码

    http://yourname:password@git.oschina.net/name/project.git

  7. python初学day01

    1.执行Python脚本时打印的字符有颜色 1. print "\033[32;1mhello\033[0m" #打印绿色 2. print "\033[31;1mhel ...

  8. Python SQLAlchemy --3

    本文為 Python SQLAlchemy ORM 一系列教學文: 刪除 學會如何查詢之後,就能夠進行後續的刪除.更新等操作. 同樣地,以幾個範例做為學習的捷徑. 123456789 user_1 = ...

  9. Android学习---如何创建数据库,SQLite(onCreate,onUpgrade方法)和SQLiteStudio的使用

    一.android中使用什么数据库? SQLite是遵守ACID的关系数据库管理系统,它包含在一个相对小的C程式庫中.它是D.RichardHipp建立的公有领域项目.SQLite 是一个软件库,实现 ...

  10. bootstrap的html模版

    <!DOCTYPE html> <html> <head> <title>Bootstrap 模板</title> <meta nam ...