Quantcast
Viewing all articles
Browse latest Browse all 22122

Parse $PATH variable and save the directory names into an array of strings

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, ":");
}

Viewing all articles
Browse latest Browse all 22122

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>