113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci/* File: curl_crtl_init.c
2513498266Sopenharmony_ci *
2613498266Sopenharmony_ci * This file makes sure that the DECC Unix settings are correct for
2713498266Sopenharmony_ci * the mode the program is run in.
2813498266Sopenharmony_ci *
2913498266Sopenharmony_ci * The CRTL has not been initialized at the time that these routines
3013498266Sopenharmony_ci * are called, so many routines can not be called.
3113498266Sopenharmony_ci *
3213498266Sopenharmony_ci * This is a module that provides a LIB$INITIALIZE routine that
3313498266Sopenharmony_ci * will turn on some CRTL features that are not enabled by default.
3413498266Sopenharmony_ci *
3513498266Sopenharmony_ci * The CRTL features can also be turned on via logical names, but that
3613498266Sopenharmony_ci * impacts all programs and some aren't ready, willing, or able to handle
3713498266Sopenharmony_ci * those settings.
3813498266Sopenharmony_ci *
3913498266Sopenharmony_ci * On VMS versions that are too old to use the feature setting API, this
4013498266Sopenharmony_ci * module falls back to using logical names.
4113498266Sopenharmony_ci *
4213498266Sopenharmony_ci * Copyright (C) John Malmberg
4313498266Sopenharmony_ci *
4413498266Sopenharmony_ci * Permission to use, copy, modify, and/or distribute this software for any
4513498266Sopenharmony_ci * purpose with or without fee is hereby granted, provided that the above
4613498266Sopenharmony_ci * copyright notice and this permission notice appear in all copies.
4713498266Sopenharmony_ci *
4813498266Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
4913498266Sopenharmony_ci * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
5013498266Sopenharmony_ci * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
5113498266Sopenharmony_ci * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
5213498266Sopenharmony_ci * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
5313498266Sopenharmony_ci * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
5413498266Sopenharmony_ci * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
5513498266Sopenharmony_ci *
5613498266Sopenharmony_ci */
5713498266Sopenharmony_ci
5813498266Sopenharmony_ci/* Unix headers */
5913498266Sopenharmony_ci#include <stdio.h>
6013498266Sopenharmony_ci#include <string.h>
6113498266Sopenharmony_ci
6213498266Sopenharmony_ci/* VMS specific headers */
6313498266Sopenharmony_ci#include <descrip.h>
6413498266Sopenharmony_ci#include <lnmdef.h>
6513498266Sopenharmony_ci#include <stsdef.h>
6613498266Sopenharmony_ci
6713498266Sopenharmony_ci#pragma member_alignment save
6813498266Sopenharmony_ci#pragma nomember_alignment longword
6913498266Sopenharmony_ci#pragma message save
7013498266Sopenharmony_ci#pragma message disable misalgndmem
7113498266Sopenharmony_cistruct itmlst_3 {
7213498266Sopenharmony_ci  unsigned short int buflen;
7313498266Sopenharmony_ci  unsigned short int itmcode;
7413498266Sopenharmony_ci  void *bufadr;
7513498266Sopenharmony_ci  unsigned short int *retlen;
7613498266Sopenharmony_ci};
7713498266Sopenharmony_ci#pragma message restore
7813498266Sopenharmony_ci#pragma member_alignment restore
7913498266Sopenharmony_ci
8013498266Sopenharmony_ci#ifdef __VAX
8113498266Sopenharmony_ci#define ENABLE "ENABLE"
8213498266Sopenharmony_ci#define DISABLE "DISABLE"
8313498266Sopenharmony_ci#else
8413498266Sopenharmony_ci
8513498266Sopenharmony_ci#define ENABLE TRUE
8613498266Sopenharmony_ci#define DISABLE 0
8713498266Sopenharmony_ciint   decc$feature_get_index (const char *name);
8813498266Sopenharmony_ciint   decc$feature_set_value (int index, int mode, int value);
8913498266Sopenharmony_ci#endif
9013498266Sopenharmony_ci
9113498266Sopenharmony_ciint   SYS$TRNLNM(
9213498266Sopenharmony_ci    const unsigned long * attr,
9313498266Sopenharmony_ci    const struct dsc$descriptor_s * table_dsc,
9413498266Sopenharmony_ci    struct dsc$descriptor_s * name_dsc,
9513498266Sopenharmony_ci    const unsigned char * acmode,
9613498266Sopenharmony_ci    const struct itmlst_3 * item_list);
9713498266Sopenharmony_ciint   SYS$CRELNM(
9813498266Sopenharmony_ci    const unsigned long * attr,
9913498266Sopenharmony_ci    const struct dsc$descriptor_s * table_dsc,
10013498266Sopenharmony_ci    const struct dsc$descriptor_s * name_dsc,
10113498266Sopenharmony_ci    const unsigned char * acmode,
10213498266Sopenharmony_ci    const struct itmlst_3 * item_list);
10313498266Sopenharmony_ci
10413498266Sopenharmony_ci
10513498266Sopenharmony_ci/* Take all the fun out of simply looking up a logical name */
10613498266Sopenharmony_cistatic int sys_trnlnm
10713498266Sopenharmony_ci   (const char * logname,
10813498266Sopenharmony_ci    char * value,
10913498266Sopenharmony_ci    int value_len)
11013498266Sopenharmony_ci{
11113498266Sopenharmony_ci    const $DESCRIPTOR(table_dsc, "LNM$FILE_DEV");
11213498266Sopenharmony_ci    const unsigned long attr = LNM$M_CASE_BLIND;
11313498266Sopenharmony_ci    struct dsc$descriptor_s name_dsc;
11413498266Sopenharmony_ci    int status;
11513498266Sopenharmony_ci    unsigned short result;
11613498266Sopenharmony_ci    struct itmlst_3 itlst[2];
11713498266Sopenharmony_ci
11813498266Sopenharmony_ci    itlst[0].buflen = value_len;
11913498266Sopenharmony_ci    itlst[0].itmcode = LNM$_STRING;
12013498266Sopenharmony_ci    itlst[0].bufadr = value;
12113498266Sopenharmony_ci    itlst[0].retlen = &result;
12213498266Sopenharmony_ci
12313498266Sopenharmony_ci    itlst[1].buflen = 0;
12413498266Sopenharmony_ci    itlst[1].itmcode = 0;
12513498266Sopenharmony_ci
12613498266Sopenharmony_ci    name_dsc.dsc$w_length = strlen(logname);
12713498266Sopenharmony_ci    name_dsc.dsc$a_pointer = (char *)logname;
12813498266Sopenharmony_ci    name_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
12913498266Sopenharmony_ci    name_dsc.dsc$b_class = DSC$K_CLASS_S;
13013498266Sopenharmony_ci
13113498266Sopenharmony_ci    status = SYS$TRNLNM(&attr, &table_dsc, &name_dsc, 0, itlst);
13213498266Sopenharmony_ci
13313498266Sopenharmony_ci    if($VMS_STATUS_SUCCESS(status)) {
13413498266Sopenharmony_ci
13513498266Sopenharmony_ci         /* Null terminate and return the string */
13613498266Sopenharmony_ci        /*--------------------------------------*/
13713498266Sopenharmony_ci        value[result] = '\0';
13813498266Sopenharmony_ci    }
13913498266Sopenharmony_ci
14013498266Sopenharmony_ci    return status;
14113498266Sopenharmony_ci}
14213498266Sopenharmony_ci
14313498266Sopenharmony_ci/* How to simply create a logical name */
14413498266Sopenharmony_cistatic int sys_crelnm
14513498266Sopenharmony_ci   (const char * logname,
14613498266Sopenharmony_ci    const char * value)
14713498266Sopenharmony_ci{
14813498266Sopenharmony_ci    int ret_val;
14913498266Sopenharmony_ci    const char * proc_table = "LNM$PROCESS_TABLE";
15013498266Sopenharmony_ci    struct dsc$descriptor_s proc_table_dsc;
15113498266Sopenharmony_ci    struct dsc$descriptor_s logname_dsc;
15213498266Sopenharmony_ci    struct itmlst_3 item_list[2];
15313498266Sopenharmony_ci
15413498266Sopenharmony_ci    proc_table_dsc.dsc$a_pointer = (char *) proc_table;
15513498266Sopenharmony_ci    proc_table_dsc.dsc$w_length = strlen(proc_table);
15613498266Sopenharmony_ci    proc_table_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
15713498266Sopenharmony_ci    proc_table_dsc.dsc$b_class = DSC$K_CLASS_S;
15813498266Sopenharmony_ci
15913498266Sopenharmony_ci    logname_dsc.dsc$a_pointer = (char *) logname;
16013498266Sopenharmony_ci    logname_dsc.dsc$w_length = strlen(logname);
16113498266Sopenharmony_ci    logname_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
16213498266Sopenharmony_ci    logname_dsc.dsc$b_class = DSC$K_CLASS_S;
16313498266Sopenharmony_ci
16413498266Sopenharmony_ci    item_list[0].buflen = strlen(value);
16513498266Sopenharmony_ci    item_list[0].itmcode = LNM$_STRING;
16613498266Sopenharmony_ci    item_list[0].bufadr = (char *)value;
16713498266Sopenharmony_ci    item_list[0].retlen = NULL;
16813498266Sopenharmony_ci
16913498266Sopenharmony_ci    item_list[1].buflen = 0;
17013498266Sopenharmony_ci    item_list[1].itmcode = 0;
17113498266Sopenharmony_ci
17213498266Sopenharmony_ci    ret_val = SYS$CRELNM(NULL, &proc_table_dsc, &logname_dsc, NULL, item_list);
17313498266Sopenharmony_ci
17413498266Sopenharmony_ci    return ret_val;
17513498266Sopenharmony_ci}
17613498266Sopenharmony_ci
17713498266Sopenharmony_ci
17813498266Sopenharmony_ci /* Start of DECC RTL Feature handling */
17913498266Sopenharmony_ci
18013498266Sopenharmony_ci/*
18113498266Sopenharmony_ci** Sets default value for a feature
18213498266Sopenharmony_ci*/
18313498266Sopenharmony_ci#ifdef __VAX
18413498266Sopenharmony_cistatic void set_feature_default(const char *name, const char *value)
18513498266Sopenharmony_ci{
18613498266Sopenharmony_ci    sys_crelnm(name, value);
18713498266Sopenharmony_ci}
18813498266Sopenharmony_ci#else
18913498266Sopenharmony_cistatic void set_feature_default(const char *name, int value)
19013498266Sopenharmony_ci{
19113498266Sopenharmony_ci    int index;
19213498266Sopenharmony_ci
19313498266Sopenharmony_ci    index = decc$feature_get_index(name);
19413498266Sopenharmony_ci
19513498266Sopenharmony_ci    if(index > 0)
19613498266Sopenharmony_ci        decc$feature_set_value (index, 0, value);
19713498266Sopenharmony_ci}
19813498266Sopenharmony_ci#endif
19913498266Sopenharmony_ci
20013498266Sopenharmony_cistatic void set_features(void)
20113498266Sopenharmony_ci{
20213498266Sopenharmony_ci    int status;
20313498266Sopenharmony_ci    char unix_shell_name[255];
20413498266Sopenharmony_ci    int use_unix_settings = 1;
20513498266Sopenharmony_ci
20613498266Sopenharmony_ci    status = sys_trnlnm("GNV$UNIX_SHELL",
20713498266Sopenharmony_ci                        unix_shell_name, sizeof unix_shell_name -1);
20813498266Sopenharmony_ci    if(!$VMS_STATUS_SUCCESS(status)) {
20913498266Sopenharmony_ci        use_unix_settings = 0;
21013498266Sopenharmony_ci    }
21113498266Sopenharmony_ci
21213498266Sopenharmony_ci    /* ACCESS should check ACLs or it is lying. */
21313498266Sopenharmony_ci    set_feature_default("DECC$ACL_ACCESS_CHECK", ENABLE);
21413498266Sopenharmony_ci
21513498266Sopenharmony_ci    /* We always want the new parse style */
21613498266Sopenharmony_ci    set_feature_default ("DECC$ARGV_PARSE_STYLE" , ENABLE);
21713498266Sopenharmony_ci
21813498266Sopenharmony_ci
21913498266Sopenharmony_ci    /* Unless we are in POSIX compliant mode, we want the old POSIX root
22013498266Sopenharmony_ci     * enabled.
22113498266Sopenharmony_ci     */
22213498266Sopenharmony_ci    set_feature_default("DECC$DISABLE_POSIX_ROOT", DISABLE);
22313498266Sopenharmony_ci
22413498266Sopenharmony_ci    /* EFS charset, means UTF-8 support */
22513498266Sopenharmony_ci    /* VTF-7 support is controlled by a feature setting called UTF8 */
22613498266Sopenharmony_ci    set_feature_default ("DECC$EFS_CHARSET", ENABLE);
22713498266Sopenharmony_ci    set_feature_default ("DECC$EFS_CASE_PRESERVE", ENABLE);
22813498266Sopenharmony_ci
22913498266Sopenharmony_ci    /* Support timestamps when available */
23013498266Sopenharmony_ci    set_feature_default ("DECC$EFS_FILE_TIMESTAMPS", ENABLE);
23113498266Sopenharmony_ci
23213498266Sopenharmony_ci    /* Cache environment variables - performance improvements */
23313498266Sopenharmony_ci    set_feature_default ("DECC$ENABLE_GETENV_CACHE", ENABLE);
23413498266Sopenharmony_ci
23513498266Sopenharmony_ci    /* Start out with new file attribute inheritance */
23613498266Sopenharmony_ci#ifdef __VAX
23713498266Sopenharmony_ci    set_feature_default ("DECC$EXEC_FILEATTR_INHERITANCE", "2");
23813498266Sopenharmony_ci#else
23913498266Sopenharmony_ci    set_feature_default ("DECC$EXEC_FILEATTR_INHERITANCE", 2);
24013498266Sopenharmony_ci#endif
24113498266Sopenharmony_ci
24213498266Sopenharmony_ci    /* Don't display trailing dot after files without type */
24313498266Sopenharmony_ci    set_feature_default ("DECC$READDIR_DROPDOTNOTYPE", ENABLE);
24413498266Sopenharmony_ci
24513498266Sopenharmony_ci    /* For standard output channels buffer output until terminator */
24613498266Sopenharmony_ci    /* Gets rid of output logs with single character lines in them. */
24713498266Sopenharmony_ci    set_feature_default ("DECC$STDIO_CTX_EOL", ENABLE);
24813498266Sopenharmony_ci
24913498266Sopenharmony_ci    /* Fix mv aa.bb aa  */
25013498266Sopenharmony_ci    set_feature_default ("DECC$RENAME_NO_INHERIT", ENABLE);
25113498266Sopenharmony_ci
25213498266Sopenharmony_ci    if(use_unix_settings) {
25313498266Sopenharmony_ci
25413498266Sopenharmony_ci        /* POSIX requires that open files be able to be removed */
25513498266Sopenharmony_ci        set_feature_default ("DECC$ALLOW_REMOVE_OPEN_FILES", ENABLE);
25613498266Sopenharmony_ci
25713498266Sopenharmony_ci        /* Default to outputting Unix filenames in VMS routines */
25813498266Sopenharmony_ci        set_feature_default ("DECC$FILENAME_UNIX_ONLY", ENABLE);
25913498266Sopenharmony_ci        /* FILENAME_UNIX_ONLY Implicitly sets */
26013498266Sopenharmony_ci        /* decc$disable_to_vms_logname_translation */
26113498266Sopenharmony_ci
26213498266Sopenharmony_ci        set_feature_default ("DECC$FILE_PERMISSION_UNIX", ENABLE);
26313498266Sopenharmony_ci
26413498266Sopenharmony_ci        set_feature_default ("DECC$FILE_SHARING", ENABLE);
26513498266Sopenharmony_ci
26613498266Sopenharmony_ci        set_feature_default ("DECC$FILE_OWNER_UNIX", ENABLE);
26713498266Sopenharmony_ci        set_feature_default ("DECC$POSIX_SEEK_STREAM_FILE", ENABLE);
26813498266Sopenharmony_ci
26913498266Sopenharmony_ci    } else {
27013498266Sopenharmony_ci        set_feature_default("DECC$FILENAME_UNIX_REPORT", ENABLE);
27113498266Sopenharmony_ci    }
27213498266Sopenharmony_ci
27313498266Sopenharmony_ci    /* When reporting Unix filenames, glob the same way */
27413498266Sopenharmony_ci    set_feature_default ("DECC$GLOB_UNIX_STYLE", ENABLE);
27513498266Sopenharmony_ci
27613498266Sopenharmony_ci    /* The VMS version numbers on Unix filenames is incompatible with most */
27713498266Sopenharmony_ci    /* ported packages. */
27813498266Sopenharmony_ci    set_feature_default("DECC$FILENAME_UNIX_NO_VERSION", ENABLE);
27913498266Sopenharmony_ci
28013498266Sopenharmony_ci    /* The VMS version numbers on Unix filenames is incompatible with most */
28113498266Sopenharmony_ci    /* ported packages. */
28213498266Sopenharmony_ci    set_feature_default("DECC$UNIX_PATH_BEFORE_LOGNAME", ENABLE);
28313498266Sopenharmony_ci
28413498266Sopenharmony_ci    /* Set strtol to proper behavior */
28513498266Sopenharmony_ci    set_feature_default("DECC$STRTOL_ERANGE", ENABLE);
28613498266Sopenharmony_ci
28713498266Sopenharmony_ci    /* Commented here to prevent future bugs:  A program or user should */
28813498266Sopenharmony_ci    /* never ever enable DECC$POSIX_STYLE_UID. */
28913498266Sopenharmony_ci    /* It will probably break all code that accesses UIDs */
29013498266Sopenharmony_ci    /*  do_not_set_default ("DECC$POSIX_STYLE_UID", TRUE); */
29113498266Sopenharmony_ci}
29213498266Sopenharmony_ci
29313498266Sopenharmony_ci
29413498266Sopenharmony_ci/* Some boilerplate to force this to be a proper LIB$INITIALIZE section */
29513498266Sopenharmony_ci
29613498266Sopenharmony_ci#pragma nostandard
29713498266Sopenharmony_ci#pragma extern_model save
29813498266Sopenharmony_ci#ifdef __VAX
29913498266Sopenharmony_ci#pragma extern_model strict_refdef "LIB$INITIALIZE" nowrt, long, nopic
30013498266Sopenharmony_ci#else
30113498266Sopenharmony_ci#pragma extern_model strict_refdef "LIB$INITIALIZE" nowrt, long
30213498266Sopenharmony_ci#    if __INITIAL_POINTER_SIZE
30313498266Sopenharmony_ci#        pragma __pointer_size __save
30413498266Sopenharmony_ci#        pragma __pointer_size 32
30513498266Sopenharmony_ci#    else
30613498266Sopenharmony_ci#        pragma __required_pointer_size __save
30713498266Sopenharmony_ci#        pragma __required_pointer_size 32
30813498266Sopenharmony_ci#    endif
30913498266Sopenharmony_ci#endif
31013498266Sopenharmony_ci/* Set our contribution to the LIB$INITIALIZE array */
31113498266Sopenharmony_civoid (* const iniarray[])(void) = {set_features, } ;
31213498266Sopenharmony_ci#ifndef __VAX
31313498266Sopenharmony_ci#    if __INITIAL_POINTER_SIZE
31413498266Sopenharmony_ci#        pragma __pointer_size __restore
31513498266Sopenharmony_ci#    else
31613498266Sopenharmony_ci#        pragma __required_pointer_size __restore
31713498266Sopenharmony_ci#    endif
31813498266Sopenharmony_ci#endif
31913498266Sopenharmony_ci
32013498266Sopenharmony_ci
32113498266Sopenharmony_ci/*
32213498266Sopenharmony_ci** Force a reference to LIB$INITIALIZE to ensure it
32313498266Sopenharmony_ci** exists in the image.
32413498266Sopenharmony_ci*/
32513498266Sopenharmony_ciint LIB$INITIALIZE(void);
32613498266Sopenharmony_ci#ifdef __DECC
32713498266Sopenharmony_ci#pragma extern_model strict_refdef
32813498266Sopenharmony_ci#endif
32913498266Sopenharmony_ci    int lib_init_ref = (int) LIB$INITIALIZE;
33013498266Sopenharmony_ci#ifdef __DECC
33113498266Sopenharmony_ci#pragma extern_model restore
33213498266Sopenharmony_ci#pragma standard
33313498266Sopenharmony_ci#endif
334