diff options
author | schmidt <douglascraigschmidt@users.noreply.github.com> | 2006-12-27 21:34:10 +0000 |
---|---|---|
committer | schmidt <douglascraigschmidt@users.noreply.github.com> | 2006-12-27 21:34:10 +0000 |
commit | eebd5827aad6628d2b1f47d1bd0b9995b5e939b1 (patch) | |
tree | 939643f4e6dfdd065c2d1aa9dfce682187d0b727 /ACE/ace/OS_NS_stdlib.cpp | |
parent | 4c5219c5e4ef10c8758a9257292f4cf8d937e4e6 (diff) | |
download | ATCD-eebd5827aad6628d2b1f47d1bd0b9995b5e939b1.tar.gz |
ChangeLogTag:Wed
Diffstat (limited to 'ACE/ace/OS_NS_stdlib.cpp')
-rw-r--r-- | ACE/ace/OS_NS_stdlib.cpp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/ACE/ace/OS_NS_stdlib.cpp b/ACE/ace/OS_NS_stdlib.cpp index 0850b2a63df..cd2005eb041 100644 --- a/ACE/ace/OS_NS_stdlib.cpp +++ b/ACE/ace/OS_NS_stdlib.cpp @@ -6,6 +6,8 @@ ACE_RCSID (ace, OS_NS_stdlib, "$Id$") +#include "ace/Default_Constants.h" + #if !defined (ACE_HAS_INLINED_OSCALLS) # include "ace/OS_NS_stdlib.inl" #endif /* ACE_HAS_INLINED_OSCALLS */ @@ -101,6 +103,76 @@ ACE_OS::getenvstrings (void) #endif /* ACE_WIN32 */ } +// Return a dynamically allocated duplicate of <str>, substituting the +// environment variables of form $VAR_NAME. Note that the pointer is +// allocated with <ACE_OS::malloc> and must be freed by +// <ACE_OS::free>. + +ACE_TCHAR * +ACE_OS::strenvdup (const ACE_TCHAR *str) +{ +#if defined (ACE_HAS_WINCE) + // WinCE doesn't have environment variables so we just skip it. + return ACE_OS::strdup (str); +#elif defined (ACE_LACKS_ENV) + ACE_UNUSED_ARG (str); + ACE_NOTSUP_RETURN (0); +#else + const ACE_TCHAR * start = 0; + if ((start = ACE_OS::strchr (str, ACE_LIB_TEXT ('$'))) != 0) + { + ACE_TCHAR buf[ACE_DEFAULT_ARGV_BUFSIZ]; + size_t var_len = ACE_OS::strcspn (&start[1], + ACE_LIB_TEXT ("$~!#%^&*()-+=\\|/?,.;:'\"`[]{} \t\n\r")); + ACE_OS::strncpy (buf, &start[1], var_len); + buf[var_len++] = ACE_LIB_TEXT ('\0'); +# if defined (ACE_WIN32) + // Always use the ACE_TCHAR for Windows. + ACE_TCHAR *temp = ACE_OS::getenv (buf); +# else + // Use char * for environment on non-Windows. + char *temp = ACE_OS::getenv (ACE_TEXT_ALWAYS_CHAR (buf)); +# endif /* ACE_WIN32 */ + size_t buf_len = ACE_OS::strlen (str) + 1; + if (temp != 0) + buf_len += ACE_OS::strlen (temp) - var_len; + ACE_TCHAR * buf_p = buf; + if (buf_len > ACE_DEFAULT_ARGV_BUFSIZ) + { + buf_p = + (ACE_TCHAR *) ACE_OS::malloc (buf_len * sizeof (ACE_TCHAR)); + if (buf_p == 0) + { + errno = ENOMEM; + return 0; + } + } + ACE_TCHAR * p = buf_p; + size_t len = start - str; + ACE_OS::strncpy (p, str, len); + p += len; + if (temp != 0) + { +# if defined (ACE_WIN32) + p = ACE_OS::strecpy (p, temp) - 1; +# else + p = ACE_OS::strecpy (p, ACE_TEXT_CHAR_TO_TCHAR (temp)) - 1; +# endif /* ACE_WIN32 */ + } + else + { + ACE_OS::strncpy (p, start, var_len); + p += var_len; + *p = ACE_LIB_TEXT ('\0'); + } + ACE_OS::strcpy (p, &start[var_len]); + return (buf_p == buf) ? ACE_OS::strdup (buf) : buf_p; + } + else + return ACE_OS::strdup (str); +#endif /* ACE_HAS_WINCE */ +} + #if !defined (ACE_HAS_ITOA) char * ACE_OS::itoa_emulation (int value, char *string, int radix) |