curlmain.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. *
  24. ***************************************************************************/
  25. /*
  26. * QADRT/QADRTMAIN2 substitution program.
  27. * This is needed because the IBM-provided QADRTMAIN2 does not
  28. * properly translate arguments by default or if no locale is provided.
  29. */
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <iconv.h>
  33. #include <errno.h>
  34. #include <locale.h>
  35. /* Do not use qadrt.h since it defines unneeded static procedures. */
  36. extern void QadrtInit(void);
  37. extern int QadrtFreeConversionTable(void);
  38. extern int QadrtFreeEnviron(void);
  39. extern char * setlocale_a(int, const char *);
  40. /* The ASCII main program. */
  41. extern int main_a(int argc, char * * argv);
  42. /* Global values of original EBCDIC arguments. */
  43. int ebcdic_argc;
  44. char ** ebcdic_argv;
  45. int main(int argc, char **argv)
  46. {
  47. int i;
  48. int j;
  49. iconv_t cd;
  50. size_t bytecount = 0;
  51. char *inbuf;
  52. char *outbuf;
  53. size_t inbytesleft;
  54. size_t outbytesleft;
  55. char dummybuf[128];
  56. char tocode[32];
  57. char fromcode[32];
  58. ebcdic_argc = argc;
  59. ebcdic_argv = argv;
  60. /* Build the encoding converter. */
  61. strncpy(tocode, "IBMCCSID01208", sizeof(tocode)); /* Use UTF-8. */
  62. strncpy(fromcode, "IBMCCSID000000000010", sizeof(fromcode));
  63. cd = iconv_open(tocode, fromcode);
  64. /* Measure the arguments. */
  65. for(i = 0; i < argc; i++) {
  66. inbuf = argv[i];
  67. do {
  68. inbytesleft = 0;
  69. outbuf = dummybuf;
  70. outbytesleft = sizeof(dummybuf);
  71. j = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
  72. bytecount += outbuf - dummybuf;
  73. } while(j == -1 && errno == E2BIG);
  74. /* Reset the shift state. */
  75. iconv(cd, NULL, &inbytesleft, &outbuf, &outbytesleft);
  76. }
  77. /* Allocate memory for the ASCII arguments and vector. */
  78. argv = (char **) malloc((argc + 1) * sizeof(*argv) + bytecount);
  79. /* Build the vector and convert argument encoding. */
  80. outbuf = (char *) (argv + argc + 1);
  81. outbytesleft = bytecount;
  82. for(i = 0; i < argc; i++) {
  83. argv[i] = outbuf;
  84. inbuf = ebcdic_argv[i];
  85. inbytesleft = 0;
  86. iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
  87. iconv(cd, NULL, &inbytesleft, &outbuf, &outbytesleft);
  88. }
  89. iconv_close(cd);
  90. argv[argc] = NULL;
  91. /* Try setting the locale regardless of QADRT_ENV_LOCALE. */
  92. setlocale_a(LC_ALL, "");
  93. /* Call the program. */
  94. i = main_a(argc, argv);
  95. /* Clean-up allocated items. */
  96. free((char *) argv);
  97. QadrtFreeConversionTable();
  98. QadrtFreeEnviron();
  99. /* Terminate. */
  100. return i;
  101. }