• getcwd()、chdir()和 fchdir()函数的基本使用


    一、getcwd() 函数

    Linux 下的每一个进程都有自己的当前工作目录(current working directory),当前工作目录是该进程解析、搜索相对路径名的起点(不是以" / "斜杆开头的绝对路径)。

    譬如,代码中调用 open 函数打开文件时,传入的文件路径使用相对路径方式进行表示,那么该进程解析这个相对路径名时、会以进程的当前工作目
    录作为参考目录。一般情况下,运行一个进程时、其父进程的当前工作目录将被该进程所继承,成为该进程的当前工作目录。

    可通过 getcwd 函数来获取进程的当前工作目录,如下所示:

    #include 
    
    char *getcwd(char *buf, size_t size);
    
    • 1
    • 2
    • 3

    这是一个系统调用,使用该函数之前,需要包含头文件
    函数参数和返回值含义如下:
    buf:getcwd()将内含当前工作目录绝对路径的字符串存放在 buf 缓冲区中。
    size:缓冲区的大小,分配的缓冲区大小必须要大于字符串长度,否则调用将会失败。
    返回值:如果调用成功将返回指向 buf 的指针,失败将返回 NULL,并设置 errno。

    Tips:若传入的 buf 为 NULL,且 size 为 0,则 getcwd()内部会按需分配一个缓冲区,并将指向该缓冲区的指针作为函数的返回值,为了避免内存泄漏,调用者使用完之后必须调用 free()来释放这一缓冲区所占内存空间。

    示例代码
    读取进程的当前工作目录

    #include 
    #include 
    #include 
    #include 
    
    int main(void) 
    {
    	 char buf[100];
    	 char *ptr;
    	 memset(buf, 0x0, sizeof(buf));
    	 ptr = getcwd(buf, sizeof(buf));
    	 
    	 if (NULL == ptr) 
    	 {
    		 perror("getcwd error");
    		 exit(-1);
    	 }
    	 printf("Current working directory: %s\n", buf);
    	 exit(0);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    二、chdir()和 fchdir()函数

    系统调用 chdir()和 fchdir()可以用于更改进程的当前工作目录,函数原型如下所示:

    #include 
    
    int chdir(const char *path);
    int fchdir(int fd);
    
    • 1
    • 2
    • 3
    • 4

    首先,使用这两个函数之一需要包含头文件
    函数参数和返回值含义如下:
    path:将进程的当前工作目录更改为 path 参数指定的目录,可以是绝对路径、也可以是相对路径,指定的目录必须要存在,否则会报错。
    fd:将进程的当前工作目录更改为 fd 文件描述符所指定的目录(譬如使用 open 函数打开一个目录)。
    返回值:成功均返回 0;失败均返回-1,并设置 errno。

    此两函数的区别在于,指定目录的方式不同,chdir()是以路径的方式进行指定,而 fchdir()则是通过文件描述符,文件描述符可调用 open()打开相应的目录时获得。

    示例代码

    #include 
    #include 
    #include 
    #include 
    
    int main(void) 
    {
    	 char buf[100];
    	 char *ptr;
    	 int ret;
    	 
    	 /* 获取更改前的工作目录 */
    	 memset(buf, 0x0, sizeof(buf));
    	 ptr = getcwd(buf, sizeof(buf));
    	 
    	 if (NULL == ptr) 
    	 {
    		 perror("getcwd error");
    		 exit(-1);
    	 }
    	 printf("Before the change: %s\n", buf);
    	 
    	 /* 更改进程的当前工作目录 */
    	 ret = chdir("./new_dir");
    	 if (-1 == ret) 
    	 {
    		 perror("chdir error");
    		 exit(-1);
    	 }
    	 
    	 /* 获取更改后的工作目录 */
    	 memset(buf, 0x0, sizeof(buf));
    	 ptr = getcwd(buf, sizeof(buf));
    	 if (NULL == ptr) 
    	 {
    		 perror("getcwd error");
    		 exit(-1);
    	 }
    	 printf("After the change: %s\n", buf);
    	 exit(0);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    上述程序会在更改工作目录之前获取当前工作目录、并将其打印出来,之后调用 chdir 函数将进程的工作目录更改为当前目录下的 new_dir 目录,更改成功之后再将进程的当前工作目录获取并打印出来。

  • 相关阅读:
    HCIP-Datacom OSPF进阶(一) 最常用的动态路由协议
    电气元器件——变频器、自锁开关、电磁继电器、温度检测
    2023最新团购社群小程序源码 社区商城小程序源码开源版 带完整后台+部署教程
    Anniversary party(树形dp 基础题)
    淘宝商品详情API接口
    Wpf知识小结
    Kubernetes 笔记 / 入门 / 生产环境 / 用部署工具安装 Kubernetes / 用 kubeadm 启动集群 / 两种高可用拓扑
    解决java.lang.ArrayIndexOutOfBoundsException: Index x out of bounds for length y
    数据分析师听起来很高大上?了解这几点你再决定是否转型
    【Serverless】快速集成云函数HarmonyOS
  • 原文地址:https://blog.csdn.net/Dustinthewine/article/details/126694615