阿里云建站和華為云建站哪個好互聯(lián)網(wǎng)營銷師培訓(xùn)教材
前言
同樣是一個 很常用的 glibc 庫函數(shù)?
不管是 用戶業(yè)務(wù)代碼 還是 很多類庫的代碼, 基本上都會用到 獲取當(dāng)前路徑??
不過 我們這里是從 具體的實(shí)現(xiàn) 來看一下?
測試用例
就是簡單的使用了一下 getcwd??
root@ubuntu:~/Desktop/linux/HelloWorld# cat Test04Getcwd.c
#include "stdio.h"int main(int argc, char** argv) {int x = 2;
int y = 3;
int z = x + y;char* p1 = (char*) malloc(40);
printf("p1 : 0x%x\n", p1);getcwd(p1, 100);
int p1Len = strlen(p1); printf(" p1 = %s, p1Len = %d\n ", p1, p1Len);}
getcwd 的實(shí)現(xiàn)
首先確認(rèn)一下 斷點(diǎn)的位置, 位于 main 中的 getcwd 的函數(shù)調(diào)用?
getcwd 的實(shí)現(xiàn)如下?
默認(rèn)的處理是直接基于 getcwd 的系統(tǒng)調(diào)用?
如果名稱超長, 則基于 generic_getcwd 來獲取當(dāng)前路徑?
generic_getcwd 是基于 proc 文件系統(tǒng), 獲取 proc 文件系統(tǒng)下的 當(dāng)前進(jìn)程存儲的 cwd?
root@ubuntu:~/Desktop/linux/HelloWorld# ll /proc/5753/cwd
lrwxrwxrwx 1 root root 0 Nov 25 23:39 /proc/5753/cwd -> /root/Desktop/linux/HelloWorld/
getcwd 的系統(tǒng)調(diào)用
getcwd 系統(tǒng)調(diào)用如下, 這里的當(dāng)前路徑為 "/jerry/dir"?
其中硬盤 "/dev/sda1" 掛載到了 "/jerry" 上面
這里的處理是 當(dāng)前路徑的 dentry 向上開始遍歷, 直到碰到 根節(jié)點(diǎn)?
然后 將這一系列的路徑輸出到 buffer 中?
完