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 /*
27 * QADRT/QADRTMAIN2 substitution program.
28 * This is needed because the IBM-provided QADRTMAIN2 does not
29 * properly translate arguments by default or if no locale is provided.
30 */
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include <iconv.h>
35 #include <errno.h>
36 #include <locale.h>
37
38 /* Do not use qadrt.h since it defines unneeded static procedures. */
39 extern void QadrtInit(void);
40 extern int QadrtFreeConversionTable(void);
41 extern int QadrtFreeEnviron(void);
42 extern char * setlocale_a(int, const char *);
43
44
45 /* The ASCII main program. */
46 extern int main_a(int argc, char * * argv);
47
48 /* Global values of original EBCDIC arguments. */
49 int ebcdic_argc;
50 char ** ebcdic_argv;
51
52
main(int argc,char ** argv)53 int main(int argc, char **argv)
54 {
55 int i;
56 int j;
57 iconv_t cd;
58 size_t bytecount = 0;
59 char *inbuf;
60 char *outbuf;
61 size_t inbytesleft;
62 size_t outbytesleft;
63 char dummybuf[128];
64 char tocode[32];
65 char fromcode[32];
66
67 ebcdic_argc = argc;
68 ebcdic_argv = argv;
69
70 /* Build the encoding converter. */
71 strncpy(tocode, "IBMCCSID01208", sizeof(tocode)); /* Use UTF-8. */
72 strncpy(fromcode, "IBMCCSID000000000010", sizeof(fromcode));
73 cd = iconv_open(tocode, fromcode);
74
75 /* Measure the arguments. */
76 for(i = 0; i < argc; i++) {
77 inbuf = argv[i];
78 do {
79 inbytesleft = 0;
80 outbuf = dummybuf;
81 outbytesleft = sizeof(dummybuf);
82 j = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
83 bytecount += outbuf - dummybuf;
84 } while(j == -1 && errno == E2BIG);
85
86 /* Reset the shift state. */
87 iconv(cd, NULL, &inbytesleft, &outbuf, &outbytesleft);
88 }
89
90 /* Allocate memory for the ASCII arguments and vector. */
91 argv = (char **) malloc((argc + 1) * sizeof(*argv) + bytecount);
92
93 /* Build the vector and convert argument encoding. */
94 outbuf = (char *) (argv + argc + 1);
95 outbytesleft = bytecount;
96
97 for(i = 0; i < argc; i++) {
98 argv[i] = outbuf;
99 inbuf = ebcdic_argv[i];
100 inbytesleft = 0;
101 iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
102 iconv(cd, NULL, &inbytesleft, &outbuf, &outbytesleft);
103 }
104
105 iconv_close(cd);
106 argv[argc] = NULL;
107
108 /* Try setting the locale regardless of QADRT_ENV_LOCALE. */
109 setlocale_a(LC_ALL, "");
110
111 /* Call the program. */
112 i = main_a(argc, argv);
113
114 /* Clean-up allocated items. */
115 free((char *) argv);
116 QadrtFreeConversionTable();
117 QadrtFreeEnviron();
118
119 /* Terminate. */
120 return i;
121 }
122