我想解析Linux的$ PATH变量,然后保存用'分隔的目录名:'成为一个字符串数组。
我知道这是一项简单的任务,但我被困住了,任何帮助都会很好。
到目前为止,我的代码是这样的,但不是对的。
char **array;
char *path_string;
char *path_var = getenv("PATH");
int size_of_path_var = strlen(path_var);
path_string = strtok(path_var, ":");
while (path_string != NULL) {
ss = strlen(path_string)
array[i] = (char *)malloc(ss + 1);
array[i] = path_string; //this is actually all i want to do for every path
i++;
path_string = strtok(NULL, ":");
}
答案 0 :(得分:6)
您的代码出现了两个主要问题,几乎总结为评论:
getenv
返回)让我使用strtok提出一个工作实现 not ,从而允许检测空路径(并将其替换为.
,如Jonathan暗示的那样)。使用gcc -Wall -Wwrite-strings
编译时没有任何警告:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
const char **array;
const char *orig_path_var = getenv("PATH");
char *path_var = strdup(orig_path_var ? orig_path_var : ""); // just in case PATH is NULL, very unlikely
const char *the_dot = ".";
int j;
int len=strlen(path_var);
int nb_colons=0;
char pathsep = ':';
int current_colon = 0;
// first count how many paths we have, and "split" almost like strtok would do
for (j=0;j<len;j++)
{
if (path_var[j]==pathsep)
{
nb_colons++;
path_var[j] = '\0';
}
}
// allocate the array of strings
array=malloc((nb_colons+1) * sizeof(*array));
array[0] = path_var; // first path
// rest of paths
for (j=0;j<len;j++)
{
if (path_var[j]=='\0')
{
current_colon++;
array[current_colon] = path_var+j+1;
if (array[current_colon][0]=='\0')
{
// special case: add dot if path is empty
array[current_colon] = the_dot;
}
}
}
for (j=0;j<nb_colons+1;j++)
{
printf("Path %d: <%s>\n",j,array[j]);
}
return(0);
}
行动详情:
;
)并进行标记化.
替换。可以显示警告,告诉用户这不安全。答案 1 :(得分:0)
我意识到这已经很老了,但是我必须做一次家庭作业,并且我想我将分享我的解决方案,因为上述内容有点复杂并且不使用strtok。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void printStrings(const char* args[]);
int main(int argc, char const *argv[]) {
char* path = getenv("PATH"); //gets the path
const char* pathParts[100]; //arr to hold pointers to parts of path (whatever size)
char* token; //holds next part of path
token = strtok(path, ":");
pathParts[0] = token; // place pointer to next part of path in arr
int i = 1;
while(token != NULL) {
token = strtok(NULL, ":");
pathParts[i] = token; // place pointer to next part of path in arr
i++;
}
printStrings(pathParts); // print parts of path 1/line
return 0;
}
void printStrings(const char* args[]) {
int i = 0;
while(args[i] != 0) {
printf("%s\n", args[i]);
i++;
}
}