xref: /curl/packages/OS400/curlmain.c (revision 8c62479a)
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   const char *tocode = "IBMCCSID01208"; /* Use UTF-8. */
65   const char *fromcode = "IBMCCSID000000000010";
66 
67   ebcdic_argc = argc;
68   ebcdic_argv = argv;
69 
70   /* Build the encoding converter. */
71   cd = iconv_open(tocode, fromcode);
72 
73   /* Measure the arguments. */
74   for(i = 0; i < argc; i++) {
75     inbuf = argv[i];
76     do {
77       inbytesleft = 0;
78       outbuf = dummybuf;
79       outbytesleft = sizeof(dummybuf);
80       j = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
81       bytecount += outbuf - dummybuf;
82     } while(j == -1 && errno == E2BIG);
83 
84     /* Reset the shift state. */
85     iconv(cd, NULL, &inbytesleft, &outbuf, &outbytesleft);
86    }
87 
88   /* Allocate memory for the ASCII arguments and vector. */
89   argv = (char **) malloc((argc + 1) * sizeof(*argv) + bytecount);
90 
91   /* Build the vector and convert argument encoding. */
92   outbuf = (char *) (argv + argc + 1);
93   outbytesleft = bytecount;
94 
95   for(i = 0; i < argc; i++) {
96     argv[i] = outbuf;
97     inbuf = ebcdic_argv[i];
98     inbytesleft = 0;
99     iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
100     iconv(cd, NULL, &inbytesleft, &outbuf, &outbytesleft);
101   }
102 
103   iconv_close(cd);
104   argv[argc] = NULL;
105 
106   /* Try setting the locale regardless of QADRT_ENV_LOCALE. */
107   setlocale_a(LC_ALL, "");
108 
109   /* Call the program. */
110   i = main_a(argc, argv);
111 
112   /* Clean-up allocated items. */
113   free((char *) argv);
114   QadrtFreeConversionTable();
115   QadrtFreeEnviron();
116 
117   /* Terminate. */
118   return i;
119 }
120