fspr_app.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* Usage Notes:
  17. *
  18. * this module, and the misc/win32/utf8.c modules must be
  19. * compiled APR_EXPORT_STATIC and linked to an application with
  20. * the /entry:wmainCRTStartup flag. This module becomes the true
  21. * wmain entry point, and passes utf-8 reformatted argv and env
  22. * arrays to the application's main function.
  23. *
  24. * This module is only compatible with Unicode-only executables.
  25. * Mixed (Win9x backwards compatible) binaries should refer instead
  26. * to the fspr_startup.c module.
  27. *
  28. * _dbg_malloc/realloc is used in place of the usual API, in order
  29. * to convince the MSVCRT that they created these entities. If we
  30. * do not create them as _CRT_BLOCK entities, the crt will fault
  31. * on an assert. We are not worrying about the crt's locks here,
  32. * since we are single threaded [so far].
  33. */
  34. #include "fspr_general.h"
  35. #include "ShellAPI.h"
  36. #include "crtdbg.h"
  37. #include "wchar.h"
  38. #include "fspr_arch_file_io.h"
  39. #include "assert.h"
  40. #include "fspr_private.h"
  41. #include "fspr_arch_misc.h"
  42. /* This symbol is _private_, although it must be exported.
  43. */
  44. extern int main(int argc, const char **argv, const char **env);
  45. int wmain(int argc, const wchar_t **wargv, const wchar_t **wenv)
  46. {
  47. char **argv;
  48. char **env;
  49. int dupenv;
  50. (void)fspr_wastrtoastr(&argv, wargv, argc);
  51. dupenv = fspr_wastrtoastr(&env, wenv, -1);
  52. _environ = _malloc_dbg((dupenv + 1) * sizeof (char *),
  53. _CRT_BLOCK, __FILE__, __LINE__ );
  54. memcpy(_environ, env, (dupenv + 1) * sizeof (char *));
  55. /* MSVCRT will attempt to maintain the wide environment calls
  56. * on _putenv(), which is bogus if we've passed a non-ascii
  57. * string to _putenv(), since they use MultiByteToWideChar
  58. * and breaking the implicit utf-8 assumption we've built.
  59. *
  60. * Reset _wenviron for good measure.
  61. */
  62. if (_wenviron) {
  63. wenv = _wenviron;
  64. _wenviron = NULL;
  65. free((wchar_t **)wenv);
  66. }
  67. fspr_app_init_complete = 1;
  68. return main(argc, argv, env);
  69. }