Changeset 55597 in vbox for trunk/src/VBox/Runtime/generic/env-generic.cpp
- Timestamp:
- May 2, 2015 4:59:15 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/generic/env-generic.cpp
r55585 r55597 400 400 401 401 402 /** 403 * Appends an already allocated string to papszEnv. 404 * 405 * @returns IPRT status code 406 * @param pIntEnv The environment block to append it to. 407 * @param pszEntry The string to add. Already duplicated, caller 408 * does error cleanup. 409 */ 410 static int rtEnvIntAppend(PRTENVINTERNAL pIntEnv, char *pszEntry) 411 { 412 /* 413 * Do we need to resize the array? 414 */ 415 int rc = VINF_SUCCESS; 416 size_t iVar = pIntEnv->cVars; 417 if (iVar + 2 > pIntEnv->cAllocated) 418 { 419 void *pvNew = RTMemRealloc(pIntEnv->papszEnv, sizeof(char *) * (pIntEnv->cAllocated + RTENV_GROW_SIZE)); 420 if (!pvNew) 421 rc = VERR_NO_MEMORY; 422 else 423 { 424 pIntEnv->papszEnv = (char **)pvNew; 425 pIntEnv->cAllocated += RTENV_GROW_SIZE; 426 for (size_t iNewVar = pIntEnv->cVars; iNewVar < pIntEnv->cAllocated; iNewVar++) 427 pIntEnv->papszEnv[iNewVar] = NULL; 428 } 429 } 430 if (RT_SUCCESS(rc)) 431 { 432 /* 433 * Append it. 434 */ 435 pIntEnv->papszEnv[iVar] = pszEntry; 436 pIntEnv->papszEnv[iVar + 1] = NULL; /* this isn't really necessary, but doesn't hurt. */ 437 pIntEnv->cVars = iVar + 1; 438 } 439 return rc; 440 } 441 442 402 443 RTDECL(int) RTEnvSetEx(RTENV Env, const char *pszVar, const char *pszValue) 403 444 { … … 474 515 { 475 516 /* 476 * Adding a new variable. Resize the array if required 477 * and then insert the new value at the end. 517 * New variable, append it. 478 518 */ 479 if (pIntEnv->cVars + 2 > pIntEnv->cAllocated) 480 { 481 void *pvNew = RTMemRealloc(pIntEnv->papszEnv, sizeof(char *) * (pIntEnv->cAllocated + RTENV_GROW_SIZE)); 482 if (!pvNew) 483 rc = VERR_NO_MEMORY; 484 else 485 { 486 pIntEnv->papszEnv = (char **)pvNew; 487 pIntEnv->cAllocated += RTENV_GROW_SIZE; 488 for (size_t iNewVar = pIntEnv->cVars; iNewVar < pIntEnv->cAllocated; iNewVar++) 489 pIntEnv->papszEnv[iNewVar] = NULL; 490 } 491 } 492 if (RT_SUCCESS(rc)) 493 { 494 pIntEnv->papszEnv[iVar] = pszEntry; 495 pIntEnv->papszEnv[iVar + 1] = NULL; /* this isn't really necessary, but doesn't hurt. */ 496 pIntEnv->cVars++; 497 Assert(pIntEnv->cVars == iVar + 1); 498 } 519 Assert(pIntEnv->cVars == iVar); 520 rc = rtEnvIntAppend(pIntEnv, pszEntry); 499 521 } 500 522 … … 573 595 /* no break, there could be more. */ 574 596 } 597 598 /* 599 * If this is a change record, we may need to add it. 600 */ 601 if (rc == VINF_ENV_VAR_NOT_FOUND && pIntEnv->fPutEnvBlock) 602 { 603 char *pszEntry = (char *)RTMemDup(pszVar, cchVar + 1); 604 if (pszEntry) 605 { 606 rc = rtEnvIntAppend(pIntEnv, pszEntry); 607 if (RT_SUCCESS(rc)) 608 rc = VINF_ENV_VAR_NOT_FOUND; 609 else 610 RTMemFree(pszEntry); 611 } 612 else 613 rc = VERR_NO_MEMORY; 614 } 575 615 576 616 RTENV_UNLOCK(pIntEnv);
Note:
See TracChangeset
for help on using the changeset viewer.