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