Skip to content

Instantly share code, notes, and snippets.

@lopex

lopex/foo.c Secret

Created December 7, 2018 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lopex/b44742fc8879726a0696df7856520f19 to your computer and use it in GitHub Desktop.
Save lopex/b44742fc8879726a0696df7856520f19 to your computer and use it in GitHub Desktop.
// CRTBNDC PGM(CBATCH/cblib) SRCSTMF('/cbatch/cblib.c') OUTPUT(*print) DBGVIEW(*ALL) REPLACE(*YES)
// CRTBNDC PGM(CBATCH/cblib) SRCSTMF('/cbatch/cblib.c') OUTPUT(*print) DBGVIEW(*ALL) REPLACE(*YES) LOCALETYPE(*CDL)
// CALL PGM(CBATCH/cblib) PARM(2A)
// isdigit, atoi, atof, isalpha, strtok, strtod, strtol
// isxdigit, isalnum
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>
#include <ctype.h>
#include <iconv.h>
#include <errno.h>
#pragma convert(1208)
static unsigned char __a2e__[256] = {
0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 37, 11, 12, 13, 14, 15,
16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31,
64, 79,127,123, 91,108, 80,125, 77, 93, 92, 78,107, 96, 75, 97,
240,241,242,243,244,245,246,247,248,249,122, 94, 76,126,110,111,
124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214,
215,216,217,226,227,228,229,230,231,232,233, 74,224, 90, 95,109,
121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150,
151,152,153,162,163,164,165,166,167,168,169,192,106,208,161, 7,
32, 33, 34, 35, 36, 21, 6, 23, 40, 41, 42, 43, 44, 9, 10, 27,
48, 49, 26, 51, 52, 53, 54, 8, 56, 57, 58, 59, 4, 20, 62,225,
65, 66, 67, 68, 69, 70, 71, 72, 73, 81, 82, 83, 84, 85, 86, 87,
88, 89, 98, 99,100,101,102,103,104,105,112,113,114,115,116,117,
118,119,120,128,138,139,140,141,142,143,144,154,155,156,157,158,
159,160,170,171,172,173,174,175,176,177,178,179,180,181,182,183,
184,185,186,187,188,189,190,191,202,203,204,205,206,207,218,219,
220,221,222,223,234,235,236,237,238,239,250,251,252,253,254,255
};
#define CONVERT_NAME_SIZE 33
#define CONVERT_ARG_SIZE 1000
#define CONVERT_ARG_MAX 20
char* buffer_alloc(size_t size) {
return malloc(size);
}
iconv_t custom_iconv_open(char* from, char* to) {
iconv_t iconv;
char from_[CONVERT_NAME_SIZE];
char to_[CONVERT_NAME_SIZE];
memset(from_, 0, CONVERT_NAME_SIZE);
memset(to_, 0, CONVERT_NAME_SIZE);
strcpy(from_, from);
strcpy(to_, to);
iconv = iconv_open(to_, from_);
if (iconv.return_value == -1) {
#pragma convert(870)
printf("iconv_open for %s to %s failed, errno=%d\n", from_, to_, errno);
#pragma convert(1208)
exit(errno);
}
return iconv;
}
size_t custom_iconv(iconv_t ciconv, char *inbuf, size_t inbytesleft, char *outbuf, size_t outbytesleft) {
int result = iconv(ciconv, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
if (result == -1) {
#pragma convert(870)
printf("conversion failed:\n");
exit(result);
#pragma convert(1208)
}
return result;
}
const char *ebcdic_to_ascii(char* from) {
static int ebcdic_to_ascii_init = 0;
static iconv_t ebcdic_to_ascii;
//char to[CONVERT_ARG_SIZE];
//memset(to, 0, CONVERT_ARG_SIZE);
int to_length = strlen(from) * 2;
char*to = buffer_alloc(to_length + 1);
if (ebcdic_to_ascii_init == 0) {
#pragma convert(870)
ebcdic_to_ascii = custom_iconv_open("IBMCCSID008700000000", "IBMCCSID01208");
#pragma convert(1208)
ebcdic_to_ascii_init = 1;
}
int result = custom_iconv(ebcdic_to_ascii, from, strlen(from), to, to_length);
return to;
}
const char *ascii_to_ebcdic(char* from) {
static int ascii_to_ebcdic_init = 0;
static iconv_t ascii_to_ebcdic;
//char to[CONVERT_ARG_SIZE];
//memset(to, 0, CONVERT_ARG_SIZE);
int to_length = strlen(from) * 2;
char*to = buffer_alloc(to_length + 1);
if (ascii_to_ebcdic_init == 0) {
#pragma convert(870)
ascii_to_ebcdic = custom_iconv_open("IBMCCSID012080000000", "IBMCCSID00870");
#pragma convert(1208)
ascii_to_ebcdic_init = 1;
}
int result = custom_iconv(ascii_to_ebcdic, from, strlen(from), to, to_length);
return to;
}
int convert_args(int argc, char const *argv[]) {
for (int i = 1; i < argc; i++) {
argv[i] = ebcdic_to_ascii((char*)argv[i]);
}
}
int ascii_isdigit(int c) {
return isdigit(__a2e__[c]);
}
int ascii_isalpha(int c) {
return isalpha(__a2e__[c]);
}
int ascii_atoi(char* str) {
return atoi(ascii_to_ebcdic(str));
}
double ascii_atof(char* str) {
return atof(ascii_to_ebcdic(str));
}
char* ascii_strtok(char* str, const char* delimiters) {
return strtok((char*)ascii_to_ebcdic(str), ascii_to_ebcdic((char*)delimiters));
}
double ascii_strtod(const char* str, char** endptr) {
return strtod(ascii_to_ebcdic((char*)str), endptr);
}
long int ascii_strtol(const char* str, char** endptr, int base) {
return strtol(ascii_to_ebcdic((char*)str), endptr, base);
}
int main(int argc, char const *argv[]) {
#pragma convert(870)
printf("arg pre trans: '%s'\n", argv[1]);
convert_args(argc, argv);
printf("arg post trans: '%s'\n", ascii_to_ebcdic((char*)argv[1]));
#pragma convert(1208)
int atoi_result = ascii_atoi("123");
double atof_result = ascii_atof("1.23");
int isdigit_result = ascii_isdigit("a"[0]);
int isdigit_result2 = ascii_isdigit("1"[0]);
int isalpha_result = ascii_isalpha("x"[0]);
int isalpha_result2 = ascii_isalpha("1"[0]);
char*strtok_result = ascii_strtok("abc;def", ";");
char**strtod_rest;
double strtod_result = ascii_strtod("107141842", strtod_rest);
char**strtol_rest;
long int strtol_result = ascii_strtol("2001", strtol_rest, 10);
#pragma convert(870)
printf("atoi result: %d\n", atoi_result);
printf("atof result: %lf\n", atof_result);
printf("isdigit result: %d\n", isdigit_result);
printf("isdigit result2: %d\n", isdigit_result2);
printf("isalpha result: %d\n", isalpha_result);
printf("isalpha result2: %d\n", isalpha_result2);
printf("strtok result: '%s'\n", strtok_result);
printf("strtod result: '%.2f', rest: '%s'\n", strtod_result, strtod_rest);
printf("strtol result: '%ld', rest: '%s'\n", strtol_result, strtol_rest);
#pragma convert(1208)
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment