[04]APUE:文件与目录
[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:文件与目录的更多相关文章
- [APUE]文件和目录(中)
一.link.unlink.remove和rename 一个文件可以有多个目录项指向其i节点.使用link函数可以创建一个指向现存文件连接 #include <unistd.h> int ...
- [APUE]文件和目录(上)
一.文件权限 1. 各种ID 我在读这一章时遇到了各种ID,根据名字完全不清楚什么意思,幸好看到了这篇文章,http://blog.csdn.net/ccjjnn19890720/article/de ...
- APUE 文件和目录
文件和目录 Unix 所有的文件都对应一个 struct stat,包含了一个文件所有的信息. #include <sys/stat.h> struct stat { mode_t st_ ...
- [APUE]文件和目录(下)
一.mkdir和rmdir函数 #include <sys/types.h> #include <sys/stat.h> int mkdir(const char *pathn ...
- APUE ☞ 文件和目录
粘着位(Sticky Bit) S_ISVTX位被称为粘着位.如果一个可执行程序文件的这一位被设置了,程序第一次运行完之后,程序的正文部分的一个副本仍被保存在交换区(程序的正文部分是机器指令).这使得 ...
- ubuntu 16.04查询文件安装目录
dpkg -L filename dpkg -l | grep filename whereis filename find / -name filename
- (三) 一起学 Unix 环境高级编程 (APUE) 之 文件和目录
. . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...
- apue chapter 4 文件和目录
1.文件信息结构体 struct stat{ mode_t st_mode; //file type and permissions ino_t st_ino; //i-node number (se ...
- 64位ubuntu14.04配置adb后提示没有那个文件或目录
1.配置完adb环境变量后在终端输入adb: ameyume@ameyume-HP-450-Notebook-PC:~$ adb /home/ameyume/adt-bundle-linux-x86_ ...
随机推荐
- 《SSM框架搭建》二.mybatis3,spring4整合
感谢学习文章来自http://www.cnblogs.com/xdp-gacl/p/4271627.html,spring3修改为spring4.还有提示驱动过期的问题,是由于使用了mysql-con ...
- mybatis单个插入和批量插入的简单比较
在J2EE项目中,mybatis作为主流持久层框架,许多知识值得我们去钻研学习,今天,记录一下数据插入性能(单个插入和批量插入). 一,测试对象 public class Test { private ...
- c#中实现多个接口出现同名同参的方法
IDictionary接口类同时继承了 IEnumerable<KeyValuePair<TKey, TValue>> 和 IEnumerable这两个接口 public in ...
- 服务器控件中使用<%#...>, JS和html控件中使用<%=...>
//在服务器控件的属性中,需要用<%#...>来绑定其他控件的ID, 并且要在页面初始方法中,执行Page.DataBind(); <asp:ImageButton ID=" ...
- Graded Browser Support
( The YUI Target Environments Matrix is here) About the Browser Test Baseline and Operating Systems ...
- select标签非空验证,第一个option value=""即可
select标签非空验证,第一个option value=""即可,否则不能验证
- <textarea>没有内容时,按回车键,设置光标不换行
$("textarea").val($("textarea").val().replace(/\n/g,""));
- 仿东软OA协同办公服务管理系统
兼容IE6,7,8以上.GooleChrome.360及遨游等浏览器.系统特色:1.系统经过抗压测试.2.语音提示功能.3.支持office2007在线编辑.4.强大的图形化工作流程设计及文档编辑留痕 ...
- 从零构建一个简单的 Python Web框架
为什么你想要自己构建一个 web 框架呢?我想,原因有以下几点: 你有一个新奇的想法,觉得将会取代其他的框架 你想要获得一些名气 你遇到的问题很独特,以至于现有的框架不太合适 你对 web 框架是如何 ...
- C# DataTable 和List之间相互转换的方法
介绍:List/IEnumerable转换到DataTable/DataView,以及DataTable转换到List 正文: 一.List<T>/IEnumerable转换到DataTa ...