Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.2 KiB

  1. /***
  2. *wtombenv.c - convert wide environment block to multibyte
  3. *
  4. * Copyright (c) 1993-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines __wtomb_environ(). Create a multibyte equivalent of
  8. * an existing wide character environment block.
  9. *
  10. *Revision History:
  11. * 11-30-93 CFW initial version
  12. * 02-07-94 CFW POSIXify.
  13. * 01-10-95 CFW Debug CRT allocs.
  14. * 08-28-98 GJF Use CP_ACP instead of CP_OEMCP.
  15. *
  16. *******************************************************************************/
  17. #ifndef _POSIX_
  18. #include <windows.h>
  19. #include <cruntime.h>
  20. #include <internal.h>
  21. #include <stdlib.h>
  22. #include <dbgint.h>
  23. /***
  24. *__wtomb_environ - copy wide environment block to multibyte environment block
  25. *
  26. *Purpose:
  27. * Create a multibyte equivalent of an existing wide character
  28. * environment block.
  29. *
  30. *Entry:
  31. * Assume _wenviron (global pointer) points to existing wide
  32. * environment block.
  33. *
  34. *Exit:
  35. * If success, every wide environment variable has been added to
  36. * the multibyte environment block and returns 0.
  37. * If failure, returns -1.
  38. *
  39. *Exceptions:
  40. * If space cannot be allocated, returns -1.
  41. *
  42. *******************************************************************************/
  43. int __cdecl __wtomb_environ (
  44. void
  45. )
  46. {
  47. char *envp;
  48. wchar_t **wenvp = _wenviron;
  49. /*
  50. * For every environment variable in the multibyte environment,
  51. * convert it and add it to the wide environment.
  52. */
  53. while (*wenvp)
  54. {
  55. int size;
  56. /* find out how much space is needed */
  57. if ((size = WideCharToMultiByte(CP_ACP, 0, *wenvp, -1, NULL, 0, NULL, NULL)) == 0)
  58. return -1;
  59. /* allocate space for variable */
  60. if ((envp = (char *) _malloc_crt(size * sizeof(char))) == NULL)
  61. return -1;
  62. /* convert it */
  63. if (WideCharToMultiByte(CP_ACP, 0, *wenvp, -1, envp, size, NULL, NULL) == 0)
  64. return -1;
  65. /* set it - this is not primary call, so set primary == 0 */
  66. __crtsetenv(envp, 0);
  67. wenvp++;
  68. }
  69. return 0;
  70. }
  71. #endif /* _POSIX_ */