Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char* prependPath( char* exeName )
- {
- char* path = getenv("PATH");
- char* pathDeepCopy = (char *)malloc(strlen(path) + 1);
- char* token[80];
- int j, i=0; // used to iterate through array
- strcpy(pathDeepCopy, path);
- //parse and split
- token[0] = strtok(pathDeepCopy, ":"); //get pointer to first token found and store in 0
- //place in array
- while(token[i]!= NULL) { //ensure a pointer was found
- i++;
- token[i] = strtok(NULL, ":"); //continue to tokenize the string
- }
- for(j = 0; j <= i-1; j++) {
- strcat(token[j], "/");
- //strcat(token[j], exeName);
- printf("%sn", token[j]); //print out all of the tokens
- }
- }
- ...
- /usr/local/applic/Maple/bin/which
- which/which
- /usr/local/applic/opnet/8.1.A.wdmguru/sys/unix/bin/which
- which/which
- Bus error (core dumped)
- char* prependPath( char* exeName )
- {
- char* path = getenv("PATH");
- char* pathDeepCopy = strdup(path);
- char* token[80];
- int j, i; // used to iterate through array
- token[0] = strtok(pathDeepCopy, ":");
- for(i = 0;(token[i] != NULL) && (i < 80);++i)
- {
- token[i] = strtok(NULL, ":");
- }
- for(j = 0; j <= i; ++j)
- {
- char* tmp = (char*)malloc(strlen(token[j]) + 1 + strlen(exeName) + 1);
- strcpy(tmp,token[j]);
- strcat(tmp,"/");
- strcat(tmp,exeName);
- printf("%sn",tmp); //print out all of the tokens
- free(tmp);
- }
- free(pathDeepCopy);
- }
- //parse and split
- token[0] = strtok(pathDeepCopy, ":");//get pointer to first token found and store in 0
- //place in array
- while(token[i]!= NULL) { //ensure a pointer was found
- i++;
- token[i] = strtok(NULL, ":"); //continue to tokenize the string
- }
- // use new array for storing the new tokens
- // pardon my C lang skills. IT's been a "while" since I wrote device drivers in C.
- const int I = i;
- const int MAX_SIZE = MAX_PATH;
- char ** newTokens = new char [MAX_PATH][I];
- for (int k = 0; k < i; ++k) {
- sprintf(newTokens[k], "%s%c", token[j], '/');
- printf("%sn", newtoken[j]); //print out all of the tokens
- }
Add Comment
Please, Sign In to add comment