void CvtStringToStringList( String string, String **listPtr, int *countPtr ) { int num_items = 0; String * items = NULL; int size_item_array = 0; String ptr = (String)_DtStripSpaces(string); String end; while ((ptr != NULL) && (*ptr != '\0')) { if (num_items >= size_item_array) { size_item_array += 20; items = (String *) XtRealloc((char *) items, sizeof(String) * size_item_array); } end = DtStrchr(ptr, ','); if (end) *end = '\0'; items[num_items] = strcpy(XtMalloc(strlen(ptr)+1), ptr); num_items++; if (end != NULL) ptr = (String)_DtStripSpaces(end + 1); else break; } *countPtr = num_items; *listPtr= items; }
/****************************************************************************** * * _DtEnvMapIt() * * Fill out a map cache for a single environment variable. */ static void _DtEnvMapIt( char *envVar, cachedEnvVar *envVarCache, char *targetHost) { char *separator, *tmpPtr, *tmpPtr2, swapout, *netpath; char *prePend, *postPend, *newPrePend; char **pathList; int availPathListSize, pathListCount, availEnvStrSize, len, tmpi, i; int considerMapping; _Xstrtokparams strtok_buf; /* * Information Layout: * * localEnvVarPtr = ptr to original "PATH=/users/foo:/users/bar" * localEnvVarCopy = copy of original "/users/foo:/users/bar" * mappedEnvVarPtr = mapped "PATH=/nfs/.../users/foo:/nfs/.../users/bar" */ if ( (envVarCache->localEnvVarPtr = getenv( envVar )) ) { envVarCache->localEnvVarCopy = strdup( envVarCache->localEnvVarPtr ); /* sneak back past "NAME=" portion. */ envVarCache->localEnvVarPtr -= strlen( envVar ) + 1; } else { /* * Nothing to map. Punt. */ envVarCache->localEnvVarCopy = (char *) NULL; envVarCache->localEnvVarPtr = (char *) NULL; return; } #ifdef _DTENV_SUPPORT_COMMA_SEPARATED /* * Pick between colon-separated and comma-separated host-qualified * mapping code. */ if ( !strcmp(envVar, "DTDATABASESEARCHPATH") ) { /* * comma-separated and host-qualified mapping. */ separator = ","; } else { /* * colon-separated mapping. */ separator = ":"; } #else separator = ":"; #endif /* _DTENV_SUPPORT_COMMA_SEPARATED */ /* * Break path list into elements */ availPathListSize = MALLOC_BUMP_SIZE; pathListCount = 0; pathList = (char **) malloc( sizeof(char *) * availPathListSize ); /* * Break up path list into an array of path elements. */ tmpPtr = strdup( envVarCache->localEnvVarCopy ); /* work copy */ while (1) { if (!pathListCount) tmpPtr2 = _XStrtok( tmpPtr, separator, strtok_buf ); else tmpPtr2 = _XStrtok( (char *) NULL, separator, strtok_buf ); if (tmpPtr2) { pathListCount++; if (pathListCount > availPathListSize) { availPathListSize += MALLOC_BUMP_SIZE; pathList = (char **) realloc( (char *) pathList, sizeof(char *) * availPathListSize ); } pathList[pathListCount-1] = strdup( tmpPtr2 ); } else { break; } } free( tmpPtr ); /* * Setup new "NAME=....." string. */ availEnvStrSize = strlen( envVar ) + 64; envVarCache->mappedEnvVarPtr = (char *) calloc( availEnvStrSize, sizeof(char) ); strcpy( envVarCache->mappedEnvVarPtr, envVar ); strcat( envVarCache->mappedEnvVarPtr, "=" ); /* * Start mapping each path element. */ for ( i = 0; i < pathListCount; i++ ) { prePend = pathList[i]; postPend = (char *) NULL; newPrePend = (char *) NULL; /* * Assume we need to map this path element. */ considerMapping = 1; #ifdef _DTENV_SUPPORT_COMMA_SEPARATED if ( !strcmp( separator, "," ) ) { if ( DtStrchr(prePend, ':' ) ) { /* * Host qualified elements in a comma separated list * will NOT be mapped. */ considerMapping = 0; } } #endif /* _DTENV_SUPPORT_COMMA_SEPARATED */ if (considerMapping) { /* * Tear apart and check for so called substitution characters. */ if (( tmpPtr = DtStrchr(prePend, '%') )) { /* * Temporarly shorten path up to substitution character. */ swapout = *tmpPtr; *tmpPtr = '\0'; /* * Move the dividing point back to a directory element. */ tmpPtr2 = DtStrrchr( prePend, '/' ); /* * Restore the send half of the string. */ *tmpPtr = swapout; if (tmpPtr2) { /* * Can do a split around the "/". * * Will have "<prePath>/" and "/<postPath>". */ postPend = strdup( tmpPtr2 ); *(tmpPtr2 + mblen(tmpPtr2, MB_CUR_MAX)) = '\0'; } } #ifdef DTENV_PERF_HOOK { int tpi; extern unsigned long stopwatch_tt_file_netfile; extern int stopwatch_repeat_rate; struct timeval start, stop; struct timezone junk; gettimeofday( &start, &junk ); for ( tpi = 0; tpi < stopwatch_repeat_rate-1; tpi++ ) { netpath = _DtEnv_tt_file_netfile( prePend ); if ( tt_ptr_error(netpath) == TT_OK ) ttfreeAndNull( netpath ); } netpath = _DtEnv_tt_file_netfile( prePend ); gettimeofday( &stop, &junk ); if (start.tv_usec > stop.tv_usec) { stop.tv_usec += 1000000; stop.tv_sec--; } stopwatch_tt_file_netfile += (stop.tv_usec - start.tv_usec); stopwatch_tt_file_netfile += (stop.tv_sec - start.tv_sec) * 1000000; } #else netpath = _DtEnv_tt_file_netfile( prePend ); #endif /* DTENV_PERF_HOOK */ if ( tt_ptr_error(netpath) != TT_OK ) { newPrePend = (char *) NULL; } else { #ifdef DTENV_PERF_HOOK { int tpi; extern unsigned long stopwatch_tt_netfile_file; extern int stopwatch_repeat_rate; struct timeval start, stop; struct timezone junk; gettimeofday( &start, &junk ); for ( tpi = 0; tpi < stopwatch_repeat_rate-1; tpi++ ) { newPrePend = _DtEnv_tt_host_netfile_file (targetHost, netpath); if ( tt_ptr_error(newPrePend) == TT_OK ) ttfreeAndNull( newPrePend ); } newPrePend = _DtEnv_tt_host_netfile_file (targetHost, netpath); gettimeofday( &stop, &junk ); if (start.tv_usec > stop.tv_usec) { stop.tv_usec += 1000000; stop.tv_sec--; } stopwatch_tt_netfile_file += (stop.tv_usec - start.tv_usec); stopwatch_tt_netfile_file += (stop.tv_sec - start.tv_sec) * 1000000; } #else newPrePend = _DtEnv_tt_host_netfile_file (targetHost, netpath); #endif /* DTENV_PERF_HOOK */ if ( tt_ptr_error(newPrePend) != TT_OK ) { newPrePend = (char *) NULL; } ttfreeAndNull( netpath ); } } /* * Calculate length of the new path element to the new path list. */ tmpi = strlen(envVarCache->mappedEnvVarPtr)+1; /* current list + ... */ if ( i != 0 ) tmpi += 1; /* separator */ if (newPrePend) tmpi += strlen(newPrePend); /* new prePend or ... */ else tmpi += strlen(prePend); /* ... old prePend */ if (postPend) tmpi += strlen(postPend); /* new postPend */ if ( tmpi > availEnvStrSize ) { /* * Grow new mappedEnvVar space. */ availEnvStrSize = tmpi + 64; envVarCache->mappedEnvVarPtr = (char *) realloc( (char *) envVarCache->mappedEnvVarPtr, availEnvStrSize ); } /* * Add the new path element. */ if ( i != 0 ) strcat( envVarCache->mappedEnvVarPtr, separator ); if (newPrePend) strcat( envVarCache->mappedEnvVarPtr, newPrePend ); else strcat( envVarCache->mappedEnvVarPtr, prePend ); if (postPend) strcat( envVarCache->mappedEnvVarPtr, postPend ); freeAndNull( prePend ); /* aka pathList[i] */ ttfreeAndNull( newPrePend ); freeAndNull( postPend ); } freeAndNull( pathList ); }