#include /* C-stdio library. */ #include /* string functions. */ #include /* stdlib functions. */ #include /* errno values. */ #include /* for QzsrPutEnvCCSID/QzsrGetEnvCCSID */ /* Declare the struct that captures errors in QzsrPutEnvCCSID/QzsrGetEnvCCSID */ typedef struct { int BytesProv; int BytesAvail; char ExceptID[7]; char Reserved; char ExecptData[256]; } ErrT; #define BUFLEN 50 /**********************************************************************/ /* */ /* Function Name: main() */ /* */ /* Descriptive Name: A sample CGI program for iSeries ILE/C to */ /* generate a UTF-8 page to be used with CGIConvMode BINARY. */ /* */ /* HTTP Server Environment variables: */ /* ---------------------------------- */ /* The C function call,QzsrGetEnvCCSID, is used to read i5/OS */ /* environment variables, specifying a CCSID for returning the value. */ /* The value of the environment variable is always returned as a */ /* string pointer. */ /* */ /* Standard Output: */ /* ---------------- */ /* Standard output is written with html text which includes HTTP */ /* header lines identifying the content type of the data written and */ /* HTTP response headers. The end of the headers and the start */ /* of text is indicated by TWO new lines (\n\n). These two new lines */ /* MUST exist before writing any html text. This text is usually html */ /* but can be plain/text. */ /* */ /* Output: A UTF-8 encoded HTML page. This information is served by */ /* the HTTP server. */ /* */ /* Exit Normal: */ /* */ /* Exit Error: None */ /* */ /**********************************************************************/ void main() { FILE *fp; int rc; int envLength; //The length of the enviornment var char buf[] = "1234567890"; ErrT Err; //Capture error code int EnvStringCCSID = 1208; //CCSID of Env Var int VarLen; //The length of the Env Var int ValBufLen = BUFLEN; //The length of the buffer char ValBuf[BUFLEN]; //Buffer to store the Env Var that we get int ResLength = 0; //The length of the response int RetEnvVarCCSID = 1208; //Return the value in CCSID 1208 (UTF-8) int EnvVarLen; //The length of the env var to get /**********************************************************************/ /* By default stdout is assumed to have text data and be connected to */ /* an EBCDIC device,so c-runtime converts the 1208 data to EBCDIC data.*/ /* Because we are running in CGIConvMode BINARY we do not want data */ /* in EBCDIC. To avoid this conversion we must tell the c-runtime to */ /* assume the data is binary data (rather than textdata) and thus */ /* avoid conversion. This is done by setting the environment */ /* variable QIBM_DESCRIPTOR_TEXTDATA to 'N'. The environment */ /* variable must be encoded in the EBCDIC job ccsid. */ /* Our program is compiled in 1208 so we need to call QzsrPutEnvCCSID */ /* to set the environment variable, specifying the encoding of the value.*/ /* The API will handle setting the value in the job CCSID. */ /**********************************************************************/ char setTextdata[]="QIBM_DESCRIPTOR_TEXTDATA=N"; char getTextdata[]="QIBM_DESCRIPTOR_TEXTDATA"; memset(&Err, 0x0, sizeof(ErrT)); VarLen = strlen(setTextdata); QzsrPutEnvCCSID(setTextdata, &VarLen, &EnvStringCCSID, &Err); if (Err.BytesProv > 0) { /****************************************************************/ /* If the put fails, data to stdout will be converted to ebcdic.*/ /* For debugging purposes you can write to a file (which will */ /* not convert to ebcdic). To do this, uncomment the lines */ /* below, and specify a file that you have write permissions to */ /* (replace '/error.txt' with a path to a writeable file). */ /****************************************************************/ /* */ /* if ((fp = fopen("/error.txt","wb, o_ccsid=1208")) == NULL) { */ /* exit; */ /* } */ /* */ /* fprintf(fp,"Failed to set QIBM_DESCRIPTOR_TEXTDATA=N\n"); */ /* fprintf(fp,"ExceptID: %s\n",Err.ExceptID); */ /* fflush(fp); */ /* fclose(fp); */ /****************************************************************/ exit; } memset(&Err, 0x0, sizeof(ErrT)); EnvVarLen = strlen(getTextdata); /********************************************************************/ /* The "Content-type" is the minimum request header that must be */ /* written to standard output. It describes the type of data that */ /* follows. */ /********************************************************************/ printf("Content-Type: text/html; charset=UTF-8\n\n"); printf("LOCALEUTF CGI Program"); /**********************************************************************/ /* Now get the env variable that we just set, specifying that we want */ /* it in CCSID 1208. */ /**********************************************************************/ QzsrGetEnvCCSID(ValBuf, //The buffer to populate with the value &ValBufLen, //The length of the buffer to populate &ResLength, //The length of the response getTextdata, //The desired env var to get value of &EnvVarLen, //The length of the requested env var &EnvStringCCSID,//CCSID of the env var string &RetEnvVarCCSID,//CCSID that we want the value returned in &Err); if (Err.BytesProv > 0) { printf("Failed to get QIBM_DESCRIPTOR_TEXTDATA
"); printf("--errno: %d
",errno); exit; } printf("HELLO WORLD

UTF8 Buffer: |%s|

",buf); printf("QIBM_DESCRIPTOR_TEXTDATA=%s",ValBuf); }