vms_decc_argv.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright 2015-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdlib.h>
  10. #include <openssl/crypto.h>
  11. #include "platform.h" /* for copy_argv() */
  12. char **newargv = NULL;
  13. static void cleanup_argv(void)
  14. {
  15. OPENSSL_free(newargv);
  16. newargv = NULL;
  17. }
  18. char **copy_argv(int *argc, char *argv[])
  19. {
  20. /*-
  21. * The note below is for historical purpose. On VMS now we always
  22. * copy argv "safely."
  23. *
  24. * 2011-03-22 SMS.
  25. * If we have 32-bit pointers everywhere, then we're safe, and
  26. * we bypass this mess, as on non-VMS systems.
  27. * Problem 1: Compaq/HP C before V7.3 always used 32-bit
  28. * pointers for argv[].
  29. * Fix 1: For a 32-bit argv[], when we're using 64-bit pointers
  30. * everywhere else, we always allocate and use a 64-bit
  31. * duplicate of argv[].
  32. * Problem 2: Compaq/HP C V7.3 (Alpha, IA64) before ECO1 failed
  33. * to NULL-terminate a 64-bit argv[]. (As this was written, the
  34. * compiler ECO was available only on IA64.)
  35. * Fix 2: Unless advised not to (VMS_TRUST_ARGV), we test a
  36. * 64-bit argv[argc] for NULL, and, if necessary, use a
  37. * (properly) NULL-terminated (64-bit) duplicate of argv[].
  38. * The same code is used in either case to duplicate argv[].
  39. * Some of these decisions could be handled in preprocessing,
  40. * but the code tends to get even uglier, and the penalty for
  41. * deciding at compile- or run-time is tiny.
  42. */
  43. int i, count = *argc;
  44. char **p = newargv;
  45. cleanup_argv();
  46. /*
  47. * We purposefully use OPENSSL_malloc() rather than app_malloc() here,
  48. * to avoid symbol name clashes in test programs that would otherwise
  49. * get them when linking with all of libapps.a.
  50. * See comment in test/build.info.
  51. */
  52. newargv = OPENSSL_malloc(sizeof(*newargv) * (count + 1));
  53. if (newargv == NULL)
  54. return NULL;
  55. /* Register automatic cleanup on first use */
  56. if (p == NULL)
  57. OPENSSL_atexit(cleanup_argv);
  58. for (i = 0; i < count; i++)
  59. newargv[i] = argv[i];
  60. newargv[i] = NULL;
  61. *argc = i;
  62. return newargv;
  63. }