背景

在Unix环境编程中,系统提供了很多以at结尾的函数,如openat、fstatat等,而这类函数通常有一个特点,就是形参列表中多了int dirfd

例如:

int open(const char *pathname, int flags, mode_t mode);
int openat(int dirfd, const char *pathname, int flags, mode_t mode); int mkfifo(const char *pathname, mode_t mode);
int mkfifoat(int dirfd, const char *pathname, mode_t mode); ...

意义

dirfd参数的意义:

1)path参数指定为绝对路径名时,fd会被忽略,openat函数就相当于open函数

2)path参数指定为相对路径名时,fd参数指出了相对路径名在文件系统的开始地址。fd参数是通过打开相对路径名所在的目录来获取。所以此时的path是相对于文件描述符dirfd所引用的目录。

3)path参数指定了相对路径名,并且fd参数是特殊值AT_FDCWD。在这种情况下,路径名在当前工作目录中获取,openat函数在操作上与open函数类似。

实际上,dirfd参数 实现了 以 文件描述符+相对路径 作为定位的 一种功能(而不是 当前工作路径+相对路径 作为定位)。我们可以参考下文的文档描述来体会这个功能:

If the pathname given in pathname is relative, then it is interpreted relative to the directory referred to by the file descriptor dirfd (rather than relative to the current working directory of the calling process, as is done by mkfifo() for a relative pathname).

如果pathname中给定的路径名是相对的,那么它将被解释为相对于文件描述符dirfd所引用的目录(而不是相对于调用进程的当前工作目录)。

用法

用法有两种,分别说明如下:

1、直接用open打开目录,用返回值作为openat的第一个参数的值,代码如下:

#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h> void creat_at(char *dir_path, char *relative_path)
{
int dir_fd;
int fd;
int flags;
mode_t mode; dir_fd = open(dir_path, O_RDONLY);
if (dir_fd < 0)
{
perror("open");
exit(EXIT_FAILURE);
} flags = O_CREAT | O_TRUNC | O_RDWR | O_DIRECTORY;
mode = 0640;
fd = openat(dir_fd, relative_path, flags, mode);
if (fd < 0)
{
perror("openat");
exit(EXIT_FAILURE);
} write(fd, "HELLO", 5); close(fd);
close(dir_fd);
} int main()
{
creat_at("./open", "log.txt");
return 0;
}

**2、借用dirfd,将DIR*转换成int类型的文件描述符 **

#include <sys/types.h>
#include <dirent.h> int dirfd(DIR *dirp);

完整代码如下:

#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h> int main()
{
DIR *dir;
int dirfd2;
int fd;
int n; dir = opendir("./open/chl");
if(NULL == dir)
{
perror("open dir error");
return -1;
}
dirfd2 = dirfd(dir);
if(-1 == dirfd2)
{
perror("dirfd error");
return -1;
} fd = openat(dirfd2,"output.log",O_CREAT|O_RDWR|O_TRUNC);
if(-1 == fd)
{
perror("opeat error");
return -1;
}
n = write(fd,"Hello world!\n",15); close(fd);
closedir(dir); return 0;
}

