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 ***************************************************************************/
2513498266Sopenharmony_ci
2613498266Sopenharmony_ci/*
2713498266Sopenharmony_ci * QADRT/QADRTMAIN2 substitution program.
2813498266Sopenharmony_ci * This is needed because the IBM-provided QADRTMAIN2 does not
2913498266Sopenharmony_ci * properly translate arguments by default or if no locale is provided.
3013498266Sopenharmony_ci */
3113498266Sopenharmony_ci
3213498266Sopenharmony_ci#include <stdlib.h>
3313498266Sopenharmony_ci#include <string.h>
3413498266Sopenharmony_ci#include <iconv.h>
3513498266Sopenharmony_ci#include <errno.h>
3613498266Sopenharmony_ci#include <locale.h>
3713498266Sopenharmony_ci
3813498266Sopenharmony_ci/* Do not use qadrt.h since it defines unneeded static procedures. */
3913498266Sopenharmony_ciextern void     QadrtInit(void);
4013498266Sopenharmony_ciextern int      QadrtFreeConversionTable(void);
4113498266Sopenharmony_ciextern int      QadrtFreeEnviron(void);
4213498266Sopenharmony_ciextern char *   setlocale_a(int, const char *);
4313498266Sopenharmony_ci
4413498266Sopenharmony_ci
4513498266Sopenharmony_ci/* The ASCII main program. */
4613498266Sopenharmony_ciextern int      main_a(int argc, char * * argv);
4713498266Sopenharmony_ci
4813498266Sopenharmony_ci/* Global values of original EBCDIC arguments. */
4913498266Sopenharmony_ciint             ebcdic_argc;
5013498266Sopenharmony_cichar **         ebcdic_argv;
5113498266Sopenharmony_ci
5213498266Sopenharmony_ci
5313498266Sopenharmony_ciint main(int argc, char **argv)
5413498266Sopenharmony_ci{
5513498266Sopenharmony_ci  int i;
5613498266Sopenharmony_ci  int j;
5713498266Sopenharmony_ci  iconv_t cd;
5813498266Sopenharmony_ci  size_t bytecount = 0;
5913498266Sopenharmony_ci  char *inbuf;
6013498266Sopenharmony_ci  char *outbuf;
6113498266Sopenharmony_ci  size_t inbytesleft;
6213498266Sopenharmony_ci  size_t outbytesleft;
6313498266Sopenharmony_ci  char dummybuf[128];
6413498266Sopenharmony_ci  char tocode[32];
6513498266Sopenharmony_ci  char fromcode[32];
6613498266Sopenharmony_ci
6713498266Sopenharmony_ci  ebcdic_argc = argc;
6813498266Sopenharmony_ci  ebcdic_argv = argv;
6913498266Sopenharmony_ci
7013498266Sopenharmony_ci  /* Build the encoding converter. */
7113498266Sopenharmony_ci  strncpy(tocode, "IBMCCSID01208", sizeof(tocode));      /* Use UTF-8. */
7213498266Sopenharmony_ci  strncpy(fromcode, "IBMCCSID000000000010", sizeof(fromcode));
7313498266Sopenharmony_ci  cd = iconv_open(tocode, fromcode);
7413498266Sopenharmony_ci
7513498266Sopenharmony_ci  /* Measure the arguments. */
7613498266Sopenharmony_ci  for(i = 0; i < argc; i++) {
7713498266Sopenharmony_ci    inbuf = argv[i];
7813498266Sopenharmony_ci    do {
7913498266Sopenharmony_ci      inbytesleft = 0;
8013498266Sopenharmony_ci      outbuf = dummybuf;
8113498266Sopenharmony_ci      outbytesleft = sizeof(dummybuf);
8213498266Sopenharmony_ci      j = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
8313498266Sopenharmony_ci      bytecount += outbuf - dummybuf;
8413498266Sopenharmony_ci    } while(j == -1 && errno == E2BIG);
8513498266Sopenharmony_ci
8613498266Sopenharmony_ci    /* Reset the shift state. */
8713498266Sopenharmony_ci    iconv(cd, NULL, &inbytesleft, &outbuf, &outbytesleft);
8813498266Sopenharmony_ci   }
8913498266Sopenharmony_ci
9013498266Sopenharmony_ci  /* Allocate memory for the ASCII arguments and vector. */
9113498266Sopenharmony_ci  argv = (char **) malloc((argc + 1) * sizeof(*argv) + bytecount);
9213498266Sopenharmony_ci
9313498266Sopenharmony_ci  /* Build the vector and convert argument encoding. */
9413498266Sopenharmony_ci  outbuf = (char *) (argv + argc + 1);
9513498266Sopenharmony_ci  outbytesleft = bytecount;
9613498266Sopenharmony_ci
9713498266Sopenharmony_ci  for(i = 0; i < argc; i++) {
9813498266Sopenharmony_ci    argv[i] = outbuf;
9913498266Sopenharmony_ci    inbuf = ebcdic_argv[i];
10013498266Sopenharmony_ci    inbytesleft = 0;
10113498266Sopenharmony_ci    iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
10213498266Sopenharmony_ci    iconv(cd, NULL, &inbytesleft, &outbuf, &outbytesleft);
10313498266Sopenharmony_ci  }
10413498266Sopenharmony_ci
10513498266Sopenharmony_ci  iconv_close(cd);
10613498266Sopenharmony_ci  argv[argc] = NULL;
10713498266Sopenharmony_ci
10813498266Sopenharmony_ci  /* Try setting the locale regardless of QADRT_ENV_LOCALE. */
10913498266Sopenharmony_ci  setlocale_a(LC_ALL, "");
11013498266Sopenharmony_ci
11113498266Sopenharmony_ci  /* Call the program. */
11213498266Sopenharmony_ci  i = main_a(argc, argv);
11313498266Sopenharmony_ci
11413498266Sopenharmony_ci  /* Clean-up allocated items. */
11513498266Sopenharmony_ci  free((char *) argv);
11613498266Sopenharmony_ci  QadrtFreeConversionTable();
11713498266Sopenharmony_ci  QadrtFreeEnviron();
11813498266Sopenharmony_ci
11913498266Sopenharmony_ci  /* Terminate. */
12013498266Sopenharmony_ci  return i;
12113498266Sopenharmony_ci}
122