831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync/* $Id$ */
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync/** @file
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync * IPRT - Environment, RTEnvDupEx, generic.
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync */
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync/*
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2010 Oracle Corporation
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync *
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync * available from http://www.virtualbox.org. This file is free software;
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync * you can redistribute it and/or modify it under the terms of the GNU
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync * General Public License (GPL) as published by the Free Software
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync *
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync * The contents of this file may alternatively be used under the terms
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync * of the Common Development and Distribution License Version 1.0
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync * VirtualBox OSE distribution, in which case the provisions of the
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync * CDDL are applicable instead of those of the GPL.
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync *
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync * You may elect to license modified versions of this file under the
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync * terms and conditions of either the GPL or the CDDL or both.
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync */
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync/*******************************************************************************
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync* Header Files *
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync*******************************************************************************/
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync#include <iprt/env.h>
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync#include "internal/iprt.h"
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync#include <iprt/assert.h>
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync#include <iprt/err.h>
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync#include <iprt/string.h>
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync#include <iprt/mem.h>
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsyncRTDECL(char *) RTEnvDupEx(RTENV Env, const char *pszVar)
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync{
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync /*
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync * Try with a small buffer. This helps avoid allocating a heap buffer for
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync * variables that doesn't exist.
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync */
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync char szSmall[256];
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync int rc = RTEnvGetEx(Env, pszVar, szSmall, sizeof(szSmall), NULL);
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync if (RT_SUCCESS(rc))
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync return RTStrDup(szSmall);
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync if (rc != VERR_BUFFER_OVERFLOW)
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync return NULL;
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync /*
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync * It's a bug bugger.
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync */
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync size_t cbBuf = _1K;
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync char *pszBuf = (char *)RTMemAlloc(cbBuf);
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync for (;;)
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync {
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync rc = RTEnvGetEx(Env, pszVar, pszBuf, cbBuf, NULL);
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync if (RT_SUCCESS(rc))
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync return pszBuf; /* ASSUMES RTMemAlloc can be freed by RTStrFree! */
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync if (rc != VERR_BUFFER_OVERFLOW)
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync break;
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync if (cbBuf >= 64 * _1M)
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync break;
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync cbBuf *= 2;
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync char *pszNew = (char *)RTMemRealloc(pszBuf, cbBuf);
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync if (!pszNew)
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync break;
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync pszBuf = pszNew;
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync }
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync RTMemFree(pszBuf);
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync return NULL;
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync}
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsyncRT_EXPORT_SYMBOL(RTEnvGetExecEnvP);
831b4c533723665e3a004ca13c9f413f4221ffbbvboxsync