#include #include #include #include #include #include #define BUFFER_MAX_SIZE 200 #define SIZE(x) ((x) > BUFFER_MAX_SIZE) ? BUFFER_MAX_SIZE : x const char *MYLIB_PATH = "MYLIB_PATH=/home/richer"; /** * list files in directory identified by its name */ void find_files(char *dir_name) { DIR *dp; struct dirent *ep; dp = opendir (dir_name); if (dp != NULL) { while (ep = readdir (dp)) { if (!((strcmp(ep->d_name, ".") == 0) || (strcmp(ep->d_name, "..") == 0))) { printf("- %s\n", ep->d_name); } } (void) closedir (dp); } else { perror ("Couldn't open the directory"); } } /** * main function */ int main(int argc, char *argv[]) { char *path, *pbegin, *pend; char buffer[BUFFER_MAX_SIZE + 1]; path = getenv("PATH"); // get all paths and find files /* pbegin = path; pend = strchr(pbegin, ':'); while (*pbegin != '\0') { if (pend == NULL) { strcpy(buffer, pbegin); } else { int size = pend-pbegin; strncpy(buffer, pbegin, SIZE(size)); buffer[pend-pbegin] = '\0'; } printf("===========================\n"); printf("directory %s\n", buffer); printf("===========================\n"); find_files(buffer); if (pend == NULL) break; pbegin = ++pend; pend = strchr(pbegin, ':'); } */ // or simplified version using strtok char *p = strtok(path, ":"); while (p != NULL) { printf("===========================\n"); printf("directory %s\n", p); printf("===========================\n"); find_files(p); p = strtok(NULL, ":"); } // play with setenv a if (!setenv("MYLIB_PATH", "/home/richer", 1) < 0) { fprintf(stderr, "could not set MYLIB_PATH with setenv !!!!\n"); } if (putenv(const_cast(MYLIB_PATH))!=0) { fprintf(stderr, "could not set MYLIB_PATH with putenv => %d\n%s !!!!\n", errno, strerror(errno)); } system("echo $MYLIB_PATH"); system("/home/richer/public_html/ens/inra/test_mylib.sh"); return EXIT_SUCCESS; }