/***************************************************************************1* _ _ ____ _2* Project ___| | | | _ \| |3* / __| | | | |_) | |4* | (__| |_| | _ <| |___5* \___|\___/|_| \_\_____|6*7* Copyright (C) Daniel Stenberg, <[email protected]>, et al.8*9* This software is licensed as described in the file COPYING, which10* you should have received as part of this distribution. The terms11* are also available at https://curl.se/docs/copyright.html.12*13* You may opt to use, copy, modify, merge, publish, distribute and/or sell14* copies of the Software, and permit persons to whom the Software is15* furnished to do so, under the terms of the COPYING file.16*17* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY18* KIND, either express or implied.19*20* SPDX-License-Identifier: curl21*22*23***************************************************************************/2425/*26* QADRT/QADRTMAIN2 substitution program.27* This is needed because the IBM-provided QADRTMAIN2 does not28* properly translate arguments by default or if no locale is provided.29*/3031#include <stdlib.h>32#include <string.h>33#include <iconv.h>34#include <errno.h>35#include <locale.h>3637/* Do not use qadrt.h since it defines unneeded static procedures. */38extern void QadrtInit(void);39extern int QadrtFreeConversionTable(void);40extern int QadrtFreeEnviron(void);41extern char * setlocale_a(int, const char *);424344/* The ASCII main program. */45extern int main_a(int argc, char * * argv);4647/* Global values of original EBCDIC arguments. */48int ebcdic_argc;49char ** ebcdic_argv;505152int main(int argc, char **argv)53{54int i;55int j;56iconv_t cd;57size_t bytecount = 0;58char *inbuf;59char *outbuf;60size_t inbytesleft;61size_t outbytesleft;62char dummybuf[128];63/* To/From codes are 32 byte long strings with64reserved fields initialized to ZEROs */65const char tocode[32] = {"IBMCCSID01208"}; /* Use UTF-8. */66const char fromcode[32] = {"IBMCCSID000000000010"};6768ebcdic_argc = argc;69ebcdic_argv = argv;7071/* Build the encoding converter. */72cd = iconv_open(tocode, fromcode);7374/* Measure the arguments. */75for(i = 0; i < argc; i++) {76inbuf = argv[i];77do {78inbytesleft = 0;79outbuf = dummybuf;80outbytesleft = sizeof(dummybuf);81j = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);82bytecount += outbuf - dummybuf;83} while(j == -1 && errno == E2BIG);8485/* Reset the shift state. */86iconv(cd, NULL, &inbytesleft, &outbuf, &outbytesleft);87}8889/* Allocate memory for the ASCII arguments and vector. */90argv = (char **) malloc((argc + 1) * sizeof(*argv) + bytecount);9192/* Build the vector and convert argument encoding. */93outbuf = (char *) (argv + argc + 1);94outbytesleft = bytecount;9596for(i = 0; i < argc; i++) {97argv[i] = outbuf;98inbuf = ebcdic_argv[i];99inbytesleft = 0;100iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);101iconv(cd, NULL, &inbytesleft, &outbuf, &outbytesleft);102}103104iconv_close(cd);105argv[argc] = NULL;106107/* Try setting the locale regardless of QADRT_ENV_LOCALE. */108setlocale_a(LC_ALL, "");109110/* Call the program. */111i = main_a(argc, argv);112113/* Clean-up allocated items. */114free((char *) argv);115QadrtFreeConversionTable();116QadrtFreeEnviron();117118/* Terminate. */119return i;120}121122123