/ Published in: C
Public domain snippet that dumps a path (or other list of directories in a specified environment variable) with one directory per line.
Expand |
Embed | Plain Text
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <string.h> /* get strdup() and strtok_r() from other snippets if needed */ #ifdef _WIN32 static char const* path_separators=";"; #else static char const* path_separators=":"; #endif int main( int argc, char** argv) { char* pPath; char const* envvar = "PATH"; if (argc > 1) { envvar = argv[1]; } pPath = getenv(envvar); if (!pPath) pPath = ""; pPath = strdup( pPath); if (!pPath) { return 1; } if (pPath!=NULL) { char* context = NULL; char* pDir = NULL; for (pDir = strtok_r( pPath, path_separators, &context); pDir != NULL; pDir = strtok_r( NULL, path_separators, &context)) { } } free( pPath); return 0; }
You need to login to post a comment.
