dar*_*nir 0 c linux environment-variables
我想测试在运行程序的系统上是否安装了GNUPlot。
为此,我认为我将通过stat()调用在用户的安装位置中测试gnuplot可执行文件的存在。
但是,我不知道如何在C中读取$ PATH环境变量,因此我可以测试这些位置是否存在文件。
小智 5
char *paths = getenv("PATH");
Run Code Online (Sandbox Code Playgroud)
要遍历列分隔的路径列表的各个部分,请使用strchr():
char *dup = strdup(getenv("PATH"));
char *s = dup;
char *p = NULL;
do {
p = strchr(s, ':');
if (p != NULL) {
p[0] = 0;
}
printf("Path in $PATH: %s\n", s);
s = p + 1;
} while (p != NULL);
free(dup);
Run Code Online (Sandbox Code Playgroud)