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.

86 lines
2.3 KiB

  1. /***
  2. *mbtowenv.c - convert multibyte environment block to wide
  3. *
  4. * Copyright (c) 1993-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines __mbtow_environ(). Create a wide character equivalent of
  8. * an existing multibyte 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. * 07-06-01 BWT Free wenvp before exiting on MultiByteToWideChar failure
  16. *
  17. *******************************************************************************/
  18. #ifndef _POSIX_
  19. #include <windows.h>
  20. #include <cruntime.h>
  21. #include <internal.h>
  22. #include <stdlib.h>
  23. #include <dbgint.h>
  24. /***
  25. *__mbtow_environ - copy multibyte environment block to wide environment block
  26. *
  27. *Purpose:
  28. * Create a wide character equivalent of an existing multibyte
  29. * environment block.
  30. *
  31. *Entry:
  32. * Assume _environ (global pointer) points to existing multibyte
  33. * environment block.
  34. *
  35. *Exit:
  36. * If success, every multibyte environment variable has been added to
  37. * the wide environment block and returns 0.
  38. * If failure, returns -1.
  39. *
  40. *Exceptions:
  41. * If space cannot be allocated, returns -1.
  42. *
  43. *******************************************************************************/
  44. int __cdecl __mbtow_environ (
  45. void
  46. )
  47. {
  48. int size;
  49. wchar_t *wenvp;
  50. char **envp = _environ;
  51. /*
  52. * For every environment variable in the multibyte environment,
  53. * convert it and add it to the wide environment.
  54. */
  55. while (*envp)
  56. {
  57. /* find out how much space is needed */
  58. if ((size = MultiByteToWideChar(CP_ACP, 0, *envp, -1, NULL, 0)) == 0)
  59. return -1;
  60. /* allocate space for variable */
  61. if ((wenvp = (wchar_t *) _malloc_crt(size * sizeof(wchar_t))) == NULL)
  62. return -1;
  63. /* convert it */
  64. if ((size = MultiByteToWideChar(CP_ACP, 0, *envp, -1, wenvp, size)) == 0) {
  65. _free_crt(wenvp);
  66. return -1;
  67. }
  68. /* set it - this is not primary call, so set primary == 0 */
  69. __crtwsetenv(wenvp, 0);
  70. envp++;
  71. }
  72. return 0;
  73. }
  74. #endif /* _POSIX_ */