Linux 环境编程:dirfd参数 有关解析的更多相关文章

  1. Linux环境编程相关的文章

    Linux环境编程相关的文章 好几年没有接触Linux环境下编程了,好多东西都有点生疏了.趁着现在有空打算把相关的一些技能重拾一下,顺手写一些相关的文章加深印象. 因为不是写书,也受到许多外部因素限制 ...

  2. Linux移植之tag参数列表解析过程分析

    在Linux移植之内核启动过程start_kernel函数简析中已经指出了start_kernel函数的调用层次,这篇主要是对具体的tag参数列表进行解析. 1.内存参数ATAG_MEM参数解析 2. ...

  3. Linux环境编程进程间通信机制理解

    一.Linux系统调用主要函数 二.创建进程 1.创建子进程系统调用fork() 2.验证fork()创建子进程效果 3.系统调用fork()与挂起系统调用wait() 三.模拟进程管道通信 四.pi ...

  4. Linux环境编程--waitpid与fork与execlp

    waitpid waitpid(等待子进程中断或结束) 表头文件 #include<sys/types.h> #include<sys/wait.h> 定义函数 pid_t w ...

  5. Linux环境编程导引

    计算机系统硬件组成 总线 贯穿整个系统的一组电子管道称为总线, 分为: 片内总线 系统总线 数据总线DB 地址总线AB 控制总线CB 外部总线 I/O设备 I/O设备是系统与外界联系的通道 键盘鼠标是 ...

  6. Linux网络编程六、报文解析(1)

    一.pcap文件解析 依赖的库:libpcap,头文件:pcap/pcap.h 获取pcap文件:tcpdump,-i:指定监听接口,默认配置好的最小的号码的接口.-w:指定存入文件,将原始报文存入指 ...

  7. Linux环境下vsftpd参数配置

    很久之前就用过vsftpd,但总是忘了参数该如何配置,今天特地有搜索了一遍,把配置方法整理如下: (1)检查是否已安装vsftpd #rpm -qa | grep vsftpd vsftpd--.el ...

  8. 笔记整理:计算CPU使用率 ----linux 环境编程 从应用到内核

    linux 提供time命令统计进程在用户态和内核态消耗的CPU时间: [root@localhost ~]# time sleep real 0m2.001s user 0m0.001s sys 0 ...

  9. Linux下ping命令参数详细解析

    -a Audible ping. #Audible ping. -A Adaptive ping. Interpacket interval adapts to round-trip time, so ...

随机推荐

  1. Spring Environment对象获取属性

    String[] activeProfiles = env.getActiveProfiles();//获取当前是启用哪一个个配置文件 System.out.println(Arrays.toStri ...

  2. 1-浅谈 python变量

    浅谈 python变量 python变量概念 程序执行的过程中,很多数据都在变化的过程,我们需要一种机制把这种变化体现出来,变量是我们记录这种变化的方式. python以及其它各种语言的变量 ,其作用 ...

  3. Python练习题 038:Project Euler 010:两百万以内所有素数之和

    本题来自 Project Euler 第10题:https://projecteuler.net/problem=10 # Project Euler: Problem 10: Summation o ...

  4. vs工程生成dll文件及其调用方法

    转载:https://blog.csdn.net/weixin_44536482/article/details/91519413 vs工程生成dll文件及其调用方法                  ...

  5. matlab中get查询图形对象属性

    来源:https://ww2.mathworks.cn/help/matlab/ref/get.html?searchHighlight=get&s_tid=doc_srchtitle get ...

  6. P5911 [POI2004]PRZ (状态压缩dp+枚举子集)

    题目背景 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 题目描述 桥已经很旧了, 所以它不能承受太重的东西.任何时候队伍在桥上的人都不能超过一定的限制. 所以这只队伍过桥时 ...

  7. ElasticSearch 索引 VS MySQL 索引

    前言 这段时间在维护产品的搜索功能,每次在管理台看到 elasticsearch 这么高效的查询效率我都很好奇他是如何做到的. 这甚至比在我本地使用 MySQL 通过主键的查询速度还快. 为此我搜索了 ...

  8. 【Excel技巧】用IF函数进行等级评定

    如果下面给出一份"2月份语文成绩考核表",那么如何对成绩进行等级评定呢. 等级评定规则: 总分(100分) A级(91-100) B级(81-90) C级(71-80) D级(70 ...

  9. JavaScript实时显示当前时间

    1.HTML部分 <div id="div1">显示当前时间!</div> 2.css部分 #div1 { width: 700px; height: 50 ...

  10. Apache Hudi与Apache Flink集成

    感谢王祥虎@wangxianghu 投稿 Apache Hudi是由Uber开发并开源的数据湖框架,它于2019年1月进入Apache孵化器孵化,次年5月份顺利毕业晋升为Apache顶级项目.是当前最 ...