2011-10-08 69 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'不是线程安全的。 –

相关问题