vms_decc_argv.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #include "apps.h" /* for app_malloc() */
  13. char **newargv = NULL;
  14. static void cleanup_argv(void)
  15. {
  16. OPENSSL_free(newargv);
  17. newargv = NULL;
  18. }
  19. char **copy_argv(int *argc, char *argv[])
  20. {
  21. /*-
  22. * The note below is for historical purpose. On VMS now we always
  23. * copy argv "safely."
  24. *
  25. * 2011-03-22 SMS.
  26. * If we have 32-bit pointers everywhere, then we're safe, and
  27. * we bypass this mess, as on non-VMS systems.
  28. * Problem 1: Compaq/HP C before V7.3 always used 32-bit
  29. * pointers for argv[].
  30. * Fix 1: For a 32-bit argv[], when we're using 64-bit pointers
  31. * everywhere else, we always allocate and use a 64-bit
  32. * duplicate of argv[].
  33. * Problem 2: Compaq/HP C V7.3 (Alpha, IA64) before ECO1 failed
  34. * to NULL-terminate a 64-bit argv[]. (As this was written, the
  35. * compiler ECO was available only on IA64.)
  36. * Fix 2: Unless advised not to (VMS_TRUST_ARGV), we test a
  37. * 64-bit argv[argc] for NULL, and, if necessary, use a
  38. * (properly) NULL-terminated (64-bit) duplicate of argv[].
  39. * The same code is used in either case to duplicate argv[].
  40. * Some of these decisions could be handled in preprocessing,
  41. * but the code tends to get even uglier, and the penalty for
  42. * deciding at compile- or run-time is tiny.
  43. */
  44. int i, count = *argc;
  45. char **p = newargv;
  46. cleanup_argv();
  47. newargv = app_malloc(sizeof(*newargv) * (count + 1), "argv copy");
  48. if (newargv == NULL)
  49. return NULL;
  50. /* Register automatic cleanup on first use */
  51. if (p == NULL)
  52. OPENSSL_atexit(cleanup_argv);
  53. for (i = 0; i < count; i++)
  54. newargv[i] = argv[i];
  55. newargv[i] = NULL;
  56. *argc = i;
  57. return newargv;
  58. }