[a] stat / lstat / fstat

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

[b] umask

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

[c] chmod / lchmod / fchmod

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

[d] chown / lchown / fchown

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

[e] truncate / ftruncate

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

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

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

[g] rename

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

[h] symlink

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

[i] readlink

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

[j] futimens

  1. #include <sys/stat.h>
  2. 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

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

[l] chdir / fchdir / getcwd

  1. #include <unistd.h>
  2. int chdir(const char *path)
  3. 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. 《SSM框架搭建》二.mybatis3,spring4整合

    感谢学习文章来自http://www.cnblogs.com/xdp-gacl/p/4271627.html,spring3修改为spring4.还有提示驱动过期的问题,是由于使用了mysql-con ...

  2. mybatis单个插入和批量插入的简单比较

    在J2EE项目中,mybatis作为主流持久层框架,许多知识值得我们去钻研学习,今天,记录一下数据插入性能(单个插入和批量插入). 一,测试对象 public class Test { private ...

  3. c#中实现多个接口出现同名同参的方法

    IDictionary接口类同时继承了 IEnumerable<KeyValuePair<TKey, TValue>> 和 IEnumerable这两个接口 public in ...

  4. 服务器控件中使用<%#...>, JS和html控件中使用<%=...>

    //在服务器控件的属性中,需要用<%#...>来绑定其他控件的ID, 并且要在页面初始方法中,执行Page.DataBind(); <asp:ImageButton ID=" ...

  5. Graded Browser Support

    ( The YUI Target Environments Matrix is here) About the Browser Test Baseline and Operating Systems ...

  6. select标签非空验证,第一个option value=""即可

    select标签非空验证,第一个option value=""即可,否则不能验证

  7. <textarea>没有内容时,按回车键,设置光标不换行

    $("textarea").val($("textarea").val().replace(/\n/g,""));

  8. 仿东软OA协同办公服务管理系统

    兼容IE6,7,8以上.GooleChrome.360及遨游等浏览器.系统特色:1.系统经过抗压测试.2.语音提示功能.3.支持office2007在线编辑.4.强大的图形化工作流程设计及文档编辑留痕 ...

  9. 从零构建一个简单的 Python Web框架

    为什么你想要自己构建一个 web 框架呢?我想,原因有以下几点: 你有一个新奇的想法,觉得将会取代其他的框架 你想要获得一些名气 你遇到的问题很独特,以至于现有的框架不太合适 你对 web 框架是如何 ...

  10. C# DataTable 和List之间相互转换的方法

    介绍:List/IEnumerable转换到DataTable/DataView,以及DataTable转换到List 正文: 一.List<T>/IEnumerable转换到DataTa ...