解析$ PATH变量并将目录名保存到一个字符串数组中 [英] Parse $PATH variable and save the directory names into an array of strings
问题描述
我想解析Linux的$ PATH变量,然后将以':'分隔的目录名保存到一个字符串数组中。
I知道这是一个简单的任务,但我卡住了,任何帮助都会很好。
到目前为止,我的代码是这样的,但有些不对。
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; //这实际上是我想要为每条路径做的所有事情
i ++;
path_string = strtok(NULL,:);
<2>代码,几乎总结了评论:
- 您创建一个公共缓冲区(由
getenv $ c返回
- 你不知道缓冲区中有多少变量,所以你根本不会分配数组数组!$ /
/ ul>
让我使用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:); //如果PATH为NULL,则不太可能
const char * the_dot =。;
int j;
int len = strlen(path_var);
int nb_colons = 0;
char pathsep =':';
int current_colon = 0;
//首先计算我们有多少条路径,而拆分就像strtok那样,对于(j = 0; j< len; j ++)
{
if(path_var [j] == pathsep)
{
nb_colons ++;
path_var [j] ='\0';
//分配字符串数组
array = malloc((nb_colons + 1)* sizeof(* array));
array [0] = path_var; //第一条路径
//其余路径
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')
{
//特殊情况:如果路径为空,则添加点
array [current_colon] = the_dot; (j = 0; j{
$ b printf(Path%d:<%s> \\\
,j,array [j]);
}
return(0);
$ / code>操作细节:
- 制作env字符串的副本以避免屠杀它$
- 计数冒号(为了使它与windows一起工作,只需替换为<$ c
-
- 根据冒号数量+ 1分配数组(1个符号比分隔符数量更多!)$ / $> b $ b
- 第二遍以再次遍历字符串,并将其填充到标记化字符串的某些部分(不需要再次分配,原始字符串已分配)。
- 特殊情况:空路径:替换为
。
。可能会显示警告,告诉用户这是不安全的。 - 打印结果
I want to parse the $PATH variable of Linux, and then save the directory names that are getting separated with ':' into an array of strings.
I know it's a simple task but I am stuck and any help would be nice.
My code so far is something like this but something ain't right.
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, ":"); }
解决方案2 main things wrong with your code, pretty much summarized by the comments:
- you strtok a public buffer (returned by
getenv
) - you don't know how many variables will be in the buffer so you don't allocate the array of arrays at all!
Let me propose a working implementation not using strtok, and thus allowing to detect empty path (and replace it by
.
as Jonathan hinted). Compiles without any warnings usinggcc -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); }
Details of the operations:
- make a copy of the env string to avoid butchering it
- count the colons (to make it work with windows, just replace with
;
) and tokenize - allocate the array according to number of colons + 1 (1 more token than number of separators!)
- second pass to go through the string again and fill it with parts of the tokenized string (no need to allocate again, the original string is already allocated)
- special case: empty path: replace by
.
. Could display a warning to tell the user that this is not safe. - print the result
这篇关于解析$ PATH变量并将目录名保存到一个字符串数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 你不知道缓冲区中有多少变量,所以你根本不会分配数组数组!$ /