getenv_safe.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. * Copyright (c) 2014, gperftools Contributors
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Google Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #ifndef GETENV_SAFE_H
  32. #define GETENV_SAFE_H
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /*
  37. * This getenv function is safe to call before the C runtime is initialized.
  38. * On Windows, it utilizes GetEnvironmentVariable() and on unix it uses
  39. * /proc/self/environ instead calling getenv(). It's intended to be used in
  40. * routines that run before main(), when the state required for getenv() may
  41. * not be set up yet. In particular, errno isn't set up until relatively late
  42. * (after the pthreads library has a chance to make it threadsafe), and
  43. * getenv() doesn't work until then.
  44. * On some platforms, this call will utilize the same, static buffer for
  45. * repeated GetenvBeforeMain() calls. Callers should not expect pointers from
  46. * this routine to be long lived.
  47. * Note that on unix, /proc only has the environment at the time the
  48. * application was started, so this routine ignores setenv() calls/etc. Also
  49. * note it only reads the first 16K of the environment.
  50. *
  51. * NOTE: this is version of GetenvBeforeMain that's usable from
  52. * C. Implementation is in sysinfo.cc
  53. */
  54. const char* TCMallocGetenvSafe(const char* name);
  55. #ifdef __cplusplus
  56. }
  57. #endif
  58. #endif