2011-10-08 42 views
2

在linux中是否有C函數調用,幫助我將$ PATH的目錄中的所有文件作爲列表或數組獲取?還是應該通過手動解析$ PATH變量來獲取它們?

+1

所有你需要做的就是分割'';'。 'strtok'應該是你所需要的。 –

+3

我想你的意思是':' –

回答

2
#include <stdlib.h> 
char *colon_delimited_path = getenv("PATH") 

返回一個:-PATH中的目錄限制列表。要轉換爲數組,可以使用strtok來拆分字符串。

+1

['strtok_r'](http://pubs.opengroup.org/onlinepubs/009604599/functions/strtok.html)會更安全,因爲'strtok'不是線程安全的。 –

相關問題