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